1

我有一个我创建的绘图类有一些性能问题。我想这与我处理绘图动作和撤消/重做功能的方式有关。任何人都可以就如何提高性能提供一些建议吗?

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

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

4

4 回答 4

2

使用以下内容作为参考,并根据您的要求修改以下内容。

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

在此处输入图像描述

于 2013-06-06T14:25:52.547 回答
1

正如你们所建议的那样,我将所有的 var 初始化都从 onDraw 中提取出来,并将它们放入构造函数中。

当用户撤消或重做他们绘制的东西时,需要这个特定的部分来清除画布:

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

所以我创建了一个新方法,我只在撤消/重做时调用它:

public void clearCanvasCache() {

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

现在很好用。。

全班:

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;

    Paint tile;

    Bitmap tileImage;

    BitmapShader shader;

    public KNDrawingSurfaceView(Context c, float width, float height, KNSketchBookActivity parent) {

        super(c);

        myWidth = width;
        myHeight = height;

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

        _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);
        tile = new Paint();

        tileImage = BitmapFactory.decodeResource(getResources(), R.drawable.checkerpattern);
        shader = new BitmapShader(tileImage, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        tile.setShader(shader);

        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) {

        Log.v("onDraw:", "curent paths size:" + paths.size());
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        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));
            clearCanvasCache();
            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));
            clearCanvasCache();
            invalidate();
        } else {

        }
        _parent.checkButtonStates();
    }

    public void onClickClear() {

        paths.clear();
        undonePaths.clear();
        clearCanvasCache();
        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) {

        if (!_parent.isDrawerOpen()) {
            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;
        } else {
            return false;
        }
    }

    public void clearCanvasCache() {

        mBitmap = Bitmap.createBitmap((int) myWidth, (int) myHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }
}
于 2013-06-06T17:11:37.770 回答
1

您正在 onDraw() 方法中重新创建 2 个位图、paint 和 BitmapShader。这会导致您的性能问题。试试这个: - 将对象创建移动到构造函数。- 我认为你可以完全删除这部分:

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

(如果您需要从画布中获取位图,请为此创建单独的方法并在需要时调用它)

于 2013-06-06T14:11:22.903 回答
1

不要在“循环”上创建或实例化任何变量,在 onDraw 之前和之外执行此操作并重用相同的变量。这肯定会提高性能!

于 2013-06-06T16:32:11.570 回答