Canvas的性能问题

时间:2016-06-14 07:47:42

标签: java android canvas

我想使用Canvas绘制位图。
我在手机上进行了测试,看起来非常慢,感觉就像我画得快,它没有注册整个机芯而只是部分机芯。 由于我想要它非常详细,我需要使用位图 我怎样才能提高我的表现?

        public class DrawView extends View {
        public Bitmap  mBitmap;
        public Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        private Paint   mPaint;


        public DrawView(Context c, AttributeSet attrs) {
            super(c, attrs);

            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);

            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xFF000000);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(9);

        }


        @Override
        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_8888);
            mCanvas = new Canvas(mBitmap);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

            canvas.drawPath(mPath, mPaint);


        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) {
            mPath.reset();
            mPath.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) {
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }
        }
        private void touch_up() {
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        }
    }

1 个答案:

答案 0 :(得分:0)

如果触摸事件变得更快,那么onDraw,那么你不需要在每次有新点时绘制,你应该在onDraw方法实际要求时绘制。 虽然,skia(基于Android的画布)并不是那么快就能画出这么多的四边形。您可以绘制简单的一批线,但将这些线形成为centripethal Catmull-Rom样条线或某些样条线,这对您来说更好。

而且,Path可以存储不那么多行