我已经重写了 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。如何让多个 Canvas 显示在一个 View 上?
最佳答案
正如评论所说,您没有将 Canvas2 附加到任何东西上。你在每一帧创建它(这很糟糕),绘制它然后让它进入我们的范围以被垃圾收集。你应该做什么它在 View 的构造函数中创建带有支持位图的 Canvas2 并将其保留为成员。然后你可以绘制它,然后将它的位图 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);
}
关于android - View 中的多个 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6189755/