gpt4 book ai didi

android - 撤消在android中自定义 ImageView 上的绘制

转载 作者:行者123 更新时间:2023-11-30 02:22:29 25 4
gpt4 key购买 nike

<分区>

我正在开发绘图应用程序,到目前为止我可以在图像上画线,问题是我发现它无法在自定义 ImageView 中集成撤消操作。

我应用了“一条一条地存储绘制路径,并在 onDraw 短语上绘制”的逻辑,但代码中似乎存在一些缺失/缺陷。

如果您想了解有关代码的更多详细信息,欢迎提出任何问题。屏幕截图是我的应用程序的样子(在图像上绘制)

感谢您的帮助。

主要 Activity 中:

// set image

bitmap = downScale(view.getTag().toString(),1280,1024);
altered_bitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());
draw_view.setNewImage(altered_bitmap,bitmap);

undo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if (pencil.getVisibility() == View.VISIBLE || pen.getVisibility() == View.VISIBLE) {
draw_view.onClickUndo();
}
}
});

自定义 ImageView 中:

private ArrayList<Path> paths = new ArrayList<Path>();
private Path mPath;

public ScaleImageView(Context context) {
super(context);
sharedConstructing(context);
}

public void sharedConstructing(Context context) {
super.setClickable(true);
this.context = context;

mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix = new Matrix();
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);

paint = new Paint();

paint.setAntiAlias(true);
paint.setStrokeWidth(width);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAlpha(alpha);


drawListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getDrawable() != null) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = getPointerCoords(event)[0];// event.getX();
downy = getPointerCoords(event)[1];// event.getY();
break;
case MotionEvent.ACTION_MOVE:
upx = getPointerCoords(event)[0];// event.getX();
upy = getPointerCoords(event)[1];// event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
mPath = new Path();
paths.add(mPath);
invalidate();
downx = upx;
downy = upy;
break;
case MotionEvent.ACTION_UP:
upx = getPointerCoords(event)[0];// event.getX();
upy = getPointerCoords(event)[1];// event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
mPath = new Path();
paths.add(mPath);
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
}
return true;
}
};

setOnTouchListener(drawListener);
}

//draw view start
public void setNewImage(Bitmap alteredBitmap, Bitmap bmp) {
canvas = new Canvas(alteredBitmap);
matrix_draw = new Matrix();
canvas.drawBitmap(bmp, matrix_draw, paint);
setImageBitmap(alteredBitmap);
mPath = new Path();
paths.add(mPath);
}

public void setBrushColor(int color) {
this.color = color;
paint.setColor(color);
paint.setAlpha(alpha);
}

public void setAlpha(int alpha) {
this.alpha = alpha;
paint.setAlpha(alpha);
}

public void setWidth(float width) {
this.width = width;
paint.setStrokeWidth(width);
}

final float[] getPointerCoords(MotionEvent e) {
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix matrix = new Matrix();
getImageMatrix().invert(matrix);
matrix.postTranslate(getScrollX(), getScrollY());
matrix.mapPoints(coords);
return coords;
}

public void setIsScale() {
isScale = !isScale;
setOnTouchListener(isScale ? zoomListener : drawListener);
}

@Override
protected void onDraw(Canvas canvas) {
for (Path p : paths){
canvas.drawPath(p, paint_line);
}
}

public void onClickUndo () {
if (paths.size()>0){
paths.remove(paths.size()-1);
invalidate();
}
}
//draw view end

enter image description here

更新:测试结果

经过一段时间的测试,发现应用程序可以运行,但所选图像不会像这样绘制在自定义 ImageView 中:

如果你有空闲时间,我已经上传了项目(<1 mb),它是一个小型绘图工具,首先将一个包含一些图像的文件夹复制到您设备中的“HistoryTool”文件夹中

路径,例如:

sd card root/ HistoryTool/ folder1 / a.jpg 

,然后你就可以在上面画画了,仅此而已,但是现在撤消操作不起作用,需要修复。

https://drive.google.com/file/d/0B9mELZtUJp0LLVh0b1Q0a3VTcG8/view?usp=sharing

非常感谢

enter image description here

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com