Android:如何撤消已绘制到画布的位图?

时间:2015-01-30 09:55:24

标签: android canvas bitmap markers

我将drawables放在画布上,比如标记,将最终的位图设置为ImageView。如何添加撤消功能,我可以一次撤消一个drawable?请帮忙。

这是我的代码:

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

//Create a new image bitmap and attach a brand new canvas to it
Bitmap tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);

 Canvas tempCanvas = new Canvas(tempBitmap);

 //Draw the image bitmap into the canvas
 tempCanvas.drawBitmap(bitmap, 0, 0, null);

 Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.check);

 //Draw into the canvas
 tempCanvas.drawBitmap(marker, x, y, null);

 //Attach the canvas to the ImageView
 imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));

1 个答案:

答案 0 :(得分:0)

我发现了一个黑客。我使用ArrayList在每一滴标记后存储带注释的位图。我只是弹出列表中的最后一项,并在撤消时显示新的位图。如果有人有兴趣,这里是代码:

bitmapLayers.add(annotatedBitmap);

对于撤消:

if (!bitmapLayers.isEmpty())
{
      bitmapLayers.remove(bitmapLayers.size() - 1);
      Bitmap last = bitmapLayers.get(bitmapLayers.size() - 1);
      imageView.setImageBitmap(last);

      //If no undos left, hide button
      if (bitmapLayers.size() == 1)
      {
          mUndo.setVisible(false);
      }
 }

如果有人能提供优化的解决方案,那就太棒了。