Android的撤消在画布上绘制方法 [英] Android undo drawing method on canvas

查看:116
本文介绍了Android的撤消在画布上绘制方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些code,需要一个画面和显示图片,那么用户能够画上了图片。

我想实现一个撤消方法。我根据我的code对很多例子我读过。现在的问题是在我的的onDraw 法 - 示例不使用 drawBitmap 但对我来说我要绘制位图为了在画布的图像上显示出来。

的code中所示显示图像,允许绘制图像上,但不会撤消附图。我想不出有什么不对/如何解决它。

 公共类的PhotoView扩展视图{    私人位图mBitmap;
    私人帆布mCanvas;
    私人路径的mpath;
    私人涂料mBitmapPaint;
    私人的ArrayList<路径和GT;路径=新的ArrayList<>();    公众的PhotoView(上下文C){
        超(C);
        mBitmap = mutableBitmap;
        的mpath =新路径();
        mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
        mCanvas =新的Canvas(mBitmap);
    }    @覆盖
    保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
        super.onSizeChanged(W,H,oldw,oldh);
    }    @覆盖
    保护无效的onDraw(帆布油画){
        //canvas.drawColor(0xFFAAAAAA);
        ****必须为了呼吁图像显示*****
        canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
        //canvas.drawPath(mPath,mPaint);
        对于(路径P:路径){
            canvas.drawPath(对,mPaint);
        }
        canvas.drawPath(的mpath,mPaint); //实时在画布上绘制
    }    私人浮动MX,我的;
    私有静态最终浮动TOUCH_TOLERANCE = 4;    私人无效touchStart(浮法X,浮法Y){
        mPath.reset();
        mPath.moveTo(X,Y);
        MX = X;
        我= Y;
    }    私人无效touchMove(浮法X,浮法Y){
        浮DX = Math.abs(X - MX);
        浮DY = Math.abs(Y - 我的);
        如果(DX> = || TOUCH_TOLERANCE DY> = TOUCH_TOLERANCE){
            mPath.quadTo(MX,MY,(X + MX)/ 2,(Y + MY)/ 2);
            MX = X;
            我= Y;
        }
    }    私人无效触(){
        mPath.lineTo(MX,我的);
        //提交的路径,我们的屏幕外
        mCanvas.drawPath(的mpath,mPaint);
        //杀死这个,所以我们不要双击平局
        paths.add(的mpath);
        的mpath =新路径();
        //mPath.reset();
        //paths.add(mPath);
    }    公共无效onClickUndo(){
        如果(paths.size()大于0)
        {
            paths.remove(paths.size() - 1);
            无效();
        }
    }
    公共位图方法GetPic(){
        mCanvas.save();
        返回mBitmap;
    }    @覆盖
    公共布尔onTouchEvent(MotionEvent事件){
        浮X = event.getX();
        浮Y = event.getY();
        开关(event.getAction()){
            案例MotionEvent.ACTION_DOWN:
                touchStart(X,Y​​);
                无效();
                打破;
            案例MotionEvent.ACTION_MOVE:
                touchMove(X,Y);
                无效();
                打破;
            案例MotionEvent.ACTION_UP:
                润色();
                无效();
                打破;
        }
        返回true;
    }
}


解决方案

因此​​,有你在做两件事情,当谈到路径用户抽奖。


  1. 在方法触() mCanvas.drawPath(的mpath,mPaint);

  2. 的onDraw() canvas.drawPath(P,mPaint);

当你调用 onClickUndo()的onDraw()的东西被撤消。但是,一个在触()没有撤销。这就是为什么你似乎撤消不工作。问题是行 mCanvas.drawPath(的mpath,mPaint);

解决方案
结果不要画的的mpath mCanvas 。当你这样做,你的 mBitmap 得到改变(你在绘制你的路径 mBitmap )。有没有办法撤消此。这是不是你想要的。如果你想在你的路径你的 mBitmap ,这样就可以将它保存在一个文件或者是这样,这次终于(也许有像保存()方法,并在该方法做到这一点)。

I created some code that takes a picture and displays the picture, then the user is able to draw on the picture.

I want to implement an undo method. I based my code on many examples I've read. The problem is in my onDraw method - the examples don't use drawBitmap but for me I have to draw the bitmap on the canvas in order for the image to show up.

The code shown displays the image, allows drawing on the image, but does not undo the drawings. I can't figure out what's wrong/how to fix it.

public class PhotoView extends View {

    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;
    private ArrayList<Path> paths = new ArrayList<>();

    public PhotoView(Context c) {
        super(c);
        mBitmap = mutableBitmap;
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //canvas.drawColor(0xFFAAAAAA);
        ****must call in order for image to show up *****
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        //canvas.drawPath(mPath, mPaint);
        for (Path p : paths){
            canvas.drawPath(p, mPaint);
        }
        canvas.drawPath(mPath, mPaint); //real time drawing on canvas
    }

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

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

    private void touchMove(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 touchUp() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        paths.add(mPath);
        mPath = new Path();
        //mPath.reset();
        //paths.add(mPath);
    }

    public void onClickUndo () {
        if (paths.size()>0)
        {
            paths.remove(paths.size()-1);
            invalidate();
        }
    }
    public Bitmap getPic() {
        mCanvas.save();
        return mBitmap;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchStart(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touchMove(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touchUp();
                invalidate();
                break;
        }
        return true;
    }
}

解决方案

So there are two things you are doing when it comes to Paths that the user draws.

  1. In the method touchUp() : mCanvas.drawPath(mPath, mPaint);
  2. In onDraw() : canvas.drawPath(p, mPaint);

When you call onClickUndo(), the onDraw() things gets undone. But the one in touchUp() is not undone. That's why your Undo doesn't seem to work. Problem is in line mCanvas.drawPath(mPath, mPaint);

Solution:
Do not draw the mPath on mCanvas. When you do this, your mBitmap gets changed (you are drawing your paths on the mBitmap). There is no way to undo this. And this is not what you want. If you want your paths in your mBitmap, so that you can save it in a file or so, do this finally (perhaps have a method like save() and do this in that method).

这篇关于Android的撤消在画布上绘制方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆