简体   繁体   English

Android自定义表面视图在相机表面视图上绘制

[英]Android custom surface view drawing on camera surface view

I first created a custom Surface View to draw things on; 我首先创建了一个自定义的Surface View来进行绘制; to visually describe, a green background on which I draw some lines and circles. 从视觉上描述一个绿色的背景,我在上面画了一些线和圆。 Then, I decided to remove the green background and have a live camera preview as the background instead. 然后,我决定删除绿色背景,并以实时摄像机预览作为背景。 This is what I tried, but could not get it working (below). 这是我尝试过的方法,但无法使其正常工作(下)。 What happens is, both the camera view and my custom surface view are being created, but only the camera view is being shown. 发生的是,同时创建了相机视图和我的自定义表面视图,但是仅显示了相机视图。 I'm sure my custom Surface View is also created because I've a TouchListener in this custom view that prints x,y values when touched (and this happens). 我确定我的自定义表面视图也已创建,因为我在此自定义视图中有一个TouchListener,在触摸时会打印x,y值(这会发生)。 But my custom drawing is not being shown over the camera preview, as I want it to be. 但是我的自定义工程图并没有显示在摄像机预览中,正如我希望的那样。 Please help!! 请帮忙!!

gameview.xml file: 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>

Camera View: 相机视图:

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();
}
}

Custom Surface View named VirtuaParkView: This is where I draw stuff using onDraw() method being called continuously from a thread. 名为VirtuaParkView的自定义表面视图:在这里,我使用从线程连续调用的onDraw()方法绘制内容。 Also implements SurfaceHolder.Callback . 还实现SurfaceHolder.Callback Code not written here for brevity. 为简洁起见,此处未编写代码。

How it's all put together: 如何将它们组合在一起:

   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

I don't know what your VirtualParkView looks like but try using this custom View. 我不知道您的VirtualParkView是什么样子,但是请尝试使用此自定义视图。 It should draw a small green square where you touch it. 它应该在您触摸的地方绘制一个绿色小方块。

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;
}
}

Then, instead of your VirtualParkView, add this to your frame - 然后,将其添加到框架中,而不是VirtualParkView-

    if (mCameraOverlay == null)
        mCameraOverlay = new CameraOverlay(this);
    f1.addView(mCameraOverlay);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM