2016-05-20 4 views
0

Nehmen wir an, ich habe 500 x 500 ImageView, die Hälfte von ImageView sind rot zweiten Teil ist transparent. Ich möchte mit dem Finger nur im nicht transparenten Teil von ImageVIEW zeichnen, also wenn ich im transparenten Teil zeichne, ist es nicht sichtbar, wie dieses Bild. Ist es möglich? enter image description hereWie zeichne ich mit dem Finger in Ansicht, ohne transparente Teile zu zeichnen?

Ich habe versucht, aber dieser Code funktioniert nicht korrekt.

public class DrawingView extends ImageView { 

     private boolean isTransparent; 
     private float mX, mY; 
     private static final float TOUCH_TOLERANCE = 4; 
     private Bitmap mBitmap; 
     private Canvas mCanvas; 
     private Path mPath; 
     private Paint mBitmapPaint; 
     Context mContext; 
     private Path circlePath; 

     public DrawingView(Context c) { 
      super(c); 
      mContext = c; 
      mPath = new Path(); 
      mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
      circlePath = new Path(); 
     } 

     @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_4444); 
      mCanvas = new Canvas(mBitmap); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      if (mBitmap != null && canvas != null && !isTransparent) { 
       canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
       canvas.drawPath(mPath, mPaint); 
      } 
     } 

     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) { 
       if(mBitmap.getPixel((int)x, (int)y) == Color.TRANSPARENT || mBitmap.getPixel((int)mY, (int)mY) == Color.TRANSPARENT) { 
        isTransparent = true; 
       } else { 
        isTransparent = false; 
       } 

       mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
       mX = x; 
       mY = y; 

       circlePath.reset(); 
       circlePath.addCircle(mX, mY, 30, Path.Direction.CW); 
      } 
     } 

     private void touch_up() { 
      mPath.lineTo(mX, mY); 
      circlePath.reset(); 
      mCanvas.drawPath(mPath, mPaint); 
      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; 
     } 
    } 
+0

Ich denke, dein bestes ist, den Teil, der die Zeichnung hat, seine eigene Ansicht zu machen, damit sein 'onTouchEvent' einfacher sein wird. Sie können eine 'ViewGroup' wie eine' LinearLayout' haben, die eine Ansicht mit dem transparenten Teil und die andere Ansicht mit dem Zeichnungsteil hat. –

+0

kris larson Ich kann das nicht tun, weil es benutzerdefinierte Bilder in Image (zum Beispiel Enimals) sein können. In diesem Fall habe ich einfach ein einfaches Bild eingefügt. –

Antwort

1

Ich weiß nicht, wo mPaint initialisiert wird, aber da mPaint verwendet wird, um die Finger Spur zu ziehen, sollte das tun, was Sie in dieser Zeile setzen wollen:

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

Siehe this image zur Erläuterung.

+0

Vielen Dank, das ist was ich suche! :) –

Verwandte Themen