单击按钮未重置画布绘图

时间:2018-07-07 09:09:38

标签: android android-canvas

单击时不会重置“画布绘画”,单击清除按钮后在画布上再次绘图时会重置........................ ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... .................................... 这是我的代码

public class Practice extends FragmentActivity {
    private RelativeLayout relativeLayout;
    private Paint paint;
    ImageView back;
    private View view;
    private Path path2;
    private Bitmap bitmap;
    private Canvas canvas;
    private Button button;
    public int width, height;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);
        back = findViewById(R.id.back);
        relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout1);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        button = (Button) findViewById(R.id.button);

        view = new SketchSheetView(Practice.this);

        paint = new Paint();

        path2 = new Path();

        relativeLayout.addView(view, new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));

        paint.setDither(true);

        paint.setColor(Color.parseColor("#FFFFFF"));

        paint.setStyle(Paint.Style.STROKE);

        paint.setStrokeJoin(Paint.Join.MITER);

        paint.setStrokeCap(Paint.Cap.BUTT);

        paint.setStrokeWidth(5);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                path2.reset();
            }
        });

    }

    class SketchSheetView extends View {

        public SketchSheetView(Context context) {

            super(context);

            bitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444);

            canvas = new Canvas(bitmap);
            this.setBackground(getResources().getDrawable(R.drawable.ic_brush_black_24dp));
        }

        private ArrayList<DrawingClass> DrawingClassArrayList = new ArrayList<DrawingClass>();

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

            DrawingClass pathWithPaint = new DrawingClass();
            canvas.drawPath(path2, paint);

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                this.setBackground(null);
                vibrator.vibrate(500);
                path2.moveTo(event.getX(), event.getY());

                path2.lineTo(event.getX(), event.getY());
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                this.setBackground(null);
                path2.lineTo(event.getX(), event.getY());
                vibrator.vibrate(500);

                pathWithPaint.setPath(path2);

                pathWithPaint.setPaint(paint);

                DrawingClassArrayList.add(pathWithPaint);
            }

            invalidate();
            return true;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (DrawingClassArrayList.size() > 0) {

                canvas.drawPath(


                        DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPath(),

                        DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPaint()

                );
            }
        }
    }

    public class DrawingClass {

        Path DrawingClassPath;
        Paint DrawingClassPaint;

        public Path getPath() {
            return DrawingClassPath;
        }

        public void setPath(Path path) {
            this.DrawingClassPath = path;

        }


        public Paint getPaint() {
            return DrawingClassPaint;
        }

        public void setPaint(Paint paint) {
            this.DrawingClassPaint = paint;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

0 个答案:

没有答案