android - View 中的多个 Canvas

标签 android view canvas bitmap

我已经重写了 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/

相关文章:

javascript - 无法读取未定义的属性 '-1'

android - 绘图引用

java - 当我的游戏应该重新启动时,我应该如何处理重置 SurfaceView 和 Thread?

html - 在 HTML5 Canvas 上绘制多个图像有时不起作用

android - 如何将 "complex"阴影添加到 android 中的 View ?

具有 5 个表连接的 MySQL View 不返回任何行

javascript - 部分透明(opacity)的HTML5 Canvas绘图

android - 我们如何在设备上为基于 phonegap 的 iOS 和 android 应用程序维护错误日志文件 (.txt)?

Java/Android 子弹对象流程

php - 如何在二维数组中不重复值的情况下在另一个循环内创建一个循环