无法在另一个SurfaceView顶部的SurfaceView上绘制或渲染

时间:2014-04-12 03:03:09

标签: android android-layout surfaceview

我正在尝试从菜单/图形中实现构建块,我可以在我的应用程序屏幕上移动,显示相机预览。

我的ScanImageBarcodeActivity包含名为CameraPreview的自定义SurfaceView,它显示了实时相机和放大器。用于扫描条形码的指导矩形(这部分工作正常,我能够显示实时视频流和解码条形码)。

我创建了另一个名为“OverlayOfSurfaceView”的自定义 SurfaceView,我想将其置于CameraPreview之上,以便绘制一些图形和图像。显示菜单 问题是“OverlayOfSurfaceView”onDraw()只被称为几次&我再也没有打过电话,因此我看不出测试图形应该是一个“蓝色条形图”。

实际上奇怪的是当停止相机预览时,我看到我用来测试OverlayOfSurfaceView的onDraw的蓝线。

以下是我的ScanImageBarcodeActivity的相关部分:

public class ScanImageBarcodeActivity extends Activity {

//removed irrelevant code for this post 

    // onCreate, instantiates layouts & surfaceView used for video preview
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_barcode_vin_scanner);
        Log.d("ClassScanViewBarcodeActivity", "onCreate ");

        // inflate layout will that contain camera preview
        FrameLayoutBarcodeScanner = (FrameLayout) findViewById(R.id.LayoutForPreview);

        // inflate surface view that will be on top of camera preview surface
        // view
        surfaceViewLayOver = (OverlayOfSurfaceView) FrameLayoutBarcodeScanner
                .findViewById(R.id.overlay);
        // make the background transparent so the user can see cameraPreview
        // behind surfaceViewLayOver
        surfaceViewLayOver.setAlpha(0);
        // 0 is teh default means visible
        surfaceViewLayOver.setVisibility(0);
        globalContext = this.getApplicationContext();
        // ini all camera & barcode API settings & start preview
        initializeGlobalCamera();

        handler = new Handler();

        /*
         * if surfaceViewLayOver has been instantiated & hopefully inflated
         * create Thread that tries to draw grahics on the canvas of
         * surfaceViewLayOver
         */
        if (surfaceViewLayOver != null) {
            // draw in another thread
            new Thread() {

                public void run() {
                    // while the camera output is being displayed
                    while (cameraPreviewing != false) {
                        // sleep 30 millisecs to control frame/draw rate
                        try {

                            Thread.sleep(30);

                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        SurfaceHolder holder = surfaceViewLayOver.getHolder();
                        // get canvas to draw
                        Canvas canvas = holder.lockCanvas();

                        try {
                            if (canvas != null) {
                                synchronized (holder) {
                                    // call onDraw of custom SurfaceView
                                    surfaceViewLayOver.onDraw(canvas);

                                }

                            }
                        } finally {
                            if (canvas != null)
                                holder.unlockCanvasAndPost(canvas);
                            canvas = null;
                            // yield other threads can render graphics to the
                            // screen
                            // Thread.yield();

                        }

                    }// end while

                }// end run
            }.start();
        } else {
            Log.d(this.getClass().getName(), "surfaceViewLayOver is: "
                    + surfaceViewLayOver);
        }
//removed irrelevant code for this post 
    }// end onCreate
}//end activity

布局,activity_barcode_vin_scanner.xml是我的Activity& amp;的布局。包含OverlayOfSurfaceView的布局代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LayoutForPreview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:scaleType="fitXY"
    android:screenOrientation="portrait" >

    <com.net.app.barcode.OverlayOfSurfaceView
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
      />
</FrameLayout>

下面是我的OverlayOfSurfaceView:

package com.net.app.barcode;

import com.net.app.R;

//removed irrelevant code 

public class OverlayOfSurfaceView extends SurfaceView  implements
SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Context context;
    private LinearLayout linearMenu = null; 
    public OverlayOfSurfaceView(Context context, AttributeSet attr) {
        super(context,attr);
        this.context = context;
        // TODO Auto-generated constructor stub
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        linearMenu = (LinearLayout) this.findViewById(R.layout.barcode_scanner_overlay_menu);
        setFocusable(true);
        requestFocus();

    }

    @Override
    public void onDraw(Canvas canvas) {


  drawGuidance(canvas);
  super.onDraw(canvas);
    }

    public void drawGuidance(Canvas canvas) {

        Log.w(this.getClass().getName(),"drawGuidance(Canvas canvas)");
        Paint paintRed = new Paint();
        paintRed.setStyle(Paint.Style.STROKE);
        paintRed.setStrokeWidth(6f);
        paintRed.setAntiAlias(true);
        paintRed.setColor(Color.BLUE);
        // drawMiddleline
        canvas.drawLine(0,this.getHeight()/2,this.getWidth(),this.getHeight()/2 , paintRed);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
         Log.w(this.getClass().getName(),"surface created");
        // TODO Auto-generated method stub
        setWillNotDraw(false);
        // TODO Auto-generated method stub
        // If your preview can change or rotate, take care of those events
        // here.


    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
         Log.w(this.getClass().getName(),"surface changed");


    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
         Log.w(this.getClass().getName(),"surface destroyed");
        // TODO Auto-generated method stub
        // empty. Take care of releasing resources here 
    }

}//end class

下面是实际快照(在OverlayOfSurfaceView上没有绘制蓝线,不能确认是否绘制/可见OverlayOfSurfaceView,虽然我知道&#34;表面已创建&#34;通过日志)。

enter image description here

OverlayOfSurfaceView SurfaceView应该在CameraPreview的顶部绘制一条蓝线,如下图所示,但不是:

enter image description here

最有可能的问题是什么?如果可能的话,具体是什么?请指出工作代码。

万分感谢!

0 个答案:

没有答案