2012-10-16 124 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