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 に答える