如何不失previous色彩在画布上执行撤消重做 [英] how perform undo redo on canvas without losing previous color

查看:172
本文介绍了如何不失previous色彩在画布上执行撤消重做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是这code执行撤销/重做操作的画布。上午时使用数组列表中删除现在的位置那么它不能正常工作,并从颜色选择器改变油漆的颜色previous绘制颜色一直也随之变化过程中。
我不理解是如何执行撤销/重做这一点。
所以plz帮助

//这是主类

 保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(MVIEW =新MyView的(本));mPaint =新的油漆();
mPaint.setAntiAlias​​(真);
mPaint.setDither(真);
mPaint.setColor(0xFFFF0000地址);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap​​(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);mEmboss =新EmbossMaskFilter(新浮法[] {1,1,1},
                               0.4f,6,3.5F);运动模糊=新BlurMaskFilter(8 BlurMaskFilter.Blur.NORMAL);

}

//而MyView的类

 公共类MyView的扩展视图{私有静态最终浮动MINP = 0.25f;
私有静态最终浮动MAXP = 0.75f​​;
私人位图mBitmap;
私人帆布mCanvas;
私人路径的mpath;
私人涂料mBitmapPaint;
ArrayList的<路径和GT;路径=新的ArrayList<路径>();
ArrayList的<路径和GT; undonePaths =新的ArrayList<路径>();
公共MyView的(语境C)
{
超(C);mBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
mCanvas =新的Canvas(mBitmap);
的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
// mCanvas.setBackgroundResource(0xFFFFFFFF的);
mCanvas.drawColor(0xFFFFFFFF的);
paths.add(的mpath);
}
公共MyView的(语境C中,int颜色)
   {
   超(C);     mBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
     mCanvas =新的Canvas(mBitmap);
     的mpath =新路径();
     mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
     mCanvas.drawColor(颜色);
     paths.add(的mpath);
  }
@覆盖保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh)
 {
     super.onSizeChanged(W,H,oldw,oldh);
 } @覆盖保护无效的onDraw(帆布油画)
  {
     canvas.drawColor(0xFF000000);     canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
     对于(路径P:路径)
      {
     canvas.drawPath(对,mPaint);
      }
    }    私人浮动MX,我的;
    私有静态最终浮动TOUCH_TOLERANCE = 4;    私人无效touch_start(浮法X,浮法Y){
    mPath.reset();
    mPath.moveTo(X,Y);
    MX = X;
    我= Y;
   }   私人无效TOUCH_MOVE(浮法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;
      }
   }   私人无效touch_up(){
       mPath.lineTo(MX,我的);
       mCanvas.drawPath(的mpath,mPaint);
       paths.add(的mpath);
       mPath.reset();
     }  @覆盖公共布尔onTouchEvent(MotionEvent事件){
       浮X = event.getX();
       浮Y = event.getY();     开关(event.getAction()){
         案例MotionEvent.ACTION_DOWN:
            touch_start(X,Y​​);
            无效();
            打破;
        案例MotionEvent.ACTION_MOVE:
            TOUCH_MOVE(X,Y);
            无效();
            打破;
        案例MotionEvent.ACTION_UP:
            润色();
            无效();
            打破;
       }
       返回true;
     }    公共无效清除()
     {
         涂料P =新的油漆();
         p.setAntiAlias​​(真);
         p.setDither(真);
         p.setColor(0xFFFFFFFF的);
         mCanvas.drawPaint(P);
      }   公共无效的drawImage(INT RESOURCEID)
    {
         位图_scratch = BitmapFactory.de codeResource(getResources(),RESOURCEID);
         mCanvas.drawBitmap(_scratch,0,0,NULL);
    }
    公共无效撤消(){
        // TODO自动生成方法存根
        mCanvas.drawCircle(POSX,波西,100,mPaint);
        Toast.makeText(getApplicationContext(),你好,1000).show();
        如果(paths.size()大于0)
        {
           undonePaths.add(paths.remove(paths.size() - 1));
           mView.invalidate();
           Toast.makeText(getApplicationContext(),撤消,1000).show();
         }
        其他
        {
            Toast.makeText(getApplicationContext(),不是,1000).show();
        }
    }
          公共无效重做(){
        // TODO自动生成方法存根
        如果(undonePaths.size()大于0)
        {
            paths.add(undonePaths.remove(undonePaths.size() - 1));
            无效();
        }
        其他
        {        }    }
   公共无效saveAsJpg(文件f)
     {
        串FNAME = f.getAbsolutePath();
        FOS的FileOutputStream = NULL;
        尝试{
        FOS =新的FileOutputStream(F);
        mBitmap.com preSS(比较pressFormat.JPEG,95,FOS);
       }赶上(Throwable的前){
      ex.printStackTrace();
       }
     } //结束saveAsJpg   } //结束MyView的   } //结束类FingerPaint


解决方案

 这个工作code。我测试它在我自己的应用程序,这是工作非常好。
可能是它帮助它u.Please评论。
            公共类主要活动扩展实现OnColorChangedListener {
            //公共静态INT selectedColor = Color.BLACK;
            公共静态的ArrayList<路径和GT; mMaindialog;
            //私人的ArrayList<路径和GT; undonePaths;
            //公众诠释selectedcolor;
            私有静态最后弦乐COLOR_ preFERENCE_KEY =颜色;
            私人的FrameLayout RelativeLayout的;
            静态字符串sdpath,位置;
            布尔我;
            //实例变量
            私人位图mBitmap = NULL;
            位图位图;
            私人油漆mPaint,m​​BitmapPaint,m​​Paint1;
            私人MyView的MView的;
            ImageView的IDD;
            //私人路径的mpath;
            INT slll = Color.BLACK;
            位图贴图= ListView5.bMap;
            私人按钮ClearPaint,Colorpaint;
            Ghostdatabase GOST;            @覆盖
            公共无效的onCreate(捆绑savedInstanceState){
                super.onCreate(savedInstanceState);
                的setContentView(R.layout.main);
                 IDD =(ImageView的)findViewById(R.id.imageView1);
                RelativeLayout的=(的FrameLayout)findViewById(R.id.frameLayout);                DisplayMetrics指标= getBaseContext()。getResources()
                        .getDisplayMetrics();
                INT W = metrics.widthPixels;
                INT H = metrics.heightPixels;                的System.out.println(宽度+ W);
                的System.out.println(高度+ H);                MVIEW =新的MyView的(这一点,W,H);
                mView.setDrawingCacheEnabled(真);                mPaint =新的油漆();
                mPaint.setAntiAlias​​(真);
                mPaint.setDither(真);
                mPaint.setColor(Color.BLACK);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeJoin(Paint.Join.ROUND);
                mPaint.setStrokeCap​​(Paint.Cap.ROUND);
                mPaint.setStrokeWidth(4);                ClearPaint =(按钮)findViewById(R.id.ne);
                ClearPaint.setOnClickListener(新OnClickListener(){                    公共无效的onClick(视图v){
                        // mBitmap.eraseColor(Color.TRANSPARENT);
                        // mPath.reset();
                        // mView.invalidate();                        mView.onClickUndo();                    }
                });
                按钮save22 =(按钮)findViewById(R.id.save);
                save22.setOnClickListener(新OnClickListener(){                    @覆盖
                    公共无效的onClick(视图v){
                        // TODO自动生成方法存根
                        文件cacheDir;
                        Toast.makeText(Main.this,照片,500).show();
                        位图图标;
                        relativelayout.setDrawingCacheEnabled(真);                        图标= Bitmap.createBitmap(relativelayout.getDrawingCache());
                        位图位图=图标;
                        relativelayout.setDrawingCacheEnabled(假);
                        //文件mFile1 = Environment.getExternalStorageDirectory();
                        日期D =新的日期();
                        字符串文件名= d.getTime()+mg1.jpg                        文件storagePath =(Environment.getExternalStorageDirectory());
                        文件DEST =新的文件(storagePath +/ CityAppImages);                        如果(!dest.exists()){
                            dest.mkdirs();                        }                        文件mFile2 =新的文件(DEST,文件名);
                        sdpath = mFile2.getAbsolutePath();                        Log.d(qqqqqqqqqqqqqqqqqqqqqqq,zzzzzzzz+ sdpath);
                        尝试{
                            FileOutputStream中outStream;                            outStream =新的FileOutputStream(mFile2);                            bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,outStream);                            outStream.flush();                            outStream.close();
                            Toast.makeText(Main.this,照片保存成功地,500)
                                    。显示();
                        }赶上(FileNotFoundException异常五){
                            // TODO自动生成catch块
                            e.printStackTrace();
                        }赶上(IOException异常五){                            // TODO自动生成catch块
                            e.printStackTrace();
                            Toast.makeText(Main.this,照片没有保存成功地
                                    500).show();
                        }                        GOST =新Ghostdatabase(Main.this);
                        gost.open();                        gost.insertTitle(sdpath);
                    }
                });                按钮来查看=(按钮)findViewById(R.id.listtt);
                view.setOnClickListener(新OnClickListener(){                    公共无效的onClick(视图v){                        意图II =新意图(Main.this,ListView5.class);
                        startActivity(二);                    }
                });                按钮Colorpaint =(按钮)findViewById(R.id.C​​olor);
                Colorpaint.setOnClickListener(新OnClickListener(){                    公共无效的onClick(视图v){
                        INT颜色= preferenceManager.getDefaultShared preferences(
                                Main.this).getInt(COLOR_ preFERENCE_KEY,Color.WHITE);
                        // INT _Color = R.color.red;
                        新ColorPickerDialog(v.getContext()
                                新OnColorChangedListener(){                                    公共无效colorChanged(INT颜色){
                                        mPaint.setColor(颜色);                                        slll =颜色;                                        Log.i(TAG,mpaint一+ mPaint);
                                    }
                                },mPaint.getColor())显示();
                        Log.i(TAG,mpaint两个+ mPaint);
                    }
                });
                relativelayout.addView(MVIEW);
            }            // ////////// ******************* Pinting视图
            // ******************* ///////////////////            MyView的扩展查看公共类实现OnTouchListener {
                私人地图<路径,整数GT; colorsMap =新的HashMap<路径,整数GT;();
                私人的ArrayList<路径和GT; mMaindialog =新的ArrayList<路径>();
                私人的ArrayList<路径和GT; undonePaths =新的ArrayList<路径>();
                INT colorPicked = slll;
                //涂料mPaint1;                私人帆布mCanvas;
                私人路径的mpath;                公共MyView的(语境C中,int W,INT高){
                    超(C);
                    如果(GlobalVariable.impath == 1)
                    {
                        Log.d(,111111+ GlobalVariable.impath);
                        的System.out.println(GlobalVariable.impath);
                        意图二= getIntent();
                         位置= ii.getStringExtra(图像);
                        // bitmap.recycle();
                         Log.d(,位置+位置);
                         位= BitmapFactory.de codeFILE(位置);
                         mBitmap = Bitmap.createScaledBitmap(位图,300,300,假);
                         Log.d(hhhhhhhhhhhhhhhssssssss,mBitmap+ mBitmap);
                         // mBitmap = Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888);
                        // idd.setImageBitmap(mBitmap);
                        Log.d(hhhhhhhhhhhhhhhssssssss,GlobalVariable.impath+ GlobalVariable.impath);
                    }
                    否则如果(GlobalVariable.impath == 2){
                        //bitmap.recycle();
                        Log.d(,22222222222222222222222+ GlobalVariable.impath);
                        位= BitmapFactory.de codeResource(getResources(),R.drawable.base);
                        mBitmap = Bitmap.createScaledBitmap(位图,100,100,FALSE);
                        // mBitmap = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
                        Log.d(hhhhhhhhhhhhhhhssssssss1111111,mBitmap+ mBitmap);
                    }        //
                    mCanvas =新的Canvas(mBitmap);
                    的mpath =新路径();                }                @覆盖
                保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
                    super.onSizeChanged(W,H,oldw,oldh);                }                @覆盖
                保护无效的onDraw(帆布油画){                    canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);                    对于(路径P:mMaindialog){
                        mPaint.setColor(colorsMap.get(P));
                        canvas.drawPath(对,mPaint);
                    }
                    mPaint.setColor(slll);
                    canvas.drawPath(的mpath,mPaint);
                }                // ////// ************触摸evants绘画************** ///////
                私人浮动MX,我的;
                私有静态最终浮动TOUCH_TOLERANCE = 0;                私人无效touch_start(浮法X,浮法Y){
                    mPath.reset();
                    mPath.moveTo(X,Y);
                    MX = X;
                    我= Y;
                    undonePaths.clear();
                }                私人无效TOUCH_MOVE(浮法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;
                    }
                }                私人无效touch_up(){
                    mPath.lineTo(MX,我的);
                    //提交的路径,我们的屏幕外
                    mCanvas.drawPath(的mpath,mPaint);
                    //杀死这个,所以我们不要双击平局
                    的mpath =新路径();
                    mPath.reset();
                    mMaindialog.add(的mpath);
                }                @覆盖
                公共布尔onTouchEvent(MotionEvent事件){
                    浮X = event.getX();
                    浮Y = event.getY();
                    开关(event.getAction()){
                    案例MotionEvent.ACTION_DOWN:
                        // touch_start(X,Y​​);
                        //无效();
                        undonePaths.clear();
                        mPath.reset();
                        mPath.moveTo(X,Y);
                        MX = X;
                        我= Y;
                        无效();
                        打破;
                    案例MotionEvent.ACTION_MOVE:
                        // TOUCH_MOVE(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;
                        }
                        无效();
                        打破;
                    案例MotionEvent.ACTION_UP:
                        // 润色();
                        //无效();
                        mPath.lineTo(MX,我的);
                        mMaindialog.add(的mpath);
                        colorsMap.put(的mpath,slll);
                        的mpath =新路径();
                        mPath.reset();
                        无效();
                        打破;
                    }
                    返回true;
                } //图像触摸事件的结束                民营涂料个CreatePen(INT colorPicked){
                    // TODO自动生成方法存根
                    mPaint1 =新的油漆();
                    mPaint1.setColor(colorPicked);
                    mPaint1.setAntiAlias​​(真);
                    mPaint1.setDither(真);
                    mPaint1.setStyle(Paint.Style.STROKE);
                    mPaint1.setStrokeJoin(Paint.Join.ROUND);
                    mPaint1.setStrokeCap​​(Paint.Cap.ROUND);
                    // mPaint1.setStrokeWidth(3);
                    返回mPaint1;
                }                公共无效onClickRedo(){
                    如果(undonePaths.size()大于0){
                        mMaindialog.add(undonePaths.remove(undonePaths.size() - 1));
                        mView.invalidate();                    }其他{                    }
                    //敬酒用户
                }                公共无效onClickUndo(){
                    如果(mMaindialog.size()大于0){
                        undonePaths.add(mView.mMaindialog.remove(mView.mMaindialog
                                .size() - 1));
                        mView.invalidate();
                    }                    其他{                    }
                }                @覆盖
                公共布尔onTouch(查看arg0中,MotionEvent ARG1){
                    // TODO自动生成方法存根
                    返回false;
                }
            } //结束MyView的            @覆盖
            公共无效colorChanged(INT颜色){
                // TODO自动生成方法存根
                preferenceManager.getDefaultShared preferences(本).edit()
                        .putInt(COLOR_ preFERENCE_KEY,颜色).commit();
                slll =颜色;
            }        }

Am not performing Undo/Redo operation canvas in this code. When am use the array list for removing the postion then it not work and during changing the color of paint from color picker the previous drawing color has been also change. I am not understanding that how to perform UNDO/REDO for this. So plz help

//This is main class

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (mView = new MyView(this));

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);

mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                               0.4f, 6, 3.5f);

mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

}

//And MyView class is

public class MyView extends View {

private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;  
private Bitmap  mBitmap;
private Canvas  mCanvas;
private Path    mPath;
private Paint   mBitmapPaint;
ArrayList<Path> paths = new ArrayList<Path>();
ArrayList<Path> undonePaths = new ArrayList<Path>();
public MyView(Context c) 
{
super(c);

mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
// mCanvas.setBackgroundResource(0xFFFFFFFF);
mCanvas.drawColor (0xFFFFFFFF);
paths.add(mPath);
}
public MyView (Context c, int color)
   {
   super(c);

     mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
     mCanvas = new Canvas(mBitmap);
     mPath = new Path();
     mBitmapPaint = new Paint(Paint.DITHER_FLAG);
     mCanvas.drawColor (color);
     paths.add(mPath);
  }
@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(0xFF000000);

     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
     for(Path p: paths)
      {
     canvas.drawPath(p, mPaint);
      }
    }

    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);
       paths.add(mPath);
       mPath.reset();
     }

  @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();
            invalidate();
            break;
       }
       return true;
     }

    public void clear ()
     {
         Paint p = new Paint();
         p.setAntiAlias(true);
         p.setDither(true);
         p.setColor(0xFFFFFFFF);
         mCanvas.drawPaint (p);
      } 

   public void drawImage (int resourceId)
    {
         Bitmap _scratch = BitmapFactory.decodeResource(getResources(), resourceId);
         mCanvas.drawBitmap(_scratch, 0, 0, null);
    } 
    public void undo() {
        // TODO Auto-generated method stub
        mCanvas.drawCircle(posX, posY, 100, mPaint);
        Toast.makeText(getApplicationContext(), "Hello", 1000).show();
        if (paths.size()>0) 
        { 
           undonePaths.add(paths.remove(paths.size()-1));
           mView.invalidate();
           Toast.makeText(getApplicationContext(), "Undo", 1000).show();
         }
        else
        {
            Toast.makeText(getApplicationContext(), "Not", 1000).show();
        }
    }
          public void redo() {
        // TODO Auto-generated method stub
        if (undonePaths.size()>0) 
        { 
            paths.add(undonePaths.remove(undonePaths.size()-1)); 
            invalidate();
        } 
        else 
        {

        }

    }
   public void saveAsJpg (File f)
     {
        String fname = f.getAbsolutePath ();
        FileOutputStream fos = null;
        try {
        fos = new FileOutputStream (f);
        mBitmap.compress (CompressFormat.JPEG, 95, fos);                 
       } catch (Throwable ex) {
      ex.printStackTrace ();
       }      
     } // end saveAsJpg

   } // end MyView

   } // end class FingerPaint

解决方案

this working code .I test it on my own app and it is working very good.
May be it help u.Please comment on it.
            public class Main extends Activity implements OnColorChangedListener {
            //public static int selectedColor = Color.BLACK;
            public static ArrayList<Path> mMaindialog;
            // private ArrayList<Path> undonePaths;
            // public int selectedcolor;
            private static final String COLOR_PREFERENCE_KEY = "color";
            private FrameLayout relativelayout;
            static String sdpath,location;
            Boolean i;
            // Instance variables
            private Bitmap mBitmap=null;
            Bitmap bitmap;
            private Paint mPaint, mBitmapPaint, mPaint1;
            private MyView mView;
            ImageView idd;
            // private Path mPath;
            int slll = Color.BLACK;
            Bitmap map=ListView5.bMap;
            private Button ClearPaint, Colorpaint;
            Ghostdatabase gost;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                 idd=(ImageView)findViewById(R.id.imageView1);
                relativelayout = (FrameLayout) findViewById(R.id.frameLayout);

                DisplayMetrics metrics = getBaseContext().getResources()
                        .getDisplayMetrics();
                int w = metrics.widthPixels;
                int h = metrics.heightPixels;

                System.out.println(" width " + w);
                System.out.println(" height " + h);



                mView = new MyView(this, w, h);
                mView.setDrawingCacheEnabled(true);

                mPaint = new Paint();
                mPaint.setAntiAlias(true);
                mPaint.setDither(true);
                mPaint.setColor(Color.BLACK);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeJoin(Paint.Join.ROUND);
                mPaint.setStrokeCap(Paint.Cap.ROUND);
                mPaint.setStrokeWidth(4);

                ClearPaint = (Button) findViewById(R.id.ne);
                ClearPaint.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // mBitmap.eraseColor(Color.TRANSPARENT);
                        // mPath.reset();
                        // mView.invalidate();

                        mView.onClickUndo();

                    }
                });
                Button save22 = (Button) findViewById(R.id.save);
                save22.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        File cacheDir;
                        Toast.makeText(Main.this, "Photo", 500).show();
                        Bitmap icon;
                        relativelayout.setDrawingCacheEnabled(true);

                        icon = Bitmap.createBitmap(relativelayout.getDrawingCache());
                        Bitmap bitmap = icon;
                        relativelayout.setDrawingCacheEnabled(false);
                        // File mFile1 = Environment.getExternalStorageDirectory();
                        Date d = new Date();
                        String fileName = d.getTime() + "mg1.jpg";

                        File storagePath = (Environment.getExternalStorageDirectory());
                        File dest = new File(storagePath + "/CityAppImages");

                        if (!dest.exists()) {
                            dest.mkdirs();

                        }

                        File mFile2 = new File(dest, fileName);
                        sdpath = mFile2.getAbsolutePath();

                        Log.d("qqqqqqqqqqqqqqqqqqqqqqq", "zzzzzzzz" + sdpath);
                        try {
                            FileOutputStream outStream;

                            outStream = new FileOutputStream(mFile2);

                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

                            outStream.flush();

                            outStream.close();
                            Toast.makeText(Main.this, "Photo Saved Sucessfully", 500)
                                    .show();
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {

                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast.makeText(Main.this, "Photo Not Saved Sucessfully",
                                    500).show();
                        }

                        gost = new Ghostdatabase(Main.this);
                        gost.open();

                        gost.insertTitle(sdpath);
                    }
                });

                Button view = (Button) findViewById(R.id.listtt);
                view.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {

                        Intent ii = new Intent(Main.this, ListView5.class);
                        startActivity(ii);

                    }
                });

                Button Colorpaint = (Button) findViewById(R.id.Color);
                Colorpaint.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        int color = PreferenceManager.getDefaultSharedPreferences(
                                Main.this).getInt(COLOR_PREFERENCE_KEY, Color.WHITE);
                        // int _color = R.color.red;
                        new ColorPickerDialog(v.getContext(),
                                new OnColorChangedListener() {

                                    public void colorChanged(int color) {
                                        mPaint.setColor(color);

                                        slll = color;

                                        Log.i("TAG", "mpaint one" + mPaint);
                                    }
                                }, mPaint.getColor()).show();
                        Log.i("TAG", "mpaint two" + mPaint);
                    }
                });
                relativelayout.addView(mView);
            }



            // //////////******************* Pinting view
            // *******************///////////////////

            public class MyView extends View implements OnTouchListener {
                private Map<Path, Integer> colorsMap = new HashMap<Path, Integer>();
                private ArrayList<Path> mMaindialog = new ArrayList<Path>();
                private ArrayList<Path> undonePaths = new ArrayList<Path>();
                int colorPicked = slll;
                // Paint mPaint1;

                private Canvas mCanvas;
                private Path mPath;

                public MyView(Context c, int w, int h) {
                    super(c);
                    if(GlobalVariable.impath==1)
                    {
                        Log.d("","111111"+GlobalVariable.impath);
                        System.out.println(GlobalVariable.impath);
                        Intent ii = getIntent();
                         location = ii.getStringExtra("IMAGE");
                        // bitmap.recycle();
                         Log.d("","location"+location);
                         bitmap = BitmapFactory.decodeFile(location);
                         mBitmap = Bitmap.createScaledBitmap(bitmap,300, 300,false);
                         Log.d("hhhhhhhhhhhhhhhssssssss","mBitmap"+mBitmap);
                         //mBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
                        // idd.setImageBitmap(mBitmap);
                        Log.d("hhhhhhhhhhhhhhhssssssss","GlobalVariable.impath"+GlobalVariable.impath);
                    }
                    else if(GlobalVariable.impath==2){
                        //bitmap.recycle();
                        Log.d("","22222222222222222222222"+GlobalVariable.impath);
                        bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.base);
                        mBitmap = Bitmap.createScaledBitmap(bitmap,100, 100, false);
                        //mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                        Log.d("hhhhhhhhhhhhhhhssssssss1111111","mBitmap"+mBitmap);
                    }



        //          
                    mCanvas = new Canvas(mBitmap);
                    mPath = new Path();

                }

                @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.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

                    for (Path p : mMaindialog) {
                        mPaint.setColor(colorsMap.get(p));
                        canvas.drawPath(p, mPaint);
                    }
                    mPaint.setColor(slll);
                    canvas.drawPath(mPath, mPaint);
                }

                // //////************touching evants for painting**************///////
                private float mX, mY;
                private static final float TOUCH_TOLERANCE = 0;

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

                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);
                    // commit the path to our offscreen
                    mCanvas.drawPath(mPath, mPaint);
                    // kill this so we don't double draw
                    mPath = new Path();
                    mPath.reset();
                    mMaindialog.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();
                        undonePaths.clear();
                        mPath.reset();
                        mPath.moveTo(x, y);
                        mX = x;
                        mY = y;
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // touch_move(x, y);
                        // invalidate();
                        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;
                        }
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        // touch_up();
                        // invalidate();
                        mPath.lineTo(mX, mY);
                        mMaindialog.add(mPath);
                        colorsMap.put(mPath, slll);
                        mPath = new Path();
                        mPath.reset();
                        invalidate();
                        break;
                    }
                    return true;
                } // end of touch events for image

                private Paint createPen(int colorPicked) {
                    // TODO Auto-generated method stub
                    mPaint1 = new Paint();
                    mPaint1.setColor(colorPicked);
                    mPaint1.setAntiAlias(true);
                    mPaint1.setDither(true);
                    mPaint1.setStyle(Paint.Style.STROKE);
                    mPaint1.setStrokeJoin(Paint.Join.ROUND);
                    mPaint1.setStrokeCap(Paint.Cap.ROUND);
                    // mPaint1.setStrokeWidth(3);
                    return mPaint1;
                }

                public void onClickRedo() {
                    if (undonePaths.size() > 0) {
                        mMaindialog.add(undonePaths.remove(undonePaths.size() - 1));
                        mView.invalidate();

                    } else {

                    }
                    // toast the user
                }

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

                    else {

                    }
                }

                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    // TODO Auto-generated method stub
                    return false;
                }
            }// end MyView

            @Override
            public void colorChanged(int color) {
                // TODO Auto-generated method stub
                PreferenceManager.getDefaultSharedPreferences(this).edit()
                        .putInt(COLOR_PREFERENCE_KEY, color).commit();
                slll = color;
            }



        }

这篇关于如何不失previous色彩在画布上执行撤消重做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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