0

I am attempting to build an Android program with a resizable rectangle over a camera view using this as a guide: How to create a resizable rectangle with user touch events on Android?

I have been stuck for a day as I am unable view the display over the relative layout and can't find the issue (I'm relatively new).

Here is my XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/camera_preview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View
        android:id="@+id/camera_box"
        android:layout_alignParentTop="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent">
    </View>

    <Button
        android:id="@+id/button_ChangeCamera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:text="Switch Camera"
        android:textColor="@android:color/holo_orange_dark"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"
        android:minWidth="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"/>
    <Button
        android:id="@+id/button_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:background="@android:color/white"
        android:text="Capture"
        android:textColor="@android:color/holo_orange_dark"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

and here is my initialization:

public class TakePhoto extends Activity {
private Camera mCamera;
private PhotoHandler mPreview;
private PictureCallback mPicture;
private Button capture, switchCamera;
private View drawView;
private Context myContext;
private RelativeLayout cameraPreview;
private boolean cameraFront = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_page);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    myContext = this;
    initialize();
}

public void initialize() {
    cameraPreview = (RelativeLayout) findViewById(R.id.camera_preview);

    mPreview = new PhotoHandler(myContext, mCamera);
    cameraPreview.addView(mPreview);

    capture = (Button) findViewById(R.id.button_capture);
    capture.setOnClickListener(captrureListener);

    switchCamera = (Button) findViewById(R.id.button_ChangeCamera);
    switchCamera.setOnClickListener(switchCameraListener);
    drawView = new DrawView(myContext);
    drawView = findViewById(R.id.camera_box);
    drawView.bringToFront();

}

//more code

}

DrawView overwrites onDraw and implements onTouchEvent, basically as done the solution posted by Nguyen Minh Binh in: How to create a resizable rectangle with user touch events on Android?

Could someone help with what I am missing?

Thanks

Alexei
  • 3
  • 2

1 Answers1

0
drawView = new DrawView(myContext);
drawView = findViewById(R.id.camera_box);

You created a new object of class DrawView, but in the next line you assign a different object to the same variable. The DrawView is essentially lost now. Instead of the second line, you could use addView(drawView), but the easiest way is to use

<…package…DrawView android:id="@+id/camera_box"

in your layout.xml

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307