Android 在相机上方显示可调整大小的矩形

Android Displaying resizable rectangle above camera

我正在尝试构建一个 Android 程序,在相机视图上使用可调整大小的矩形,以此作为指南: How to create a resizable rectangle with user touch events on Android?

由于无法查看相对布局上的显示并且找不到问题(我比较新),我已经卡了一天。

这是我的 XML 文件:

<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>

这是我的初始化:

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 覆盖 onDraw 并实现 onTouchEvent,基本上与 Nguyen Minh Binh 发布的解决方案相同:How to create a resizable rectangle with user touch events on Android?

有人可以帮我解决我遗漏的问题吗?

谢谢

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

您创建了 class DrawView 的新对象,但在下一行中您将不同的对象分配给同一变量。 DrawView 现在基本上丢失了。您可以使用 addView(drawView) 而不是第二行,但最简单的方法是使用

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

在你的 layout.xml