删除画布位图以使背景可见

时间:2016-07-12 10:23:36

标签: android canvas bitmap android-canvas

我正在实现一个代码,其中我想要一个前景和一个背景图像,我想要删除前景图像,如果我犯了一个错误,我就可以撤消它。

用于背景我正在使用图像视图,前景是画布上的位图

我已经工作了这么远, 它正在擦除前景(即画布)但撤消重做不起作用

代码是:

public class MainActivity extends AppCompatActivity {

private Bitmap DrawBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint DrawBitmapPaint;
RelativeLayout Rl;
CustomView View;

DrawView drawView;
private Button undo, redo;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.loadActivity();
}

private Paint mPaint;


public class CustomView extends View {

    public CustomView(Context c) {

        super(c);

        create_image();


        setLayerType(android.view.View.LAYER_TYPE_SOFTWARE, mPaint);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // mCanvas.drawColor(Color.BLUE);

        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        for (Path p : paths){
            canvas.drawPath(p, mPaint);
        }
        setDrawingCacheEnabled(true);
        canvas.drawBitmap(DrawBitmap, 0, 0, DrawBitmapPaint);
        canvas.drawPath(mPath, mPaint);
        canvas.drawRect(mY, 0, mY, 0, DrawBitmapPaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        mCanvas.drawPath(mPath, mPaint);
        mPath = new Path();
        paths.add(mPath);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
               // performClick();
                invalidate();
                break;
        }
        return true;
    }

    public void clear() {
        create_image();

        // Added later..
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        // mCanvas.drawColor(Color.BLUE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(40);
        this.invalidate();
    }

    public void onClickUndo() {
        if (paths.size() > 0) {
            undonePaths.add(paths.remove(paths.size() - 1));
            invalidate();
        } else {

        }
        //toast the user
    }

    public void onClickRedo() {
        if (undonePaths.size() > 0) {
            paths.add(undonePaths.remove(undonePaths.size() - 1));
            invalidate();
        } else {

        }
        //toast the user
    }


}

public void loadActivity() {

    undo = (Button) findViewById(R.id.button1);
    redo = (Button) findViewById(R.id.button2);

    drawView = new DrawView(this);

    View = new CustomView(this);
    Rl = (RelativeLayout) findViewById(R.id.linearLayout2);
    Rl.addView(View);

    Bitmap bitmap = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.sample);
    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);


    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    // mCanvas.drawColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(40);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    mPaint.setStrokeWidth(40);

    mPaint.setColor(Color.BLUE);
    mPaint.setStrokeWidth(40);



    undo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View.onClickUndo();
        }
    });

    redo.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            View.onClickRedo();
        }
    });

}

public void create_image() {

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenWidth = displaymetrics.widthPixels;
    int screenHeight = displaymetrics.heightPixels;
    DrawBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
            Bitmap.Config.ARGB_4444);

    mCanvas = new Canvas(DrawBitmap);
    Bitmap bitmap = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.sample);
    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    mCanvas.drawBitmap(bitmap, 0, 0, null);

    mPath = new Path();

    DrawBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paths.add(mPath);

}
}

我的布局是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.approduction.drawing.MainActivity">

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Undo" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Redo" />

</LinearLayout>


<RelativeLayout
    android:id="@+id/linearLayout2"
    android:layout_below="@id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"/>
</RelativeLayout>

任何建议都很有价值 提前谢谢..

1 个答案:

答案 0 :(得分:0)

问题是你在这里有2个画布:canvasmCanvas
您的撤消/重做仅适用于canvas而不适用于mCanvas。这就是你的问题。您需要删除mCanvas中的onTouchUp()或更改其中的代码以实现撤消/重做功能。

请在此处参阅我的回答以获取更多信息:https://stackoverflow.com/a/38220061/4747587