我从一个询问如何在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); …
Run Code Online (Sandbox Code Playgroud)