Android绘制平滑路径

时间:2018-06-03 18:49:12

标签: android drawing

我有需要改进的代码。

这是错的:它有点慢而且波涛汹涌,这意味着线条不平滑而且绘图有点延迟。

public void touchStarted(Point point) {
    if (null == drawingModePath) {
        drawingModePath = new Path();
    }
    drawingModePath.moveTo(point.x, point.y);
}


public void touchMoved(Point point) {
    drawingModePath.lineTo(point.x, point.y);

    Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
                .getDefaultDisplay().getWidth(), (int) getWindowManager()
                .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
    mainDrawingView.setImageBitmap(bitmap);

    // Path

    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.WHITE);

    canvas.drawPath(drawingModePath, paint);
}

public void touchEnded(Point point) {
        touchMoved(point);
}

实质上,此代码的作用是基于touchStarted,touchMoved和touchEnded绘制路径。如果有人可以帮助我优化这一点,我将不胜感激。也许如果每次touchMoved发生时我都不重新创建位图?这里不确定...不确定...我使用UIBezierPath在iOS上执行此代码,它更快(更顺畅)。无论如何,我来找你帮忙。投入赞赏。

1 个答案:

答案 0 :(得分:0)

你正在重新创造每一个动作。这将影响绘图的性能。事件每8ms触发一次(或16ms我不确定),想象你每8ms重新实例化一次?那太难了。

所以这必须在实例化部分

Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
            .getDefaultDisplay().getWidth(), (int) getWindowManager()
            .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
mainDrawingView.setImageBitmap(bitmap);


paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);

touchMoved()应仅记录新路径并调用invalidate()以使View重绘自身,从而调用draw方法(onDraw())。

public void touchMoved(Point point) {
    drawingModePath.lineTo(point.x, point.y);
    invalidate();
}

然后实施onDraw()方法进行绘图

下面我在我的一个项目中如何绘制绘图界面:

public class SignatureView extends View {

    public SignatureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // instantiating my paint object
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        path = new Path();
    }

    @Override
    protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
    {
        // this is where i initialize my canvas, because in constructor, the view is not completely instantiated yet, so getting the height and width there will result in null exception.
        bitmap = Bitmap.createBitmap(xNew, yNew, Bitmap.Config.ARGB_8888);
        background_canvas = new Canvas(bitmap);
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        // draw the new path to a buffer canvas 
        background_canvas.drawPath(path, paint);
        // put the buffer in the real canvas
        canvas.drawBitmap(bitmap, 0, 0, paint);
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {

        //this is like your move event, it just records the new path every move.
        int action = ev.getActionMasked();
        if ( action == MotionEvent.ACTION_DOWN )
        {
            path.moveTo(ev.getX(), ev.getY());
        }
        else if ( action == MotionEvent.ACTION_MOVE )
        {
            path.lineTo(ev.getX(), ev.getY());
            // call invalidate() to make the view redraw itself, resulting in calling the onDraw() method.
            invalidate();
        }
        else if ( action == MotionEvent.ACTION_UP )
        {
            onDone.method();
        }
        return true;
    }

    public void clear()
    {
        background_canvas.drawColor(Color.WHITE);
        path.reset();
        invalidate();
    }

    interface OnDone{
        void method();
    }

    public void setOnDone(OnDone new_onDone)
    {
        onDone = new_onDone;
    }

    OnDone onDone;
    private Paint paint;
    private Bitmap bitmap;
    private Canvas background_canvas;
    private Path path;
    public Bitmap getBitmap()
    {
        return bitmap;
    }
}