自定义绘图视图橡皮擦

时间:2014-06-04 23:32:39

标签: android drawing erase

欢迎你们, 我有自定义绘图视图(指纹)的问题 我正在使用

drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

问题是它绘制黑线然后删除绘图。 如何禁用此黑线并将其设置为透明

我的代码是:

import android.annotation.SuppressLint;
import android.graphics.PorterDuff;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;

public class DrawingView extends View {
    // drawing path
    private Path drawPath;
    // drawing and canvas paint
    private Paint drawPaint, canvasPaint, drawCirclePaint;

    // initial color
    public int paintColor = Color.BLACK;
    // canvas
    private Canvas drawCanvas;
    // canvas bitmap
    private Bitmap canvasBitmap;

    private float brushSize;

    private boolean startedEditing = false;

    private int width, height;

    public boolean isLined = false;

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setupDrawing();
    }

    public boolean isStartedEditing() {
        return startedEditing;
    }

    public void setBackColor(int color) {
        this.setBackgroundColor(color);
    }

    public int getBackColor() {
        int colorID;
        try {
            ColorDrawable color = (ColorDrawable) this.getBackground();
            colorID = color.getColor();

        } catch (Exception e) {
            colorID = Color.WHITE;
        }
        return colorID;
    }

    public void setupDrawing() {

        brushSize = 4;
        drawPath = new Path();

        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        drawPaint.setStrokeWidth(brushSize);

        drawCirclePaint = new Paint();
        drawCirclePaint.setColor(paintColor);
        drawCirclePaint.setStyle(Paint.Style.FILL);
        drawCirclePaint.setStrokeJoin(Paint.Join.ROUND);
        drawCirclePaint.setStrokeCap(Paint.Cap.ROUND);
        drawCirclePaint.setStrokeWidth(brushSize / 4);

        canvasPaint = new Paint(Paint.DITHER_FLAG);

    }

    public void setBrushSize(float newSize) {
        float pixelAmount = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, newSize, getResources()
                        .getDisplayMetrics());
        brushSize = pixelAmount;
        drawPaint.setStrokeWidth(brushSize);
    }

    public void setBrushColor(int newColor) {
        drawPaint.setColor(newColor);

        drawCirclePaint.setColor(newColor);
    }

    public int getBrushColor() {

        return (drawPaint.getColor() == Color.WHITE) ? Color.BLACK : drawPaint
                .getColor();

    }

    @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);

        width = w;
        height = h;

        // setLinedBackground();
    }

    @SuppressLint("NewApi")
    public void setLinedBackground(boolean setOrRemove) {

        if (setOrRemove) {
            isLined = true;
            // Lined Background
            // Initialize lined background
            Bitmap linedBackground = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            Canvas linedBackgroundCanvas = new Canvas(linedBackground);
            // Set back white color
            linedBackgroundCanvas.drawColor(Color.WHITE);

            final float scale = DrawingView.this.getResources()
                    .getDisplayMetrics().density;
            int pixels = (int) (50 * scale + 0.5f);

            for (int i = 1, j = 0; j < drawCanvas.getHeight() / (pixels); i++, j++) {

                Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                myPaint.setStrokeWidth((int) (1 * scale + 0.5f));
                myPaint.setColor(Color.argb(60, 168, 168, 168));

                linedBackgroundCanvas.drawLine(5, i * pixels,
                        drawCanvas.getWidth() - 5, i * pixels, myPaint);

            }
            BitmapDrawable ob = new BitmapDrawable(
                    DrawingView.this.getResources(), linedBackground);
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                this.setBackgroundDrawable(ob);
            } else {
                this.setBackground(ob);
            }
        } else {
            isLined = false;
            // Lined Background
            // Initialize lined background
            setBackColor(Color.WHITE);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {

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

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        AddDrawingNote.moreLayout.setVisibility(View.GONE);
        startedEditing = true;

        float touchX = event.getX();
        float touchY = event.getY();
        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:

            drawCanvas.drawPoint(touchX, touchY, drawCirclePaint);

            drawPath.moveTo(touchX, touchY);
            break;

        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchX, touchY);
            break;

        case MotionEvent.ACTION_UP:
            drawCanvas.drawPath(drawPath, drawPaint);
            drawPath.reset();
            break;

        default:
            return false;
        }
        invalidate();
        return true;
    }

    public void setErase() {

        drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    }

}

和xml

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/drawingnote_main_layout_bg"
                android:orientation="vertical"
                android:weightSum="100" >

                <ImageView
                    android:id="@+id/drawingView_back"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_margin="1dp"
                    android:background="#FFFF00"/>

                <ibrahim.radwan.qnotes.DrawingView
                    android:id="@+id/add_drawing_note_drawing"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_margin="1dp"
                    android:background="#00ffffff" />
            </RelativeLayout>

任何想法:)

1 个答案:

答案 0 :(得分:0)

哟,我想出来了。只需将绘画颜色设置为视图的背景即可。