2012-10-16 154 views
4

我想在surfaceview上添加圖像並將其設置爲可點擊。我可以通過xml使用佈局來實現。但是我無法從代碼動態地執行它。我甚至不能使用lockcanvas在畫布上繪製圖像,因爲我將SurfaceHolder類型設置爲SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS.So我必須在我的應用程序中在Surfaceview上繪製圖像。請引導我?如何在surfaceview上繪製圖像android

+0

其它的參考[DroidNova系列(http://www.droidnova.com/2d-tutorial-series-part-i,770.html) –

回答

8

有些事情就像

public class MSurface extends SurfaceView implements SurfaceHolder.Callback { 

    public MSurface(Context context) { 
     super(context); 
     getHolder().addCallback(this); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.icon); 
     canvas.drawColor(Color.BLACK); 
     canvas.drawBitmap(icon, 10, 10, new Paint());   
    } 

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

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     Canvas canvas = null; 
     try { 
      canvas = holder.lockCanvas(null); 
      synchronized (holder) { 
       onDraw(canvas); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (canvas != null) { 
       holder.unlockCanvasAndPost(canvas); 
      } 
     } 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 

    } 
} 
+1

如何設置在surfaceview nad中心的圖像如何將其擴展到整個surfaceview –

+0

如何縮放位圖或填充/ streach以覆蓋整個surfaceView? – TomeeNS

+0

與上面類似的問題? – kemdo