Canvas.drawBitmap或Canvas.drawColor无法正常工作

时间:2014-11-05 13:40:05

标签: android android-canvas surfaceview android-bitmap

我需要以15 fps的速度在Surfaceview上绘制位图,我的SurfaceView如下

public class RawVideoViewSV extends SurfaceView implements SurfaceHolder.Callback{

private boolean isSurface;
private Bitmap mBitmap;
private SurfaceHolder holder;

public RawVideoViewSV(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
     holder = getHolder();
     holder.addCallback(this); 
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    isSurface = false;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    isSurface = true;
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    isSurface = false;
}

public SurfaceHolder getSurfaceHolder(){
    return holder;
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    if(isSurface){
        canvas.drawColor(Color.GREEN);
        if(mBitmap != null)
            canvas.drawBitmap(mBitmap, 0, 0, null);
    }
}


public void doDraw(Canvas canvas,Bitmap bmp) {
    // TODO Auto-generated method stub
    System.out.println("RawVideoViewSV---> doDraw isSurface = " + isSurface + bmp.getHeight());
    Rect source = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
    if(isSurface){
        canvas.drawColor(Color.GREEN);
        if(bmp != null)
            canvas.drawBitmap(bmp, null,source, null);
    }
}}

在我的活动中我正在创建将其传递到Surface上绘制的位图,但由于某种原因,canvas.drawcolor既不工作也没有canvas.drawbitmap在surfacview中工作。

rawVideoViewSV0 = (RawVideoViewSV) findViewById(R.id.videoRawViewSV0);
Bitmap bmpx = Bitmap.createBitmap(rgb, videoSample.getWidth(),
videoSample.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas mCanvas = null;
    SurfaceHolder mHolder = rawVideoViewSV0.getHolder();

    try {
        mCanvas = mHolder.lockCanvas();
        synchronized (mHolder) {    
            rawVideoViewSV0.doDraw(mCanvas,bmpx);
        }
    } finally {                   
        if (mCanvas != null) {
            mHolder.unlockCanvasAndPost(mCanvas);
        }
    }

rawVideoViewSV0的类型为RawVideoViewSV。 我已经验证我的位图不是空的,只有第一次调用onDraw时,surfaceview才显示为绿色。

请有人指出我哪里出错了。

1 个答案:

答案 0 :(得分:0)

SurfaceView有两个部分,Surface和View。视图是使用UI的其余部分绘制的,但通常只是一个透明的"孔"用于布局目的。 Surface显示在UI下方的单独图层上。

您似乎正在将视频帧写入Surface,然后使用纯绿色视图覆盖它们。删除onDraw()函数(获取View部件的Canvas)并执行doDraw()中的所有操作(获取Surface部件的Canvas)。