撤消绘图图像编辑器 android?
Undo drawing image editor android?
对于我的应用程序,我正在创建一个图像编辑器,它目前工作正常,但我想实现取消撤消按钮,它基本上可以返回到图像的先前状态。我试图通过使用位图的 ArrayList 来做到这一点。起初我使用此代码从图库中打开图像并设置 canvas:
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
.getHeight(), bmp.getConfig());
int x = alteredBitmap.getWidth();
int y = alteredBitmap.getHeight();
if (x > y) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
lscape = true;
} else {
lscape = false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
paint.setStyle(Paint.Style.FILL);
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = managedQuery(imageFileUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
if (orientation!=90) {
matrix.postRotate(orientation);
scaledBitmap = Bitmap.createScaledBitmap(alteredBitmap, width, height, false);
canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(bmp, matrix, paint);
choosenImage.setImageBitmap(scaledBitmap);
choosenImage.setOnTouchListener(this);
b.add(scaledBitmap);
}
在某些代码点,我用它来画一条直线:
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = event.getX();
downy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
upx = event.getX();
upy = event.getY();
canvas.drawLine(getScaledWidth(downx, width, choosenImage.getWidth()), downy / choosenImage.getHeight() * height, getScaledWidth(upx, width, choosenImage.getWidth()), upy / choosenImage.getHeight() * height, paint);
choosenImage.invalidate();
b.add(scaledBitmap);
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
现在我应该为撤消按钮上的 onclick 写什么?我已经尝试了所有方法,但不知道如何重置 canvas 以显示保存在数组列表中的位图之一...不知道我将 canvas 设置为位图的方式是否有问题或者如果我遗漏了什么。这是我最近的尝试:
scaledBitmap = b.get(b.size()-2);
b.remove(b.size() - 1);
choosenImage.invalidate();
我是 android 开发的新手,很抱歉,如果我不清楚,但真的非常感谢任何帮助我被困在这个项目中,它很快就会到期。
提前致谢!
位图列表 --> 内存不足!
位图保存在非常有限的内存区域中,并且出于目的而拥有完整的全屏位图列表会使您的应用程序很快耗尽内存。
路径列表 --> 更好
我为我的(非常旧的)应用程序“Fingercolors”所做的是保留用户绘制的所有路径的列表。路径由点列表和一些关于颜色、笔画宽度等的信息组成。
为了撤消步骤,我从一个空位图开始(屏幕外)并重新绘制列表中的所有路径除了最后一个,然后显示重建的位图。
这允许无限制地撤消和重做,即使对于具有很多路径的复杂图像,它也足够快。
对于我的应用程序,我正在创建一个图像编辑器,它目前工作正常,但我想实现取消撤消按钮,它基本上可以返回到图像的先前状态。我试图通过使用位图的 ArrayList 来做到这一点。起初我使用此代码从图库中打开图像并设置 canvas:
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
.getHeight(), bmp.getConfig());
int x = alteredBitmap.getWidth();
int y = alteredBitmap.getHeight();
if (x > y) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
lscape = true;
} else {
lscape = false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
paint.setStyle(Paint.Style.FILL);
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = managedQuery(imageFileUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
if (orientation!=90) {
matrix.postRotate(orientation);
scaledBitmap = Bitmap.createScaledBitmap(alteredBitmap, width, height, false);
canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(bmp, matrix, paint);
choosenImage.setImageBitmap(scaledBitmap);
choosenImage.setOnTouchListener(this);
b.add(scaledBitmap);
}
在某些代码点,我用它来画一条直线:
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = event.getX();
downy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
upx = event.getX();
upy = event.getY();
canvas.drawLine(getScaledWidth(downx, width, choosenImage.getWidth()), downy / choosenImage.getHeight() * height, getScaledWidth(upx, width, choosenImage.getWidth()), upy / choosenImage.getHeight() * height, paint);
choosenImage.invalidate();
b.add(scaledBitmap);
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
现在我应该为撤消按钮上的 onclick 写什么?我已经尝试了所有方法,但不知道如何重置 canvas 以显示保存在数组列表中的位图之一...不知道我将 canvas 设置为位图的方式是否有问题或者如果我遗漏了什么。这是我最近的尝试:
scaledBitmap = b.get(b.size()-2);
b.remove(b.size() - 1);
choosenImage.invalidate();
我是 android 开发的新手,很抱歉,如果我不清楚,但真的非常感谢任何帮助我被困在这个项目中,它很快就会到期。
提前致谢!
位图列表 --> 内存不足!
位图保存在非常有限的内存区域中,并且出于目的而拥有完整的全屏位图列表会使您的应用程序很快耗尽内存。
路径列表 --> 更好
我为我的(非常旧的)应用程序“Fingercolors”所做的是保留用户绘制的所有路径的列表。路径由点列表和一些关于颜色、笔画宽度等的信息组成。
为了撤消步骤,我从一个空位图开始(屏幕外)并重新绘制列表中的所有路径除了最后一个,然后显示重建的位图。
这允许无限制地撤消和重做,即使对于具有很多路径的复杂图像,它也足够快。