画布中的PorterDuff.Mode.CLEAR不适用于5.0或更高版本:Android

时间:2015-08-20 13:23:14

标签: android canvas android-5.0-lollipop android-view porter-duff

我在下面的代码中使用了清晰的图像部分,

Paint round_brush;

class TouchView extends View
{

    Paint pTouch;
    int X = -100;
    int Y = -100;
    Canvas c2;

    public TouchView(Context context, Bitmap background_bitmap_, Bitmap foreground_bitmap_)
    {
        super(context);

        setFocusable(true);
        setFocusableInTouchMode(true);

        background_bitmap = null;
        foreground_bitmap = null;

        background_bitmap = background_bitmap_;

        foreground_bitmap = foreground_bitmap_.copy(Bitmap.Config.ARGB_8888, true);

        c2 = new Canvas(foreground_bitmap);

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

        setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        round_brush = new Paint();
        // smooths out the edges of what is being drawn
        round_brush.setAntiAlias(true);
        // set color 
        round_brush.setColor(Color.GRAY);
        // set style
        round_brush.setStyle(Paint.Style.STROKE);
        // set stroke
        round_brush.setStrokeWidth(4);

    }

    @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)
    {
        super.onDraw(canvas);

        //draw background
        canvas.drawBitmap(background_bitmap, new Matrix(), null);

        //cropping part of above image
        c2.drawCircle(X, Y, brushSize, pTouch);

        canvas.drawCircle(X, Y, brushSize, round_brush);

        //draw the overlay over the background
        canvas.drawBitmap(foreground_bitmap, new Matrix(), null);

    }

}

当我在4.4或更低的设备上测试时,它按预期工作,一切都很好!但在5.0或以上的设备上它绘制黑色圆圈!为什么?怎么了? 5.0或以上设备是否有特殊条件?

我的测试设备结果,

  1. Moto E(4.4)确定
  2. Moto E( 5.0.2 失败
  3. 三星(4.1.2)确定
  4. Yureka( 5.0.2 失败

0 个答案:

没有答案