我试图在画布上绘制多个图像并显示它但我只是不知道如何。
这是我的代码:
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);
}
}
答案 0 :(得分:4)
我没有在Canvas上使用Drawables,但我可以肯定地告诉你,使用位图它可以正常工作:
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
答案 1 :(得分:2)
您不需要创建画布的新实例,只需要执行md2.draw(canvas)。你也可以试试:
当你只绘制mDrawable时会发生什么,然后只有md2,它们会显示吗?