我已经覆盖了onDraw()方法,如下所示:
public void onDraw(Canvas canvas1){
Canvas canvas2 = new Canvas();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.graphic1);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.graphic2);
canvas1.drawBitmap(
top,
new Rect(0, 0, graphic1.getWidth(), graphic1.getHeight()),
new Rect(0, 0, width, width),
null);
canvas2.drawBitmap(
top,
new Rect(0, 0, graphic2.getWidth(), graphic2.getHeight()),
new Rect(0, 0, width, width),
null);
}
只显示canvas1上的graphic1,canvas2和graphic2不显示。如何在一个视图上显示多个画布?
答案 0 :(得分:3)
正如评论所说,你没有将Canvas2附加到任何东西上。你在每一帧(这是坏的)创建它,绘制它,然后让它的范围被垃圾收集。您应该做什么创建Canvas2,在视图的构造函数中使用支持Bitmap并将其保留为成员。然后你可以绘制它,然后将它的Bitmap blit到Canvas1。例如:
public MyCustomView(Context context)
{
super(context);
_canvas2 = new Canvas(_backingBitmap);
}
public void onDraw(Canvas canvas1)
{
Bitmap graphic1 = BitmapFactory.decodeResource(getResources(), R.drawable.graphic1);
Bitmap graphic2 = BitmapFactory.decodeResource(getResources(), R.drawable.graphic2);
canvas1.drawBitmap(
top,
new Rect(0, 0, graphic1.getWidth(), graphic1.getHeight()),
new Rect(0, 0, width, width),
null);
_canvas2.drawBitmap(
top,
new Rect(0, 0, graphic2.getWidth(), graphic2.getHeight()),
new Rect(0, 0, width, width),
null);
canvas1.drawBitmap(
top,
new Rect(0, 0, _backingBitmap.getWidth(), _backingBitmap.getHeight()),
new Rect(0, 0, width, width),
null);
}