我是Android开发新手,我发现很难在camera2 api上找到好的例子。
我在大多数问题上慢慢地工作,但在这个问题上,我被困住了。 在默认相机中,当您触摸屏幕进行对焦时,它会暂时显示焦点区域的矩形。我想做类似的事情(或者在这种情况下,完全相同的事情开始,所以我可以弄清楚。)
我在某处读到(我认为SDK文档中的TextureView页面),当它被用作相机预览时,你无法在textureview上绘制 - 并且调用lock方法将返回null而不是画布。
我在网上找到了这个:https://github.com/commonsguy/vidtry/ 但是我无法让它发挥作用。我或者得到错误说我的主视图不能投射到我的可绘制视图,反之亦然 - 或者我的可绘制视图位于顶部并使屏幕变黑 - 或者它在底部并且不会响应触摸事件(并试图强制从上面的视图中执行点击它会导致崩溃。)
我被卡住了!任何人都可以给我一个解释或示例,说明如何在事件位置上绘制矩形几秒钟?谢谢!
答案 0 :(得分:5)
我在这里给你一个经过测试的代码来做这件事。我希望它可以帮助你。最幸运的。
private class CustomView extends SurfaceView {
private final Paint paint;
private final SurfaceHolder mHolder;
private final Context context;
public CustomView(Camera2BasicFragment context) {
super(context.getActivity().getBaseContext());
mHolder = getHolder();
mHolder.setFormat(PixelFormat.TRANSPARENT);
this.context = context.getActivity().getBaseContext();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
invalidate();
if (mHolder.getSurface().isValid()) {
final Canvas canvas = mHolder.lockCanvas();
Log.d("touch", "touchRecieved by camera");
if (canvas != null) {
Log.d("touch", "touchRecieved CANVAS STILL Not Null");
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawColor(Color.TRANSPARENT);
canvas.drawCircle(event.getX(), event.getY(), 100, paint);
mHolder.unlockCanvasAndPost(canvas);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Canvas canvas1 = mHolder.lockCanvas();
if(canvas1 !=null){
canvas1.drawColor(0, PorterDuff.Mode.CLEAR);
mHolder.unlockCanvasAndPost(canvas1);
}
}
}, 1000);
}
mHolder.unlockCanvasAndPost(canvas);
}
}
return false;
}
}
答案 1 :(得分:2)
对于那些正在寻找完整代码的人,此示例代码在相机预览上绘制了一个小矩形。只需添加表面视图并在Camera2BasicFragment项目中修改onViewCreated函数即可。
public class Camera2BasicFragment extends Fragment
implements View.OnClickListener, ActivityCompat.OnRequestPermissionsResultCallback {
SurfaceView surfaceView;
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
view.findViewById(R.id.picture).setOnClickListener(this);
view.findViewById(R.id.info).setOnClickListener(this);
mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture);
surfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);
surfaceView.setZOrderOnTop(true);
SurfaceHolder mHolder = surfaceView.getHolder();
mHolder.setFormat(PixelFormat.TRANSPARENT);
mHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
if (canvas == null) {
Log.e(TAG, "Cannot draw onto the canvas as it's null");
} else {
Paint myPaint = new Paint();
myPaint.setColor(Color.rgb(100, 20, 50));
myPaint.setStrokeWidth(10);
myPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(100, 100, 200, 200, myPaint);
holder.unlockCanvasAndPost(canvas);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
fragment_camera2_basic.xml文件具有其他表面视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.android.camera2basic.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/surfaceView"/>
<FrameLayout
android:id="@+id/control"
android:layout_width="match_parent"
android:layout_height="112dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="@color/control_background">
<Button
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/picture" />
<ImageButton
android:id="@+id/info"
android:contentDescription="@string/description_info"
style="@android:style/Widget.Material.Light.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:padding="20dp"
android:src="@drawable/ic_action_info" />
</FrameLayout>
</RelativeLayout>
答案 2 :(得分:1)
我解决了 - 我必须将表面视图设置为drawColor(Color.TRANSPARENT),并带有清除标记,一切都很好。