Reverted Android layout and Activity changes back to original with addition of simple sight line

There was no need to completely change our implementation and remove
the top and bottom text views
This commit is contained in:
TJ Woon 2015-02-27 18:28:16 +08:00
parent cc404e0314
commit 2a8a237a97
2 changed files with 68 additions and 91 deletions

View File

@ -8,10 +8,6 @@ import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.ShapeDrawable;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PreviewCallback;
@ -24,9 +20,7 @@ import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import net.sourceforge.zbar.ImageScanner;
@ -61,15 +55,6 @@ implements SurfaceHolder.Callback {
String whichCamera;
String flashMode;
/* START - ALMAVIVA */
RelativeLayout relativeLayout;
RelativeLayout line;
View parent;
int width;
int height;
boolean drawSight = false;
/* END - ALMAVIVA */
// For retrieving R.* resources, from the actual app package
// (we can't use actual.application.package.R.* in our code as we
// don't know the applciation package name when writing this plugin).
@ -98,6 +83,7 @@ implements SurfaceHolder.Callback {
catch (JSONException e) { params = new JSONObject(); }
String textTitle = params.optString("text_title");
String textInstructions = params.optString("text_instructions");
Boolean drawSight = params.optBoolean("drawSight", true);
whichCamera = params.optString("camera");
flashMode = params.optString("flash");
@ -107,11 +93,19 @@ implements SurfaceHolder.Callback {
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
drawSight = params.optString("drawSight") != null ? Boolean.valueOf(params.optString("drawSight").toLowerCase()) : true;
// Set content view
setContentView(getResourceId("layout/cszbarscanner"));
// Update view with customisable strings
TextView view_textTitle = (TextView) findViewById(getResourceId("id/csZbarScannerTitle"));
TextView view_textInstructions = (TextView) findViewById(getResourceId("id/csZbarScannerInstructions"));
view_textTitle.setText(textTitle);
view_textInstructions.setText(textInstructions);
// Draw/hide the sight
if(!drawSight) {
findViewById(getResourceId("id/csZbarScannerSight")).setVisibility(View.INVISIBLE);
}
// Create preview SurfaceView
scannerSurface = new SurfaceView (this) {
@ -130,64 +124,14 @@ implements SurfaceHolder.Callback {
scannerSurface.getHolder().addCallback(this);
// Add preview SurfaceView to the screen
((FrameLayout) findViewById(getResourceId("id/csZbarScannerView"))).addView(scannerSurface);
/* START - ALMAVIVA */
// Creating a new RelativeLayout
if(drawSight){
relativeLayout = new RelativeLayout(this);
line = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
parent = ((FrameLayout) findViewById(getResourceId("id/csZbarScannerView")));
parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Ensure you call it only once :
parent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
width = parent.getWidth();
height = parent.getHeight();
double dim = width < height ? (width / 1.2) : (height / 1.2);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams((int)dim,(int)dim);
rlp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
rlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
rlp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
relativeLayout.setGravity(Gravity.CENTER);
relativeLayout.setLayoutParams(rlp);
relativeLayout.invalidate();
relativeLayout.requestLayout();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(8,((int)dim - 16));
lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
line.setGravity(Gravity.CENTER);
line.setLayoutParams(lp);
line.setBackgroundColor(Color.RED);
line.invalidate();
line.requestLayout();
}
});
ShapeDrawable rectShapeDrawable = new ShapeDrawable(); // pre defined class
// get paint
Paint paint = rectShapeDrawable.getPaint();
// set border color, stroke and stroke width
paint.setColor(Color.GREEN);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(8); // you can change the value of 5
//relativeLayout.setBackgroundDrawable(rectShapeDrawable);
relativeLayout.addView(line);
((RelativeLayout) findViewById(getResourceId("id/csZbarScannerViewContainer"))).addView(relativeLayout);
}
/* END - ALMAVIVA */
FrameLayout scannerView = (FrameLayout) findViewById(getResourceId("id/csZbarScannerView"));
scannerView.addView(scannerSurface);
findViewById(getResourceId("id/csZbarScannerTitle")).bringToFront();
findViewById(getResourceId("id/csZbarScannerInstructions")).bringToFront();
findViewById(getResourceId("id/csZbarScannerSightContainer")).bringToFront();
findViewById(getResourceId("id/csZbarScannerSight")).bringToFront();
scannerView.requestLayout();
scannerView.invalidate();
}
@Override
@ -294,12 +238,7 @@ implements SurfaceHolder.Callback {
private AutoFocusCallback autoFocusCb = new AutoFocusCallback()
{
public void onAutoFocus(boolean success, Camera camera) {
try{
camera.cancelAutoFocus();
autoFocusHandler.postDelayed(doAutoFocus, autoFocusInterval);
}catch(Exception e){
}
}
};

View File

@ -1,15 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/csZbarScannerViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/csZbarScannerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/csZbarScannerBackground" >
</FrameLayout>
<TextView android:id="@+id/csZbarScannerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:paddingTop="15dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:gravity="center"
android:text="@string/csZbarScannerTitle"
android:textColor="@color/csZbarScannerTextColor"
android:background="@color/csZbarScannerTextBackground"
android:fontFamily="sans-serif-light"
android:textSize="20pt" />
</RelativeLayout>
<TextView android:id="@+id/csZbarScannerInstructions"
android:layout_gravity="bottom|center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="15dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:gravity="center"
android:textSize="12pt"
android:textColor="@color/csZbarScannerTextColor"
android:background="@color/csZbarScannerTextBackground"
android:fontFamily="sans-serif-light"
android:text="@string/csZbarScannerInstructions" />
<RelativeLayout android:id="@+id/csZbarScannerSightContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View android:id="@+id/csZbarScannerSight"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:background="#ff0000" />
</RelativeLayout>
</FrameLayout>