0

我首先创建了一个自定义 Surface View 来绘制东西;从视觉上描述,我在绿色背景上画了一些线条和圆圈。然后,我决定移除绿色背景,并使用实时相机预览作为背景。这是我尝试过的,但无法正常工作(如下)。发生的情况是,正在创建相机视图和我的自定义表面视图,但只显示相机视图。我确定我的自定义 Surface 视图也被创建,因为我在这个自定义视图中有一个 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
4

1 回答 1

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);
于 2013-05-29T05:28:18.830 回答