2012-02-21 56 views
0

您好我需要在paints中执行撤销操作。有人建议使用命令模式,但我没有得到命令模式。在那里有任何解决方案。帮助我如何做撤消。 我使用下面的代码绘制颜料。如何执行撤消操作到油漆中android

  BookType3.class: 
       public class BookType3 extends Activity implements OnClickListener{ 
      MyView myview; 
     int counter=0; 
     private Paint mBitmapPaint; 
      TextView t1,t2; 
      private Bitmap mBitmap; 
    public static boolean action=false; 
     Button back,erase,undo,save,home; 
     int image1,image2,image3; 
     private Canvas mCanvas; 
     RelativeLayout relative; 
       RelativeLayout.LayoutParams lp6,lp7; 
       public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
       requestWindowFeature(Window.FEATURE_NO_TITLE); 
       setContentView(R.layout.booktype1); 
      List<String> names = Arrays.asList("a","b","c"); 
       relative=(RelativeLayout)findViewById(R.id.relative3); 
       myview = new MyView(this); 
        myview.setId(004); 
       lp6 = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT,   RelativeLayout.LayoutParams.WRAP_CONTENT); 
       myview.setLayoutParams(lp6); 
      myview.setBackgroundResource(R.drawable.writingsapce); 
       undo=(Button)findViewById(R.id.undobutton); 
       undo.setOnClickListener(this); 
        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); 
      String s1=""; 
     t1=(TextView)findViewById(R.id.button1); 
     t1.setOnClickListener(this);   
     t2=(TextView)findViewById(R.id.button2); 
     t2.setOnClickListener(this); 
      relative.addView(myview,lp6); 
     } 
      private Paint  mPaint; 
      public class MyView extends View{ 
    private Path mPath; 
     public MyView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     mPath = new Path(); 
      mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
    } 
      @Override 
     protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
      super.onSizeChanged(w, h, oldw, oldh); 
      mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(mBitmap); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
         canvas.drawPath(mPath, mPaint); 
         if(action) 
           { 
         invalidate(); 

          } 
          } 

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

     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); 
      // commit the path to our offscreen 
      mCanvas.drawPath(mPath, mPaint); 
      // kill this so we don't double draw 
      mPath.reset(); 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event) { 
      int x = (int) event.getX(); 
      int y = (int) event.getY(); 
      Log.i("x",""+event.getX()); 
      Log.i("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 onClick(View v) { 
// TODO Auto-generated method stub 
if(v==t1) 
{ 
    mPaint.setColor(Color.RED); 
    t30.setText("A"); 
    t31.setText("a"); 
    t32.setText("Aa"); 
    t30.setTextColor(Color.RED); 
    t31.setTextColor(Color.RED); 
    t32.setTextColor(Color.RED); 
     try{ 
     mBitmap.eraseColor(android.graphics.Color.TRANSPARENT); 
     Canvas Canvas=new Canvas(mBitmap); 

     action=true; 
    myview.onDraw(Canvas); 

     }catch(IllegalStateException ie){ 
      ie.printStackTrace(); 
     } 
    } 
    if(v==t2) 
     { 
    t30.setTextColor(Color.BLUE); 
    t31.setTextColor(Color.BLUE); 
    t32.setTextColor(Color.BLUE); 
    mPaint.setColor(Color.BLUE); 
    t30.setText("B"); 
    t31.setText("b"); 
    t32.setText("Bb"); 
    try{ 
      mBitmap.eraseColor(android.graphics.Color.TRANSPARENT); 
      Canvas Canvas=new Canvas(mBitmap); 
        action=true; 
     myview.onDraw(Canvas); 

      }catch(IllegalStateException ie){ 
       ie.printStackTrace(); 
      } 
         } 
            if(v==undo) 
          { 

        } 
          } 
         } 

回答

0

不是命令模式,我认为你应该使用Memento Pattern,这是专为喜欢的东西撤销和重做。有两种方法来实现这种模式。要么通过保存三角洲,要么保存整个州。保存状态在你的情况下最容易,但它有更大的内存占用。这将涉及每次用户修改主位图时将位图写入文件系统。您可以决定一个固定数量的撤消步骤(例如10),然后保存10个最新的位图。保存增量将更加复杂。您可以通过记录每个绘画动作的触摸事件,颜色,笔刷等来实现它。然后,您可以使用动作事件回放和/或回滚单个动作。保存增量会占用更少的内存,并且您可以存储更多的操作,而不必写入文件系统。

+0

您所描述的三角洲系统基本上是我听到“命令模式”时的想法。这些命令会对动作进行编码以及公开执行和反转方法。 – ravuya 2012-02-21 19:30:15

0
private Slate mSlate; 
private TiledBitmapCanvas mTiledCanvas; 

public void clickUndo(View unused) { 

     mSlate.undo(); 
    } 


public void undo() { 
     if (mTiledCanvas == null) { 
      Log.v(TAG, "undo before mTiledCanvas inited"); 
     } 
     mTiledCanvas.step(-1); 

     invalidate(); 
    }