2012-09-06 6 views
4

이미 API 데모에서 fingurePaint.java를 보았습니다. 안드로이드에서 터치 이동으로 이미지의 일부를 지우려면 터치 부드러운 지우개를 구현하고 싶습니다.안드로이드에서 터치 부드러운 이미지 지우개를 구현하는 방법?

fingurePaint이

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

를 구현하라고하지만이 이미지를 지울 작동하지 않습니다. 그것은 터치로 그려진 무언가를 지우기 위해 노력하고 있습니다.

public class SandboxView extends View implements OnTouchListener { 
    public final Bitmap bitmap; 
    private final int width; 
    private final int height; 
    private Matrix transform = new Matrix(); 

    private Vector2D position = new Vector2D(); 
    private float scale = 1; 
    private float angle = 0; 
    public boolean isInitialized = false; 
    private TouchManager touchManager = new TouchManager(2); 
    final GestureDetector mGesDetect; 


    // Debug helpers to draw lines between the two touch points 
    private Vector2D vca = null; 
    private Vector2D vcb = null; 
    private Vector2D vpa = null; 
    private Vector2D vpb = null; 

    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4; 
    private Path mPath; 
    private Canvas mCanvas; 
    private Paint  mPaint; 
    private Paint mBitmapPaint; 
    public SandboxView(Context context, Bitmap bitmap) { 
     super(context); 

     this.bitmap = bitmap; 
     this.width = bitmap.getWidth(); 
     this.height = bitmap.getHeight(); 
     this.mGesDetect = new GestureDetector(context, new DoubleTapGestureDetector()); 

     setOnTouchListener(this); 
    } 


    private float getDegreesFromRadians(float angle) { 
     return (float)(angle * 360.0/Math.PI); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     if (!isInitialized) { 
      Bitmap mBitmap = bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(mBitmap); 
      mPaint = new Paint(); 
      mPath = new Path(); 
      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(12); 
      mPaint.setAlpha(0); 
      mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
      mPaint.setAntiAlias(true); 

      mBitmapPaint = new Paint(Paint.DITHER_FLAG); 


      int w = getWidth(); 
      int h = getHeight(); 
      position.set(w/2, h/2); 
      isInitialized = true; 
     } 
     if(isEraser==1){ 
      canvas.drawColor(80000000); 


      canvas.drawBitmap(bitmap, transform, mBitmapPaint); 

      canvas.drawPath(mPath, mPaint); 
     } 
     else{ 


     Paint paint = new Paint(); 

     transform.reset(); 
     transform.postTranslate(-width/2.0f, -height/2.0f); 
     transform.postRotate(getDegreesFromRadians(angle)); 
     transform.postScale(scale, scale); 
     transform.postTranslate(position.getX(), position.getY()); 

     canvas.drawBitmap(bitmap, transform, paint); 

     try { 
      /*paint.setColor(0xFF007F00); 
      canvas.drawCircle(vca.getX(), vca.getY(), 64, paint); 
      paint.setColor(0xFF7F0000); 
      canvas.drawCircle(vcb.getX(), vcb.getY(), 64, paint); 

      paint.setColor(0xFFFF0000); 
      canvas.drawLine(vpa.getX(), vpa.getY(), vpb.getX(), vpb.getY(), paint); 
      paint.setColor(0xFF00FF00); 
      canvas.drawLine(vca.getX(), vca.getY(), vcb.getX(), vcb.getY(), paint);*/ 




     } 
     catch(NullPointerException e) { 
      // Just being lazy here... 
     } 
     } 
    } 

    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(); 
    } 




    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     if(isEraser ==1){ 
      float x = event.getX(); 
      float y = event.getY(); 

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


      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; 
     } 
     else{ 
     vca = null; 
     vcb = null; 
     vpa = null; 
     vpb = null; 
     mGesDetect.onTouchEvent(event); 

     try { 
      touchManager.update(event); 

      if (touchManager.getPressCount() == 1) { 
       vca = touchManager.getPoint(0); 
       vpa = touchManager.getPreviousPoint(0); 
       position.add(touchManager.moveDelta(0)); 
      } 
      else { 
       if (touchManager.getPressCount() == 2) { 
        vca = touchManager.getPoint(0); 
        vpa = touchManager.getPreviousPoint(0); 
        vcb = touchManager.getPoint(1); 
        vpb = touchManager.getPreviousPoint(1); 

        Vector2D current = touchManager.getVector(0, 1); 
        Vector2D previous = touchManager.getPreviousVector(0, 1); 
        float currentDistance = current.getLength(); 
        float previousDistance = previous.getLength(); 

        if (previousDistance != 0) { 
         scale *= currentDistance/previousDistance; 
        } 

        angle -= Vector2D.getSignedAngleBetween(current, previous); 
       } 
      } 

      invalidate(); 
     } 
     catch(Throwable t) { 
      // So lazy... 
     } 
     return true; 
     } 
    } 
    class DoubleTapGestureDetector extends GestureDetector.SimpleOnGestureListener { 


     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      colorseekbar.setVisibility(View.INVISIBLE); 
      opacityseekbar.setVisibility(View.INVISIBLE); 
      return true; 
     } 
    } 

}

그래서 터치 이동을 사용하여 이미지의 부분을 삭제하는 데 도움이 바랍니다. 사전에

감사합니다.

+0

내가 작업 데모 http://whats-online.info/science-and-tutorials/137/Android-tutorial-How-to-erase-part-of-bitmap-하여이 예를 참조 할 수 있습니다 image-on-touch/ –

답변

0

먼저 생성자에 모든 속성이있는 페인트를 선언하십시오.

쓰기 당신의 의 onDraw() 방법

@Override 
protected void onDraw(Canvas canvas) 
    { 
    System.out.println("come in on draw......"); 
    canvas.drawColor(Color.TRANSPARENT); 
    canvas.drawBitmap(mBitmap, 0, 0, mPaint); 
      if(eraser==true) 
       mPaint.setColor(Color.TRANSPARENT): 
      else 
       mPaint.setColor(Color.RED): 
    canvas.drawPath(mPath, mPaint); 

    super.dispatchDraw(canvas); 
} 

두 번째 솔루션이 코드 : 당신의 touch_move의 방법 아래

()를 호출 방법

mBitmap.setPixel (x, y, Color.TRANSPARENT);이 방법 만 X를 통과해야 비트 맵을 바꿀 것입니다, Y & COLOR

public void changeBitmap(int x, int y, Bitmap mBitmap) 
{ 
Bitmap tempBitmap = Bitmap.createBitmap(mBitmap); //Edited code 
tempBitmap.setPixel(x, y, Color.TRANSPARENT); 
} 



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; 
      changeBitmap(x, y, your_bitmap) 
     } 
    } 
0

이 경우를 반대로하는 것이 더 좋지 않습니까? 배경색을 사용하여 이미지에 페인트 칠을하고 저장하면 필요한 경우 해당 레이어를 병합합니다.

+0

뒷면에는 이미지가 있습니다. 내 말은 다른 이미지 위에 하나의 이미지가 있고 최상위 이미지를 지우는 것입니다. 어떻게하는지 –

0

다음 터치 이벤트에 경로 나 선, 원, 이미지 무엇을 그리고 다음이 통과 임시 캔버스와 비트 맵을 정의 임시 비트 맵을 onDraw에서 캔버스로 작업하면 작업이 제대로 완료되고 지우기를 원할 경우 임시 캔버스에서 지우기를 수행하십시오.

protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     TemporaryBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444); 
     TemporaryCanvas = new Canvas(TemporaryBitmap); 
} 
TemporaryCanvas.drawColor(0, PorterDuff.Mode.clear); 
public void onDraw(Canvas canv){ 
canv.drawBitmap(TemporaryBitmap, matrix, paint); 
관련 문제