gpt4 book ai didi

绘画 Canvas 中的 Android 橡皮擦不会删除

转载 作者:太空狗 更新时间:2023-10-29 16:40:16 26 4
gpt4 key购买 nike

我有以下代码来激活/停用橡皮擦:

public PorterDuffXfermode clear = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);    
eraseB.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (!eraser) {
eraser = true;
eraseB.setImageResource(R.drawable.erase_on);
paint = new Paint(Paint.DITHER_FLAG);
paint.setColor(0x00000000);
paint.setAlpha(0x00);
paint.setXfermode(clear);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(stroke);
paintv.setPaint(paint);
} else {
eraser = false;
eraseB.setImageResource(R.drawable.erase);
paint = new Paint(Paint.DITHER_FLAG);
paint.setDither(true);
paint.setXfermode(null);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(stroke);
paintv.setPaint(paint);
}
}
});

setPaint 来 self 的自定义 View :

public void setPaint(Paint paint) {
this.paint = paint;
LogService.log("in setPaint", "paint = " + paint);
}

和我使用的 onDraw:

canvas.drawPath(mPath, paint);

如果我停用橡皮擦,它会画一条红线,但如果我激活橡皮擦,它会画一条黑线而不是删除。我该如何解决这个问题

最佳答案

Canvas 不支持橡皮擦,而 Bitmap 支持。

基本解决方法流程:

  1. 创建另一个 Canvas

  2. 创建位图

  3. 将该位图设置为该 Canvas

    public void init(int width, int height) {
    Log.i(TAG,"init with "+width+"x"+height);
    foreground = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    cacheCanvas = new Canvas();
    cacheCanvas.setBitmap(foreground);
    }
  4. 记录该位图上的触摸,包括绘画和橡皮擦

    public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    currentStroke = new Stroke();
    currentStroke.color = paint;
    currentStroke.path.moveTo(eventX, eventY);
    currentStroke.path.lineTo(eventX, eventY);

    synchronized (strokes) {
    strokes.add(currentStroke);
    }
    lastTouchX = eventX;
    lastTouchY = eventY;
    // There is no end point yet, so don't waste cycles invalidating.
    return true;

    case MotionEvent.ACTION_MOVE:
    case MotionEvent.ACTION_UP:
    // Start tracking the dirty region.
    resetDirtyRect(eventX, eventY);

    // When the hardware tracks events faster than they are delivered,
    // the
    // event will contain a history of those skipped points.
    int historySize = event.getHistorySize();
    for (int i = 0; i < historySize; i++) {
    float historicalX = event.getHistoricalX(i);
    float historicalY = event.getHistoricalY(i);
    expandDirtyRect(historicalX, historicalY);
    if (i == 0) {
    lastX = historicalX;
    lastY = historicalY;
    currentStroke.path.lineTo(historicalX, historicalY);
    } else {
    currentStroke.path.quadTo(lastX, lastY,
    (historicalX + lastX) / 2,
    (historicalY + lastY) / 2);
    }
    }

    // After replaying history, connect the line to the touch point.
    if(historySize==0){
    long duration=event.getEventTime()-event.getDownTime();
    float offset=0.1f;
    if(duration<300){
    offset=50.0f/duration;
    }
    currentStroke.path.lineTo(eventX+offset, eventY+offset);
    }else{
    currentStroke.path.lineTo(eventX, eventY);
    }
    synchronized (strokes) {
    strokes.add(currentStroke);
    }

    break;

    default:
    return false;
    }

    // Include half the stroke width to avoid clipping.
    float width = paint.getStrokeWidth() / 2;
    invalidate((int) (dirtyRect.left - width),
    (int) (dirtyRect.top - width), (int) (dirtyRect.right + width),
    (int) (dirtyRect.bottom + width));

    lastTouchX = eventX;
    lastTouchY = eventY;

    return true;
    }
  5. 在 View 的 Canvas 上绘制该位图

    protected void onDraw(Canvas canvas) {
    synchronized (strokes) {
    if (strokes.size() > 0) {
    for (Stroke s : strokes) {
    cacheCanvas.drawPath(s.path, s.color);
    }
    canvas.drawBitmap(foreground, 0, 0, null);
    strokes.clear();
    }
    }
    }

关于绘画 Canvas 中的 Android 橡皮擦不会删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18915003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com