如何在android中对paint进行撤消操作

时间:2012-02-21 14:56:06

标签: android android-emulator

我需要在paint中执行撤消操作。有人建议使用命令模式,但我没有得到命令模式。有没有任何解决方案。帮我解决如何撤消。           我使用下面的代码绘制颜料。

         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)
                           {

                  }
                           }
                        }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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();
    }