我是Android应用程序开发的新手。我想创建一个应用程序,从摄像头获取流并在SurfaceView或FrameLayout上显示。
我需要一个选项在流上方显示“显示网格线”,当用户单击它时,网格线将显示在相机流上。
有人可以帮我解决如何在相机流上显示网格线吗?
任何帮助都将不胜感激...
谢谢。 Mohsin
我是Android应用程序开发的新手。我想创建一个应用程序,从摄像头获取流并在SurfaceView或FrameLayout上显示。
我需要一个选项在流上方显示“显示网格线”,当用户单击它时,网格线将显示在相机流上。
有人可以帮我解决如何在相机流上显示网格线吗?
任何帮助都将不胜感激...
谢谢。 Mohsin
@Override
protected void onDraw(Canvas canvas)
{
if(grid){
// Find Screen size first
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = (int) (metrics.heightPixels*0.9);
// Set paint options
paint.setAntiAlias(true);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.argb(255, 255, 255, 255));
canvas.drawLine((screenWidth/3)*2,0,(screenWidth/3)*2,screenHeight,paint);
canvas.drawLine((screenWidth/3),0,(screenWidth/3),screenHeight,paint);
canvas.drawLine(0,(screenHeight/3)*2,screenWidth,(screenHeight/3)*2,paint);
canvas.drawLine(0,(screenHeight/3),screenWidth,(screenHeight/3),paint);
}
}
你还需要在相机预览类的构造函数中添加以下行:
this.setWillNotDraw(false);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ImageView
android:id="@+id/grid"
android:src="@drawable/your_grid_drawable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>