应用一长串路径后,视图无响应

时间:2016-12-17 13:06:50

标签: android android-canvas android-view android-bitmap

我正在尝试使用视图中的撤消/重做操作绘制多个颜色的位图文件。这是我的撤消/重做和颜色/位图设置的设置,

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

private Map<Path, Integer> colorMap = new HashMap<>();
private Map<Path, Bitmap> colorFillMap = new HashMap<Path, Bitmap>();

绘制配置:

        drawPath = new Path();
        drawPaint = new Paint(Paint.DITHER_FLAG);
        drawPaint.setAntiAlias(true);
        drawPaint.setFilterBitmap(true);
        drawPaint.setDither(true);
        drawPaint.setColor(paintColor);
        drawPaint.setStrokeWidth(brushSize);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        drawPaint.setAlpha(100);

        setLayerType(View.LAYER_TYPE_SOFTWARE, drawPaint);

每当发生变化时,我只是将修改后的颜色/位图存储在路径列表中。这有助于恢复先前状态中使用的完全相同的颜色/位图。

private void touch_start(float x, float y) {
        undonePaths.clear();
        drawPath.reset();
        drawPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y, float x2, float y2) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            drawPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {

        // commit the path to our offscreen
        drawPath.lineTo(mX, mY);
        drawCanvas.drawPath(drawPath, drawPaint);

        // kill this so we don't double draw
        paths.add(drawPath);
        // the selectedColor is dynamically changeable
        colorMap.put(drawPath, selectedColor);
        drawPath = new Path();
        drawPath.reset();
    }

    public void onClickUndo() {
        if (paths.size() > 0) {
            undonePaths.add(paths.remove(paths.size() - 1));
            invalidate();
        }
    }

    public void onClickRedo() {
        if (undonePaths.size() > 0) {
            paths.add(undonePaths.remove(undonePaths.size() - 1));
            invalidate();
        }
    }

最后在 onDraw()方法内部,我根据存储的值绘制每个路径,

protected void onDraw(Canvas canvas) {
        canvas.save();

        try {
            for (Path p : paths) {

            if (colorFillMap.get(p) != null) {
                // the colorFillMap is dynamically changeable
                canvas.drawBitmap(colorFillMap.get(p), 0, 0, drawPaint);
            } else {
                    if (colorMap.get(p) != null) {
                        drawPaint.setColor(colorMap.get(p));
                    }
                        if (p != null) {
                            canvas.drawPath(p, drawPaint);

                            drawPaint.setColor(selectedColor);

                            canvas.drawPath(drawPath, drawPaint);
                        }
                }
            }
        }

        canvas.restore();
    }

然而,在应用几条路径后,视图变慢并且触摸响应性变得无响应。我不知道因为列表中的大量路径导致了这种情况。无论如何我可以加快这个过程吗?我做错了吗?

1 个答案:

答案 0 :(得分:0)

无论如何我可以加快这个过程吗?

您可以在位图上执行绘图,因此您只需要在onDraw()中绘制位图。例如:

private Canvas mCanvas;
private Bitmap mCanvasBitmap;
@Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mCanvasBitmap == null) {
            mCanvasBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mCanvasBitmap);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mCanvasBitmap == null) return;
        canvas.drawBitmap(mCanvasBitmap,0,0,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    ....

    mCanvas.draw(...);
    invalidate();   

    ....

    }

但是,您需要找到重做/撤消操作的新方法。