无法在相机预览中绘制矩形

having trouble drawing rectangle on camera preview

我正在尝试在相机预览的顶部绘制一个矩形叠加层。我在 FrameView 中获得了叠加层,并且成功调用了 onDraw() 但矩形从未出现。我四处搜索并尝试了不同的解决方案,但 none 到目前为止对我有用。

这就是我的 FrameView

public class FrameView extends View {
    private Paint mFramePaint = null;
    private final String TAG = "FrameView";

    public FrameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mFramePaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mFramePaint.setStyle(Paint.Style.FILL);
        mFramePaint.setColor(Color.GREEN);

        Rect drawRect = new Rect(300,300,300,300);

        canvas.drawRect(drawRect, mFramePaint);
    }
}

这是我的 xml 相机预览 activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.eyesee.CameraPreviewActivity" 
    android:id="@+id/camera_preview">

    <com.example.eyesee.FrameView
        android:id="@+id/frame_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

这是我的相机预览中的代码片段activity,用于附加相机预览

private Camera mCamera;
private CameraPreview mPreview;
private final String TAG = "CameraPreviewActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_preview);

    mCamera = MainActivity.getCameraInstance();

    mPreview = new CameraPreview(this, mCamera);
    RelativeLayout preview = (RelativeLayout)   findViewById(R.id.camera_preview);
    preview.addView(mPreview);
}

在RelativeLayout中,子视图是按顺序绘制的, 您需要在视图下方添加预览:

preview.addView(mPreview,0);

你的矩形大小为 0 - new Rect(left,top,right,bottom)

Rect drawRect = new Rect(0,0,300,300);