2013-03-25 70 views
0

我正在嘗試開發一個應用程序。我想現在清除畫布,我寫了清晰的屏幕編碼。它的工作..但一個問題的背景圖像也清除。我只想清除圖紙。如何清除圖紙?這裏我動態地設置了背景圖片。如何只在畫布中清除繪圖?

package com.example.graphicsdraw; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.EmbossMaskFilter; 
import android.graphics.MaskFilter; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.PorterDuff; 
import android.graphics.PorterDuffXfermode; 
import android.view.Display; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RelativeLayout; 
import android.widget.RelativeLayout.LayoutParams; 

public class MainActivity extends Activity { 
    private Button Back, Next, Erase,clrScreen; 
    int FlagSet = 0, ImageSet = 0; 
    static int Flag = 0; 
    MyView myview; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     myview = new MyView(this); 
     RelativeLayout myLayout = new RelativeLayout(this);   
     myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     Back = new Button(this); 
     Back.setText("Back"); 
     Next = new Button(this); 
     Next.setText("Next"); 
     Erase = new Button(this); 
     Erase.setText("Eraser"); 
     clrScreen = new Button(this); 
     clrScreen.setText("Clear"); 
     myLayout.addView(myview); 
     myLayout.addView(Back); 
     myLayout.addView(Next); 
     myLayout.addView(Erase); 
     myLayout.addView(clrScreen); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(60, LayoutParams.WRAP_CONTENT); 
     params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
     params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(60, LayoutParams.WRAP_CONTENT); 
     params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
     RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(65, LayoutParams.WRAP_CONTENT); 
     params3.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     params3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
     Erase.setLayoutParams(params3); 
     Erase.bringToFront(); 
     Next.setLayoutParams(params2); 
     Next.bringToFront(); 
     Back.setLayoutParams(params); 
     Back.bringToFront(); 
     clrScreen.setLayoutParams(params3); 
     clrScreen.bringToFront(); 
     this.setContentView(myLayout); 


     Back.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       Intent i = new Intent(MainActivity.this, MainActivity.class); 
       Flag = Flag - 1; 
       startActivity(i); 
       finish(); 

      } 
     }); 
     Next.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       Intent i = new Intent(MainActivity.this, MainActivity.class); 
       Flag = Flag + 1; 
       startActivity(i); 
       finish(); 

      } 
     }); 

     Erase.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
       /*Intent i = new Intent(MainActivity.this, MainActivity.class); 
       Flag = Flag; 
       startActivity(i); 
       finish();*/ 
       mPaint = new Paint(); 
       mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 
       mPaint.setColor(Color.BLUE); 
       mPaint.setAntiAlias(true); 
      } 
     }); 
     clrScreen.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       myview.Clear_Drawing(); 

      } 


     }); 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(Color.WHITE);//0xffffffff 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(10); 
     mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f); 
     mPaint.setMaskFilter(mEmboss); 
    } 
    private Paint  mPaint; 
    private MaskFilter mEmboss; 
    public class MyView extends View 
    { 
     MainActivity ma=new MainActivity(); 
     Bitmap mBitmap = null; 
     private Canvas mCanvas; 
     Path mPath = null; 
     private Paint mBitmapPaint; 
     Bitmap bmp; 
     Display display = getWindowManager().getDefaultDisplay(); 
     int width = display.getWidth(); 
     public MyView(Context c) { 
      super(c); 
      switch(Flag) 
      { 
      case 0: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 1: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 2: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 3: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 4: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 5: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 6: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 7: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 8: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 9: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 10: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 11: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 12: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 13: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 14: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 15: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 16: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 17: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 18: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 19: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 20: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 21: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 22: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 23: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 24: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 25: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 26: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 27: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 28: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 29: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 30: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 31: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 32: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 33: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 34: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 35: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 36: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 37: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 38: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 39: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 40: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 41: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 42: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 43: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 44: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      case 45: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       break; 
      case 46: 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); 
       break; 
      default: 
       break; 
      } 
      //Display display = getWindowManager().getDefaultDisplay(); 
      // int width = display.getWidth(); 
      mBitmap = Bitmap.createScaledBitmap(bmp, width, 350, true); 
      //mBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(mBitmap); 
      mPath = new Path(); 
      mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
      mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) ;     

     }  
     protected void onDraw(Canvas canvas) 
     { 
      canvas.drawColor(Color.TRANSPARENT); 
      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
      canvas.drawPath(mPath, 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); 
      mPath.reset(); 
     } 
     public void Clear_Drawing() 
     { 
      //mBitmap = null; 
      mPath = null; 
      mBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888); 
      mPath = new Path(); 
     } 
     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; 
     } 
    } 
} 
+0

其中一個答案能解決您的問題嗎? – 2013-11-29 01:15:16

回答

0

除了直接繪製到背景上,您應該使用myview.setBackground()作爲背景。繼續使用Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888)創建一個用於繪圖的空白位圖。

mBitmap = Bitmap.createScaledBitmap(bmp, width, 350, true); 
setBackground(mBitmap); 
mBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888); 
mCanvas = new Canvas(mBitmap); 
+0

順便說一句,您可以使用%運算符來替換您的大開關語句。表達式Flag%2的計算結果爲偶數和0爲奇數,所以0和1是唯一需要處理的兩種情況。 – 2013-03-25 09:28:07

0

它一定會清除背景圖像,因爲它是帆布的一部分,你應該做的是使用的FrameLayout地方背景的第一個孩子,並在其上創建透明畫布和更改第一個孩子的背景該FrameLayout裏不繪製圖像畫布

上創建透明畫布下的佈局,當用戶更改背景圖片更改下面沒有佈局在畫布上的形象,剩下的只是罰款

+0

我可以清除繪圖嗎?@ aashish – Bala 2013-03-25 10:01:43

+0

當然你可以像你現在這樣做只是記住你想保留的背景不能在畫布上,但在它下面 – 2013-03-25 10:03:16

+0

怎麼辦? ?如果你可以編輯我的代碼並給我......提前致謝...... @ aashish – Bala 2013-03-25 10:10:09

0

這是比如重置繪圖工具並提供一個新的畫布來繪製。

mBitmap = Bitmap.createBitmap(mv.getWidth(),mv.getHeight() ,Bitmap.Config.ARGB_8888);  
mCanvas = new Canvas(mBitmap); 
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(0); 
mv.invalidate();// call invalidate to refresh the view's draw.