2012-03-21 73 views
0

有人請幫助我,我不知道哪裏會出錯,我已經嘗試了一切可能的事情在這裏,但相同的黑色補丁出現 ..不知道哪裏即將出錯..使圖像的一部分透明使用PorterDuffXfermode

第一個圖像直接在畫布上繪製,然後在畫布對象c2上,繪製overlayDefault圖像,然後將繪畫應用到它,然後回到原始畫布上,繪製我獲得的overlay_mutable後使用

overlay_mutable = overlayDefault.copy(Config.ARGB_8888, true); 

但仍然繪製圖像,但只點擊一個黑色補丁顯示和底下ing圖像沒有看到!

public class PartTransparent extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(new TouchView(this)); 
    } 

    class TouchView extends View{ 
    Bitmap bgr; 
    Bitmap overlayDefault; 
    Bitmap overlay_mutable; 
    Paint pTouch; 
    int X = -100; 
    int Y = -100; 
    Canvas c2; 

    public TouchView(Context context) { 
     super(context); 

     bgr = BitmapFactory.decodeResource(getResources(),R.drawable.mah_grp_pic); 
     overlayDefault =  BitmapFactory.decodeResource(getResources(),R.drawable.z_oceanblue); 
    overlay_mutable = overlayDefault.copy(Config.ARGB_8888, true); // convert to ARGB_8888 format,only den can it be put on canvas in next line.. 
    c2 = new Canvas(); 
    c2.setBitmap(overlay_mutable); 
    c2.drawBitmap(overlayDefault, 0, 0, null); 

    pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);   
    pTouch.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
    pTouch.setAlpha(0); 
    pTouch.setColor(Color.TRANSPARENT); 
    pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL)); // Blur modes 

} 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 

    switch (ev.getAction()) { 

     case MotionEvent.ACTION_DOWN: { 

      X = (int) ev.getX(); 
      Y = (int) ev.getY(); 
      invalidate(); 
      break; 
     } 
     case MotionEvent.ACTION_MOVE: { 
       X = (int) ev.getX(); 
       Y = (int) ev.getY(); 
       invalidate(); 
       break; 
     }   
     case MotionEvent.ACTION_UP: 
      break; 
    } 
    return true; 
} 

@Override 
public void onDraw(Canvas canvas){ 
    canvas.drawBitmap(bgr, 0, 0, null); 
    //copy the default overlay into temporary overlay and punch a hole in it       
    c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw 
    c2.drawCircle(X, Y, 80, pTouch); 
    //draw the overlay over the background 
    canvas.drawBitmap(overlay_mutable, 0, 0, null); 
    super.onDraw(canvas); 
} 
} 
+1

無論如何,我找到了解決方案,而不是「overlay_mutable = overlayDefault.copy(Config.ARGB_8888,true);」 使用overlay_mutable = Bitmap.createBitmap(width,height,Config.ARGB_8888); – Mahesh 2012-03-23 13:58:20

回答

2

[回答我的問題,我已經回答的評論部分先前]

更換

overlay_mutable = overlayDefault.copy(Config.ARGB_8888,真);」

overlay_mutable = Bitmap.createBitmap(寬度,高度,Config.ARGB_8888);

+1

你可以接受答案,以解決這個問題。 – sandrstar 2013-07-11 04:33:30

+0

你救了我整整一天! – zzy 2014-12-07 06:07:25

-1

overlay_mutable = overlayDefault.copy(Config.ARGB_8888, true);創建一個位圖,其確實具有alpha通道,但hasAlpha()將返回false

因此,所有你需要做的是在通過複製(overlay_mutable)創建的位圖調用setHasAlpha(true)