2012-12-06 91 views
6

我想擦除在灰色方形位圖上檢測到手指的區域,但它不起作用。如何用手指擦除畫圖

我顯示位圖與ic_launch作爲圖像然後我在圖像上顯示一個灰色正方形油漆其中i可修改顏色到透明

什麼問題?謝謝

private Bitmap mBitmap; 
private Canvas mCanvas; 
private Path mPath; 
private Paint mBitmapPaint; 
private Paint mPaint; 

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

    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeWidth(40); 


    mBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); 
    for (int i = 0; i < 100; i++) { 
     for (int j = 0; j < 100; j++) { 
      mBitmap.setPixel(i, j, Color.GRAY); 
     } 

    } 
    mCanvas = new Canvas(mBitmap); 
    mPath = new Path(); 
    mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(0, 0, oldw, oldh); 

} 

@Override 
protected void onDraw(Canvas canvas) { 

    canvas.drawRect(100, 100, 200, 200, mBitmapPaint); 
    canvas.drawPath(mPath, mPaint); 

    Bitmap _scratch = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ic_launcher); 
    canvas.drawColor(Color.WHITE); 

    // logo 
    canvas.drawBitmap(_scratch, 0, 100, null); 



    for (int i = 0; i < 100; i++) { 
     for (int j = 0; j < 100; j++) { 
      if ((mX < 100 && mX >= 0) && (mY < 100 && mY >= 0)) { 

       mBitmap.setPixel((int) mY,(int) mX, Color.TRANSPARENT); 
      } 
     } 

    } 
    canvas.drawBitmap(mBitmap, 0, 100, mBitmapPaint); 

    Bitmap mutableBitmap = Bitmap.createBitmap(_scratch.getWidth(), 
      _scratch.getHeight(), Bitmap.Config.ARGB_8888); 
    mutableBitmap.setPixel(50, 50, 124); 
    canvas.drawBitmap(mutableBitmap, 0, 100, null); 
    int pixelColor = mBitmap.getPixel(50, 50); 
    int red = Color.red(pixelColor); 
    int green = Color.green(pixelColor); 
    Log.v("red", "red:" + red + " /green:" + green); 


} 


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(); 
    Log.v("onTouchEvent", "x:" + x + "/y:" + y); 

    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

你檢查它?讓我知道如果你解決了它..如果是的話,那麼標記爲答案 – 2012-12-06 11:48:48

回答

5

對於繪圖,您需要使用一個Paint類繪製路徑。但是對於再次刪除已畫上你觸摸一個路徑座標這樣

mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

Android的SDK樣本,他們給了一類FingerPaint解釋得很好

+0

@ user1881979但我有一些關於使用mPaint.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.CLEAR))的問題;你可以看看這個問題http://stackoverflow.com/questions/16229496/eraser-with-porterduff-mode-clear-always-draws-a-transparent-line – AndroidDev