我从一个问题的答案中获取了这段代码,该问题询问如何在Android上绘制。然而,在使用并测试此代码后,我发现在绘制大型物体或多个路径时效率不高。问题出在
以下是该类:
但我再次在互联网上搜索,以寻找更好的绘制方式,因此我找到了以下内容:
1. 添加以下内容到构造函数中:
这种方法是行得通的,它不会减慢视图,但这种方法的问题在于我不能有撤销和重做功能。我尝试了许多方法来使用第二种方法进行撤销和重做,但我无法做到。所以我在这里提出了以下三个请求之一: 1. 用第二种方法实现撤销和重做的方法 2. 另一种方法使其可以撤销和重做 3. 一个完整的新类,其中所有内容都已完成,如开源库等。
如果您能帮忙就请帮忙,谢谢!
编辑1: 好的,我将它限制到这个程度,然后我什么也做不了了,我已经努力尝试了8个小时。它工作正常,直到撤销(您可以撤销任意数量的路径),然后再次绘制时所有剩余路径都消失了,我不知道是什么原因导致它这样做。
所以,基本上我所做的是首先使用第一种方法(即在调用撤销之前),然后如果点击了撤销,则将
onDraw
方法内的代码上,因为每次调用invalidate()
方法时都会调用onDraw
方法,该方法包含一个循环,将所有paths
重新绘制到canvas
上。随着添加更多的路径,这样做会变得非常缓慢。以下是该类:
public class DrawingView extends View implements OnTouchListener {
private Canvas m_Canvas;
private Path m_Path;
private Paint m_Paint;
ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
public static boolean isEraserActive = false;
private int color = Color.BLACK;
private int stroke = 6;
public DrawingView(Context context, AttributeSet attr) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
setBackgroundColor(Color.WHITE);
this.setOnTouchListener(this);
onCanvasInitialization();
}
public void onCanvasInitialization() {
m_Paint = new Paint();
m_Paint.setAntiAlias(true);
m_Paint.setDither(true);
m_Paint.setColor(Color.parseColor("#000000"));
m_Paint.setStyle(Paint.Style.STROKE);
m_Paint.setStrokeJoin(Paint.Join.ROUND);
m_Paint.setStrokeCap(Paint.Cap.ROUND);
m_Paint.setStrokeWidth(2);
m_Canvas = new Canvas();
m_Path = new Path();
Paint newPaint = new Paint(m_Paint);
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
@Override
public void setBackground(Drawable background) {
mBackground = background;
super.setBackground(background);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
public boolean onTouch(View arg0, MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
for (Pair<Path, Paint> p : paths) {
canvas.drawPath(p.first, p.second);
}
}
private void touch_start(float x, float y) {
if (isEraserActive) {
m_Paint.setColor(Color.WHITE);
m_Paint.setStrokeWidth(50);
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
} else {
m_Paint.setColor(color);
m_Paint.setStrokeWidth(stroke);
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
m_Path.reset();
m_Path.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
m_Path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
m_Path.lineTo(mX, mY);
// commit the path to our offscreen
m_Canvas.drawPath(m_Path, m_Paint);
// kill this so we don't double draw
m_Path = new Path();
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
public void onClickUndo() {
if (!paths.isEmpty()) {//paths.size() > 0) {
undonePaths.add(paths.remove(paths.size() - 1));
undo = true;
invalidate();
}
}
public void onClickRedo() {
if (!undonePaths.isEmpty()){//undonePaths.size() > 0) {
paths.add(undonePaths.remove(undonePaths.size() - 1));
undo = true;
invalidate();
}
}}
但我再次在互联网上搜索,以寻找更好的绘制方式,因此我找到了以下内容:
1. 添加以下内容到构造函数中:
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
使用以下代码重写 onSizeChanged 方法:
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
m_Canvas = new Canvas(mBitmap);
}
将以下内容添加到onDraw方法中:
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
if (!paths.isEmpty())
canvas.drawPath(paths.get(paths.size() - 1).first, paths.get(paths.size() - 1).second);
}
这种方法是行得通的,它不会减慢视图,但这种方法的问题在于我不能有撤销和重做功能。我尝试了许多方法来使用第二种方法进行撤销和重做,但我无法做到。所以我在这里提出了以下三个请求之一: 1. 用第二种方法实现撤销和重做的方法 2. 另一种方法使其可以撤销和重做 3. 一个完整的新类,其中所有内容都已完成,如开源库等。
如果您能帮忙就请帮忙,谢谢!
编辑1: 好的,我将它限制到这个程度,然后我什么也做不了了,我已经努力尝试了8个小时。它工作正常,直到撤销(您可以撤销任意数量的路径),然后再次绘制时所有剩余路径都消失了,我不知道是什么原因导致它这样做。
@Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null)
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
if (!paths.isEmpty() && !undo)
canvas.drawPath(paths.get(paths.size() - 1).first, paths.get(paths.size() - 1).second);
if (undo) {
setBackground(mBackground);
for (Pair<Path, Paint> p : paths)
canvas.drawPath(p.first, p.second);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
m_Canvas = new Canvas(mBitmap);
undo = false;
}
}
所以,基本上我所做的是首先使用第一种方法(即在调用撤销之前),然后如果点击了撤销,则将
undo
设置为true
,并执行if (undo)
下的代码,实际上就是第一种方法(重新计算所有路径),然后我将重新计算所有路径的结果绘制到mBitmap
中,因此每当再次调用onDraw
时,它都会在其上绘制,但这部分仍需要进一步完善,希望有人可以帮助解决这个问题。