使用PorterduffXfermode创建透明圆

时间:2014-03-02 21:13:41

标签: android image android-canvas

我正在尝试创建一个显示两个图像“bitmap_1,bitmap_2”的应用程序,当用户点击屏幕时(显示图像“bitmap_2”),会出现一个透明圆圈,显示一个圆圈下方的部分bitmap_1。

我花了很多时间寻找解决方案,我推出了一些代码,它在三星S 9001“S1”上运行良好但是在其他设备上显示黑色圆圈

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

        setBmImages(imagePosition);

        // overlayDefault =
        // BitmapFactory.decodeResource(getResources(),R.drawable.pre1);
        // overlay =
        // BitmapFactory.decodeResource(getResources(),R.drawable.pre1).copy(Config.ARGB_8888,
        // true);
        c2 = new Canvas(overlay);

        pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
        pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
        pTouch.setColor(Color.TRANSPARENT);
        pTouch.setMaskFilter(new BlurMaskFilter(18, Blur.NORMAL));

    }

...

          @Override
           public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // draw background
        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, 0, 0, null);

    }

请帮我解决这个问题,参考这段代码:  PorterduffXfermode: Clear a section of a bitmapAndroid canvas: draw transparent circle on imageMake certain area of bitmap transparent on touch

1 个答案:

答案 0 :(得分:0)

@Elias Sh。

我已经解决了这个问题。

试试这个

     @Override
               public void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            // draw background
            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);    

            c2.drawCircle(X, Y, 80, pTouch);


    // draw the overlay over the background
    // code for png's with transparency
    /* canvas.drawBitmap(overlay, 0, 0, null); */

    // below code to be used only if images are NOT png's with transparency
    Paint ptouch2 = new Paint(Paint.ANTI_ALIAS_FLAG);
            ptouch2.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
            canvas.drawBitmap(overlay, 0, 0, ptouch2);
}