我首先创建了一个自定义的Surface View来绘制内容;在视觉上描述,我画了一些线条和圆圈的绿色背景。然后,我决定删除绿色背景并将实时相机预览作为背景。这是我尝试过的,但无法使其正常工作(下图)。发生的情况是,正在创建摄像机视图和我的自定义表面视图,但仅显示摄像机视图。我确定我的自定义Surface View也是创建的,因为我在这个自定义视图中有一个TouchListener,它在触摸时打印x,y值(并且会发生这种情况)。但我的自定义绘图没有显示在相机预览上,正如我想要的那样。请帮忙!!
gameview.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</FrameLayout>
相机视图:
public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
Camera camera;
public CameraView(Context context, Camera c) {
super(context);
camera = c;
getHolder().addCallback(this);
getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// TODO Auto-generated constructor stub
}
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
try {
camera.setPreviewDisplay(getHolder());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}
}
名为VirtuaParkView的自定义曲面视图:这是我使用从线程连续调用的onDraw()方法绘制内容的地方。同时实现SurfaceHolder.Callback
。为简洁起见,此处未编写代码。
如何将它们组合在一起:
CameraView cView = new CameraView(getApplicationContext(),Camera.open()); //get Camera View
VirtuaParkView vParkView = new VirtuaParkView(getApplicationContext()); // get custom surface view
setContentView(R.layout.gameview); //set the Frame layout
FrameLayout fl = (FrameLayout)findViewById(R.id.mainlayout); // get the layout root
fl.addView(cView); //add camera view
fl.addView(vParkView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); //add custom drawing
答案 0 :(得分:0)
我不知道您的VirtualParkView是什么样的,但尝试使用此自定义视图。它应该画一个小的绿色方块,你触摸它。
public class CameraOverlay extends View {
private static final Integer rectSize = 30;
Paint paint;
private Rect touchRect;
public CameraOverlay(Context context) {
super(context);
paint = new Paint();
touchRect = new Rect(0,1,0,1);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
int height = canvas.getHeight();
int width = canvas.getWidth();
canvas.drawLine(width/2,0,width/2,height,paint);
canvas.drawLine(0,height/2,width,height/2,paint);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GREEN);
canvas.drawRect(touchRect, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "CameraOverlay - onTouchEvent()");
invalidate();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getX();
float y = event.getY();
touchRect.left = (int) (x - rectSize);
touchRect.top =(int) (y - rectSize);
touchRect.right = (int) (x + rectSize);
touchRect.bottom = (int) (y + rectSize);
}
return false;
}
}
然后,将其添加到您的框架中,而不是您的VirtualParkView -
if (mCameraOverlay == null)
mCameraOverlay = new CameraOverlay(this);
f1.addView(mCameraOverlay);