我试图在相机预览上绘制一个矩形叠加层。我在FrameView中获得了叠加层,onDraw()被成功调用,但矩形从不显示。我已经四处搜索并尝试了不同的解决方案,但到目前为止还没有一个对我有效。
这就是我对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
<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>
这是我的相机预览活动中用于附加相机预览的代码片段
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);
}