猿问

清除画布并在其中再次绘制

我试图在我的应用程序中创建一个“全部清除”按钮,但它不起作用。这是我用来创建和使用画布的代码:


public DrawingView(Context context, AttributeSet attrs) {

        super(context, attrs);

        setLayerType(LAYER_TYPE_HARDWARE, null);

        startActivity = System.currentTimeMillis();

        setupDrawing();

    }


    public void setDimension (DisplayMetrics displaymetrics) {

        height = displaymetrics.heightPixels;

        width = displaymetrics.widthPixels;

        diameter = width;

        if (height < width){

            diameter = height;

        }

        offset = (int) (0.32*diameter);

        diameter -= offset;

        String imageName = protocol+draw;

    }


    //setup drawing

    private void setupDrawing(){


        //prepare for drawing and setup paint stroke properties

        drawPath = new Path();

        drawPaint = new Paint();

        drawPaint.setColor(paintColor);

        drawPaint.setAntiAlias(true);

        drawPaint.setStrokeWidth(5);

        drawPaint.setStyle(Paint.Style.STROKE);

        drawPaint.setStrokeJoin(Paint.Join.ROUND);

        drawPaint.setStrokeCap(Paint.Cap.ROUND);

        canvasPaint = new Paint(Paint.DITHER_FLAG);

    }


    //size assigned to view

    @Override

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        super.onSizeChanged(w, h, oldw, oldh);

        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

        drawCanvas = new Canvas(canvasBitmap);

    }


    //draw the view - will be called after touch event

    @Override

    protected void onDraw(Canvas canvas) {

        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);

        canvas.drawPath(drawPath, drawPaint);


        canvas.drawRect(width/2 - diameter/2 ,

                (70),

                width/2 + diameter/2,

                1100, drawPaint);

    }

这是我用来清除屏幕的代码,但是按下按钮没有任何显示:


public void restoreDraw () {

        drawPath = null;

        canvasBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888);

        drawPath = new Path();

    }

我应该清除位图画布,对吗?


HUWWW
浏览 96回答 1
1回答

陪伴而非守候

您必须添加requestLayout()内部restoreDraw()方法。public void restoreDraw () {&nbsp; &nbsp; drawPath = null;&nbsp; &nbsp; canvasBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888);&nbsp; &nbsp; drawPath = new Path();&nbsp; &nbsp; requestLayout();}
随时随地看视频慕课网APP

相关分类

Java
我要回答