如何清除canvas android中的所有内容?

时间:2014-02-05 11:02:33

标签: android canvas

我正在开发canvas应用程序和Undo& amp;重做功能现在工作正常我想使用重置按钮重置画布。我尝试使用一些参考但无法找到适当的解决方案,这是我的代码

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    private ArrayList<Path> undonePaths = new ArrayList<Path>();
    private ArrayList<Path> paths = new ArrayList<Path>();
    FrameLayout frmLayout;
    Canvas canvas;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frmLayout=(FrameLayout)findViewById(R.id.frameLayout);     
        final DrawingPanel dp = new DrawingPanel(this);
        frmLayout.addView(dp);

                ((Button) findViewById(R.id.Clear))
        .setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Path path = new Path(); 
             Paint clearPaint = new Paint(); 
             clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
             canvas.drawRect(0, 0, 0, 0, clearPaint); 

            }
        });

        ((Button) findViewById(R.id.Undo))
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if (paths.size() > 0) {
                            undonePaths.add(paths
                                    .remove(paths.size() -1));
                            dp.invalidate();
                        }
                    }
                });


        ((Button) findViewById(R.id.Redo))
        .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 if (undonePaths.size()>0) {
                       paths.add(undonePaths.remove(undonePaths.size()-1));
                       dp.invalidate();
                   }
            }
        });
    }

    public class DrawingPanel extends View implements OnTouchListener {

        private Canvas mCanvas;
        private Path mPath;
        private Paint mPaint, circlePaint, outercirclePaint;

        // private ArrayList<Path> undonePaths = new ArrayList<Path>();
        private float xleft, xright, xtop, xbottom;

        public DrawingPanel(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);
            this.setOnTouchListener(this);
            circlePaint = new Paint();
            mPaint = new Paint();
            outercirclePaint = new Paint();
            outercirclePaint.setAntiAlias(false);
            circlePaint.setAntiAlias(false);
            mPaint.setAntiAlias(false);
            mPaint.setColor(0xFF000000);
            outercirclePaint.setColor(0x44FFF000);
            circlePaint.setColor(0xF57F35);
            outercirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
            circlePaint.setStyle(Paint.Style.FILL);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.MITER);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(20);
            outercirclePaint.setStrokeWidth(15);
            mCanvas = new Canvas();
            mPath = new Path();
            paths.add(mPath);
        }
        public void colorChanged(int color) {
            mPaint.setColor(color);
        }
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }

        @Override
        protected void onDraw(Canvas canvas) {

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

        }

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

        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 = new Path();
            paths.add(mPath);
        }

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if (x <= cx+circleRadius+5 && x>= cx-circleRadius-5) {
                // if (y<= cy+circleRadius+5 && cy>= cy-circleRadius-5){
                // paths.clear();
                // return true;
                // }
                // }
                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;
        }
    }
}

现在,我想重置画布。有人可以帮助我吗?

错误

by using this code got error         02-05 11:50:03.169: E/AndroidRuntime(7542): FATAL EXCEPTION: main
02-05 11:50:03.169: E/AndroidRuntime(7542): java.lang.NullPointerException
02-05 11:50:03.169: E/AndroidRuntime(7542):     at com.example.undoredo.MainActivity$1.onClick(MainActivity.java:52)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at android.view.View.performClick(View.java:4204)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at android.view.View$PerformClick.run(View.java:17355)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at android.os.Handler.handleCallback(Handler.java:725)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at android.os.Looper.loop(Looper.java:137)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at java.lang.reflect.Method.invokeNative(Native Method)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at java.lang.reflect.Method.invoke(Method.java:511)
02-05 11:50:03.169: E/AndroidRuntime(7542):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

2 个答案:

答案 0 :(得分:0)

 Path path = new Path(); 
 Paint clearPaint = new Paint(); 
 clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
 canvas.drawRect(0, 0, 0, 0, clearPaint); 

答案 1 :(得分:0)

根据文档尝试调用canvas.restore()

<强>恢复

  

此调用会平衡之前对save()的调用,并用于删除   自上次保存调用以来对矩阵/剪辑状态的所有修改。   调用restore()比调用save()的次数多出错。

保存

  

此调用会平衡之前对save()的调用,并用于删除   自上次保存调用以来对矩阵/剪辑状态的所有修改。   调用restore()比调用save()的次数多出错。

所以你最初可以调用canvas.save(),然后在清除时只调用restore。