2012-02-10 117 views
1

我想在画布上绘制多个图像并显示它,但我只是不知道如何。在Android上的画布上绘制多个图像

这里是我的代码:

public class CustomDrawableView extends View { 
private Drawable mDrawable; 
private Drawable mD2; 

public CustomDrawableView(Context context) { 
    super(context); 

    int x = 40; 
    int y = 100; 
    int width = 20; 
    int height = 10; 

    Resources res = context.getResources(); 
    mDrawable = res.getDrawable(R.drawable.main); 
    mDrawable.setBounds(x, y, x + width, y + height); 

    mD2 = res.getDrawable(R.drawable.virus); 
    mD2.setBounds(x+50,y-70,width+10,height+5); 
} 

protected void onDraw(Canvas canvas) { 
    mDrawable.draw(canvas); 
    Canvas canvas2 = new Canvas(); 
    mD2.draw(canvas2); 
} 
} 
+2

您不需要创建画布的新实例,只需执行 md2.draw(canvas)。 – user710502 2012-02-10 03:15:02

+0

我尝试过......但它没有奏效。这就是为什么我想可能有2个画布会起作用... – 2012-02-10 03:17:49

+0

当你只绘制mDrawable,然后只绘制md2时,会发生什么? – user710502 2012-02-10 03:26:13

回答

2

你不需要创建一个画布的新实例,只需做md2.draw(canvas)。而且你也可以尝试:

当你只绘制mDrawable,然后只绘制md2时,会发生什么?

4

我还没有可绘制在画布的工作,但我可以告诉你肯定是位图它只是罚款是这样的:

canvas.drawBitmap(bmp1,0,0,myPaint); // draws in top left corner 
canvas.drawBitmap(bmp2,100,100, myPaint); // draws at an offset of 100 px on both the X and the Y axis 
+0

感谢它的工作。 – 2013-06-27 13:24:57