我从一个问题的答案中得到了这段代码,其中一个问题是问如何在Android中进行绘制,但是随后在使用它并在我的应用程序中对其进行测试时,我发现绘制大对象或多条路径时效率不高。问题来自onDraw
内部的代码,因为每次调用invalidate()
时都会调用onDraw
,其中包含一个循环,该循环将所有paths
再次绘制到canvas
,并通过向其添加更多路径,它变得非常慢。
这是类(class):
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);
2使用以下代码覆盖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);
}
3放在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);
}
这种方法有效,并且不会减慢 View 的速度,但是这种方法的问题是我无法具有撤消和重做功能。
我尝试了许多方法来使用第二种方法进行撤消和重做,但我做不到。因此,我要问的是以下三件事之一:
一种使用第二种方法撤消和重做的方法
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之前),然后如果单击undo,则
undo
设置为true
,并执行if (undo)
下的代码,这实际上是第一种方法(再次计算所有路径),然后我将重新计算所有路径的结果绘制到mBitmap
中,因此,每当再次调用onDraw
时,它都会在此之上绘制,但是该部分仍需要工作,希望有人可以提供帮助。 最佳答案
处理这种情况的方法是使用具有 View 大小的位图。在触摸事件上,绘制到位图的 Canvas 中。在onDraw中,只需将位图绘制到 Canvas 上的0,0。对于撤消/重做。您可以擦除位图并重新绘制所有路径。它需要更长的时间,但是每个撤消/重做仅发生一次。
如果用户通常执行一次撤消/重做操作。您可以通过仅退后一步使用另一个位图来进行优化。