2011-05-12 50 views
1

在我開始之前,我希望您告訴我爲「撤消」操作使用了一種非常不正確的方法。 我有一個繪圖應用程序,它將保存您在視圖上繪製的每個筆畫,並且當您退出應用程序時,最終圖像將被複制到一個文件夾中,其他人將被刪除。在繪圖應用程序中撤消

現在,當您在視圖上繪製筆畫時,每個文件都將保存在一個臨時文件中。文件夾和每個文件的路徑將被插入到數據庫中,這是爲了實現撤消操作。當用戶點擊撤消時,最後插入的值(路徑)將被刪除,並且下一個路徑將被調用。 我的計劃是使用此路徑在視圖上繪製圖像,因此每次用戶的「撤消」時,數據庫中保存的最後一條路徑將被刪除,並且將採用下一條路徑。

希望你有我的想法..! 現在我的問題是,我無法將圖像繪製到視圖中, 我有一個繪製斯托克斯的自定義視圖。

查看

public class MyView extends View { 

     private static final float MINP = 0.25f; 
     private static final float MAXP = 0.75f; 

     private Bitmap mBitmap; 

     private Paint mBitmapPaint; 

     public MyView(Context c) { 
      super(c); 


      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); 
      mCanvas.drawColor(Color.BLACK); 

     } 

     @Override 
     protected void onDraw(Canvas canvas) { 

      try{ 

       new SaveFileThread().execute(null);  //------->save strokes as files 

       Log.i("In ondraw log", "Wow "+filename); 

       if(undoflag=="undo" && nextp!="haha"){ 

        String pat=UnDo(); 

        if(pat!=null){ 

         if(pat==filename){ pat=UnDo(); Log.i("undo called twice", "not again");} 
         else{ 

         // Bitmap nbmp=LoadBMPsdcard(pat); 
          //render the SD card image 
         //  canvas.drawBitmap(nbmp, 0, 0, null); 

         } 
        } 
        //the parent onDraw() method. 
       // super.onDraw(canvas); 

       } 


      //canvas.drawColor(000000); 
      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
      canvas.drawPath(mPath, mPaint); 

      }catch(Exception e){ 
       e.printStackTrace(); 
      } 

     } 

     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); 
      // 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) { 
      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; 
     } 
    } 

我有一個線程,會做降耗, 如何從SD卡這一視圖中繪製的圖像。

任何人都可以建議我做任何更好的想法。 如果你有一些示例代碼,如果你帶來不同的想法,這將是一個很大的幫助。

在此先感謝

編碼快樂

回答

0

好了,我不會的位圖文件中的所有存儲。如果它是一個簡單的繪圖應用程序,那麼你可以(我會)只有一個arrayList的20位圖,並將位圖存儲到列表中。如果長度超過20,請移除第一個項目。這會給你一些撤銷緩衝區,並允許你更輕鬆地處理操作。至於保存到SD卡看看here

〜Aedon

編輯1:

使用此方法來創建畫布的位圖。

public Bitmap toBitmap(Canvas canvas) { 
    Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); 
    canvas.setBitmap(b); 
    draw(canvas); 
    return b; 
} 

樣品onToushListener:

new OnTouchListener() { 
    public void onTouch(View v, MotionEvent e) { 
     switch(e.getAction()) { 
      case MotionEvent.ACTION_DOWN: mIsGesturing = true; mCurrentDrawPath.moveTo(...); // Fall through 
      case MotionEvent.ACTION_MOVE: // create the draw path the way you are now. 
      case MotionEvent.ACTION_UP: mIsGesturing = false; addNewBufferBitmap(toBitmap(canvas)); break; 
     } 
    } 
}; 

和的onDraw(帆布)將類似於此:

public void onDraw(Canvas canvas) { 
    canvas.drawBitmap(getLastBufferBitmap(), 0, 0, null); 
    if (mCurrentDrawPath != null) canvas.drawPath(mCurrentDrawPath, mPathPaint); 
    if (mIsGesturing) mCanvas = canvas; 
} 
+0

嗨,感謝您回答我的問題,我認爲這是一個很好的使用堆棧的建議,但是我的疑問是,每次用戶單擊「撤消」時,我怎樣才能從畫布中提取該位圖? – rahul 2011-05-13 03:51:26

+0

當您繪製到畫布時,聲明它的位圖。當用戶點擊撤消時,畫布將不得不更新。只需繪製最後一個已知位圖(列表中的最後一個)。 – AedonEtLIRA 2011-05-13 16:01:16

+0

好的......我們如何通過創建一個新的畫布對象來更新畫布。像這樣 - > mCanvas = new Canvas(bitmap); ? – rahul 2011-05-13 17:38:31

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(); 
    } 
相關問題