2014-11-05 69 views
0

我需要在每秒15幀的速度借鑑Surfaceview位圖,我的SurfaceView是如下Canvas.drawBitmap或Canvas.drawColor不工作

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

在我的活動正在創建其正在傳遞到表面,以位圖繪製,但由於某種原因,既不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類型。 我已驗證我的位圖不爲null,只有在第一次調用onDraw時,surfaceview顯示爲綠色,就是它。

請有人指出我哪裏會出錯。

+0

你知道surfaceview將「不會繪製」標誌設置爲true嗎? ...無論你想在這裏存檔(水印在cam/file上的視頻?),你做錯了... – Selvin 2014-11-05 13:51:49

+0

我已經知道我錯了它現在已經工作了...感謝您的快速回復.. – zenith 2014-11-06 07:10:04

回答

0

SurfaceView有兩部分,Surface和View。視圖與UI的其餘部分一起繪製,但通常只是用於佈局目的的透明「洞」。 Surface將出現在UI下面的單獨圖層上。

它看起來像你正在寫視頻幀到表面,然後用堅實的綠色視圖覆蓋它們。刪除onDraw()函數(它爲查看部分獲取畫布)並執行doDraw()(其獲取用於表面部分的畫布)中的所有內容。