0

我想擦除我的位图,但我不想擦除背景图像。当我尝试擦除时,它是白色的,并且在帧中绘制非常困难。

这是来自CanvasView

erasePaint.setColor(Color.WHITE);
//erasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

erasePaint.setAntiAlias(true);
erasePaint.setStyle(Paint.Style.STROKE);
erasePaint.setStrokeJoin(Paint.Join.ROUND);
erasePaint.setStrokeWidth(12);

//....

protected void onDraw(Canvas canvas) {
    paint.setPathEffect(null);


    if(bitmap!=null){

        for(MyEraser e:eraserList){
            canvas.drawPath(e.p,erasePaint);
            invalidate();
        }

        final OnTouchListener eraseListener = new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                //  erasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                //FirstActivity.ll.setVisibility(LinearLayout.GONE);

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        myEraser = new MyEraser();
                        lastTouchX = event.getX();
                        lastTouchY = event.getY();
                        myEraser.mouseDown(event.getX(), event.getY());
                        return true;

                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_UP:
                        resetDirtyRect(event.getX(),event.getY());
                        int historySize = event.getHistorySize();
                        for(int i=0;i<historySize;i++){
                            float historicalX = event.getHistoricalX(i);
                            float historicalY = event.getHistoricalY(i);
                            expandDirtyRect(historicalX, historicalY);

                            myEraser.mouseUp(historicalX, historicalY);
                        }
                        myEraser.mouseUp(event.getX(), event.getY());
                        eraserList.add(myEraser);
                        break;

                    default:
                        Log.d("mock it up", "Unknown touch event  " + event.toString());
                        return false;
                }

                invalidate(
                    (int) (dirtyRect.left - HALF_STROKE_WIDTH),
                    (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                    (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                    (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

                lastTouchX = event.getX();
                lastTouchY = event.getY();
                return true;
            }
        };
    }
}

这是我的橡皮擦

public class MyEraser {

    Paint paint = new Paint();
    Path p = new Path();

    public MyEraser(){
        paint.setColor(Color.WHITE);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5);
    }

    public void mouseDown(float x, float y) {
        //path.addCircle(x,y,5,Path.Direction.CW);
        p.moveTo( x, y );
        // path.lineTo(x, y);
    }

    public void mouseMove(Path path, float x, float y) {
        // path.addCircle(x,y,5,Path.Direction.CW);
    }

    public void mouseUp(float x, float y){
        //path.addCircle(x,y,5,Path.Direction.CW);
        p.lineTo(x, y);
    }

    public void draw(Canvas c,Path path){
        //paint.setColor(Color.WHITE);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(7);
        c.drawPath(p, paint);
    }
}
4

1 回答 1

0

编辑:重新开始以获得更好的解释:)

我不熟悉Android,实际上我从未使用过它。使用此代码作为如何组织代码的示例,而不是功能代码。

class MyView extends View {
    private Eraser myEraser;
    private Bitmap myBackgroundImage;
    private Canvas myForegroundCanvas;

    public MyView(Context context, Attributes, attrs) {
        myEraser = new Eraser()
        myBackgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.your_background_name);
        Bitmap image = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); // the width and height of the view
        myForegroundCanvas = new Canvas(image);
    }

    public boolean onTouchEvent(MotionEvent event) {
        // update your eraser path
        return true;
    }

    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(myBackgroundImage, 0, 0, null);

        //for (Eraser item : eraserList) {
            // if you have multiple eraser, add them to a list
            myEraser.draw(myForegroundCanvas);
        //}

        canvas.drawCanvas(myForegroundCanvas, 0, 0, null);
    }
}

主要思想是保持背景和前景图像之间的分离。这样,您可以轻松更改背景并对其进行更新。您还可以在前台进行重置以擦除所有内容等。

我希望这可以帮助你。

于 2013-03-27T21:14:51.910 回答