使用PorterDuffXfermode使图像的一部分透明

时间:2012-03-21 13:17:45

标签: android background transparent

有人请帮助我,我知道我哪里出错了,我已经尝试了所有可能的事情,但是出现了相同的黑色补丁 ..不知道哪里出错......

第一个图像是直接在画布上绘制的,然后在画布对象c2上,我绘制overlayDefault图像,对它应用绘画,然后在原始画布上绘制我绘制后使用的overlay_mutable < / p>

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

但仍然会绘制图像,但点击时只会出现黑色色块并且看不到基础图像!!

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

2 个答案:

答案 0 :(得分:2)

[回答我自己的问题,我之前在评论部分回答过]

替换

  

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

  

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

答案 1 :(得分:-1)

overlay_mutable = overlayDefault.copy(Config.ARGB_8888, true);创建一个具有Alpha通道的位图,但hasAlpha()将返回false

所以你需要做的是在通过copy(overlay_mutable)创建的位图上调用setHasAlpha(true)