如何清除所有画布android?

时间:2014-05-05 09:01:14

标签: android canvas android-canvas

我在画布相关的应用程序中工作,除了清除所有功能外,一切正常。

我已经使用此代码创建画布,它也有擦除操作但手动擦除。

public class CharactersCanvas extends Activity {
    public int width;
    public int height;
    public Arrays paths1;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;
    Context context;
    private Paint circlePaint;
    private Path circlePath;    
    static List<Integer> listFlag;
    /* MyView mv; */
    DrawingPanel dp;
    AlertDialog dialog;
    private ArrayList<Path> undonePaths = new ArrayList<Path>();
    public ArrayList<Path> paths = new ArrayList<Path>();
    private ArrayList<String> step = new ArrayList<String>();
    FrameLayout frmLayout;
    Canvas canvas;
    protected static ImageView imageView;
    private Integer frameIndex = 0;
    public int res;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.canvas_screen);
        dp = new DrawingPanel(this);

        frmLayout = (FrameLayout) findViewById(R.id.frameLayout1);
        imageView=(ImageView) findViewById(R.id.imageView5);
        frmLayout.addView(dp);

        Bundle bundle = this.getIntent().getExtras();
         res = bundle.getInt("resourseInt"); 
        imageView.setImageResource(res);

            frameIndex = (bundle != null && bundle.getInt("image") != 0) ? HomeScreen.characters.indexOf(bundle.getInt("image")) : frameIndex;

            System.out.println(" Blank " + frameIndex);


            frmLayout.setBackgroundResource((int)HomeScreen.characters.get(frameIndex));

        /*
         * if(){ dp.setBackgroundResource(R.drawable.atemplate); }
         */

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

                    @Override
                    public void onClick(View v) {
                        paths = new ArrayList<Path>();
                        if (paths != null)
                            paths.clear();
                        if(dp !=null)
                            dp.invalidate();


                    }
                });

        ((Button) findViewById(R.id.next_buttonn))
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        paths = new ArrayList<Path>();
                        if (frameIndex < (HomeScreen.characters.size() - 1)) {
                            if (paths != null)
                                paths.clear();
                            if (dp != null)
                                dp.invalidate();
                            System.out.println(" Size is "
                                    + HomeScreen.characters.size());
                            frmLayout
                                    .setBackgroundResource((int) HomeScreen.characters
                                            .get(frameIndex != HomeScreen.characters
                                                    .size() ? ++frameIndex
                                                    : frameIndex));
                        }

                    }

                });

        ((Button) findViewById(R.id.previous_buttonn))
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        if (frameIndex >= 0) {

                            paths = new ArrayList<Path>();

                            if (paths != null)
                                paths.clear();
                            if (dp != null)
                                dp.invalidate();

                            frmLayout
                                    .setBackgroundResource((int) HomeScreen.characters
                                            .get(frameIndex == 0 ? frameIndex
                                                    : --frameIndex));

                        }

                    }

                });

        ((Button) findViewById(R.id.letters_buttonn))
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        MediaPlayer mPlayer = MediaPlayer.create(
                                CharactersCanvas.this, R.raw.one_sound);

                        mPlayer.start();

                        Intent intent = new Intent(getApplicationContext(),
                                CharactersGridView.class);
                        intent.putExtra("resourseInt", res); 
                         // integerList is
                                                        // ArrayList<Integer>
                        startActivity(intent);

                        /*
                         * startActivity(new Intent(CharactersCanvas.this,
                         * CharactersGridView.class));
                         */
                    }
                });

        ((Button) findViewById(R.id.home_buttonn))
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        MediaPlayer mPlayer = MediaPlayer.create(
                                CharactersCanvas.this, R.raw.one_sound);

                        mPlayer.start();

                        startActivity(new Intent(CharactersCanvas.this,
                                HomeScreen.class));
                        finish();
                    }
                });




    }

    private Paint mPaint;
    private MaskFilter mEmboss;
    private MaskFilter mBlur;



    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(0xFFFF0000);
            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(10);
            outercirclePaint.setStrokeWidth(10);
            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) {
          if (paths != null && paths.size() > 0) {
            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;
        }
    }

}

我想用一个按钮清除所有的抄写。如何做到这一点?任何想法都会非常有用。

1 个答案:

答案 0 :(得分:1)

清除画布的内容,你可以画一种颜色,

canvas.drawColor(Color.TRANSPARENT)
例如