gpt4 book ai didi

Android 绘图 View 非常慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:38 25 4
gpt4 key购买 nike

我从一个询问如何在 Android 中绘制的问题的答案中得到这段代码,但是当我在我的应用程序中使用和测试它时,我发现它在绘制大东西或许多路径时效率不高.问题来自 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);

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 速度,但这种方法的问题是我无法使用撤消和重做功能。

我尝试了很多事情来使用第二种方法进行撤消和重做,但我做不到。所以我在这里要问的是三件事之一: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 再次被调用,它在上面绘制,但是那部分仍然需要工作,我希望有人能帮助那部分。

最佳答案

处理这种情况的方法是使用具有 View 大小的位图。在触摸事件中,绘制到位图的 Canvas 中。在 onDraw 中,只需将位图绘制到 0,0 处的 Canvas 中。对于撤消/重做,。您可以删除位图并重新绘制所有路径。它会花费更长的时间,但每次撤消/重做只会发生一次。如果用户通常执行一次撤消/重做。您可以通过仅退一步使用另一个位图来进行优化。

关于Android 绘图 View 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17011112/

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