Android:如何撤消已绘制到 canvas 的位图?
Android: How do I undo a bitmap which has been drawn to a canvas?
我正在将可绘制对象放在 canvas 之类的标记上,将最终位图设置为 ImageView。如何添加一次可以撤消一个可绘制对象的撤消功能?请帮忙。
这是我的代码:
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));
我想出了一个窍门。我使用 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);
}
}
如果有人能提供优化的方案就好了
我正在将可绘制对象放在 canvas 之类的标记上,将最终位图设置为 ImageView。如何添加一次可以撤消一个可绘制对象的撤消功能?请帮忙。
这是我的代码:
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));
我想出了一个窍门。我使用 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);
}
}
如果有人能提供优化的方案就好了