为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUWWW 2023-04-26 14:48:06
我试图在我的应用程序中创建一个“全部清除”按钮,但它不起作用。这是我用来创建和使用画布的代码: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();    }我应该清除位图画布,对吗?
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

您必须添加requestLayout()内部restoreDraw()方法。


public void restoreDraw () {

    drawPath = null;

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

    drawPath = new Path();

    requestLayout();

}


查看完整回答
反对 回复 2023-04-26
  • 1 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信