android - Canvas 的性能问题

标签 android android-canvas

我创建的绘图类存在一些性能问题。我想这与我处理绘图操作和撤消/重做功能的方式有关。谁能提供一些关于如何提高性能的建议?

public class KNDrawingSurfaceView extends View {

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;
    public Bitmap mBitmap;
    public Canvas mCanvas;
    public Path mPath;
    public Paint mBitmapPaint;   
    float myWidth;
    float myHeight;
    public Paint mPaint;
    public MaskFilter mEmboss;
    public MaskFilter mBlur;
    public ArrayList<Path> paths = new ArrayList<Path>();
    public ArrayList<Paint>paints = new ArrayList<Paint>();
    public ArrayList<Path> undonePaths = new ArrayList<Path>(); 
    public ArrayList<Paint>undonePaints = new ArrayList<Paint>();

    private KNSketchBookActivity _parent;

    public KNDrawingSurfaceView(Context c, float width,float height, KNSketchBookActivity parent) {
        super(c);
        myWidth = width;
        myHeight = height;
        _parent =parent;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

        mPath = new Path();

        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        mBitmap = Bitmap.createBitmap((int)myWidth, (int)myHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        Log.v("onDraw:", "curent paths size:"+paths.size());
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        Paint tile = new Paint();

        Bitmap tileImage =  BitmapFactory.decodeResource(getResources(),R.drawable.checkerpattern);
        BitmapShader shader = new BitmapShader(tileImage, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        tile.setShader(shader);
        canvas.drawRect(0, 0, myWidth, myHeight, tile);

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        for (Path p : paths){
            canvas.drawPath(p, mPaint);
        }
        canvas.drawPath(mPath,mPaint);
    }

    public void onClickUndo () { 

        if (paths.size()>0) 
        { 
           undonePaths.add(paths.remove(paths.size()-1));
           undonePaints.add(paints.remove(paints.size()-1));
           invalidate();
         }
        else
        {

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

        }
        _parent.checkButtonStates();
     }
    public void onClickClear (){
        paths.clear();
        undonePaths.clear();
        invalidate();
        _parent.checkButtonStates();
     }
    public void saveDrawing(){


        FileOutputStream outStream = null;
        String fileName = "tempTag";
        try {

            outStream = new FileOutputStream("/sdcard/" + fileName + ".png");

            mBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }


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

    private void touch_start(float x, float y) {
        undonePaths.clear();
        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);

        mCanvas.drawPath(mPath, mPaint);

        paths.add(mPath);
        paints.add(mPaint);
        _parent.checkButtonStates();
        mPath = new Path(); 
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if(x>myWidth){
            x=myWidth;

        }
        if(y>myHeight){
            y=myHeight;

        }
        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;
    }
}

请与我分享您在处理绘图/ Canvas 优化方面的任何经验或链接

最佳答案

引用以下内容,并根据您的要求修改以下内容。

你在 onDraw() 中有以下内容

 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
 // will clear the draw

每次可以 invalidate() 时都会调用 onDraw(canvas)。您的抽奖将被刷新。

我不知道你想做什么,但我删除了上面的内容

移动了里面的onSizeChanged

   mBitmap = Bitmap.createBitmap((int)myWidth, (int)myHeight, Bitmap.Config.ARGB_8888); 
   mCanvas = new Canvas(mBitmap); 

您的代码运行良好。在模拟器上测试过。

 public class MainActivity extends Activity {

DrawingView dv ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dv = new DrawingView(this);
    setContentView(dv); 
}

public class DrawingView extends View {

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;
    public Bitmap mBitmap,tileImage;
    public Canvas mCanvas;
    public Path mPath;
    public Paint mBitmapPaint;   
    float myWidth;
    float myHeight;
    Paint tile ;
    public Paint mPaint;
    public MaskFilter mEmboss;
    public MaskFilter mBlur;
    public ArrayList<Path> paths = new ArrayList<Path>();
    public ArrayList<Paint>paints = new ArrayList<Paint>();
    public ArrayList<Path> undonePaths = new ArrayList<Path>(); 
    public ArrayList<Paint>undonePaints = new ArrayList<Paint>();
    BitmapShader shader;

    public DrawingView(Context c) {
        super(c);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.RED);
        tile = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        tileImage =  BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        shader = new BitmapShader(tileImage, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 
        tile.setShader(shader);
    }

    @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);    
        myWidth =w;
        myHeight = h;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        Log.v("onDraw:", "curent paths size:"+paths.size());
        for (Path p : paths){
            canvas.drawPath(p, mPaint);
        }
        canvas.drawPath(mPath,mPaint);
    }

    public void onClickUndo () { 

        if (paths.size()>0) 
        { 
           undonePaths.add(paths.remove(paths.size()-1));
           undonePaints.add(paints.remove(paints.size()-1));
           invalidate();
         }
        else
        {

        }

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

        }

     }
    public void onClickClear (){
        paths.clear();
        undonePaths.clear();
        invalidate();

     }
    public void saveDrawing(){


        FileOutputStream outStream = null;
        String fileName = "tempTag";
        try {

            outStream = new FileOutputStream("/sdcard/" + fileName + ".png");

            mBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }


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

    private void touch_start(float x, float y) {
        undonePaths.clear();
        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);

        mCanvas.drawPath(mPath, mPaint);

        paths.add(mPath);
        paints.add(mPaint);

        mPath = new Path(); 
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if(x>myWidth){
            x=myWidth;

        }
        if(y>myHeight){
            y=myHeight;

        }
        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;
    }
}

enter image description here

关于android - Canvas 的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16963801/

相关文章:

android - 移动应用程序的聊天集成

java - 如何调用包含在 Android Activity 中的方法?

android - 如何访问 Android 设备上的可移动存储?

java - 标记未出现在圆心

android - 如何像 Google Apps 一样在谷歌地图中同时显示 MarkerIcon 和标题?

java - 将 9-patch drawable 应用到 Button,保持默认填充

java - 我想从下一个 Activity 传递复选框值,但它抛出 NullPointerException

Android 在 Canvas 中拖放/旋转位图

android - 填充完整的 Canvas ,但保留边界填充区域,因为它像圆形、矩形

android - 如何在 SurfaceView 中调用自定义 View 的拖动事件?