【问题标题】:Android custom surface view drawing on camera surface viewAndroid 自定义表面视图绘制在相机表面视图上
【发布时间】:2013-05-24 01:01:00
【问题描述】:

我首先创建了一个自定义的 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

【问题讨论】:

    标签: java android android-camera android-canvas surfaceview


    【解决方案1】:

    我不知道您的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2011-09-13
      • 2015-01-04
      相关资源
      最近更新 更多