3

左侧是在 Android P 测试版上截取的屏幕截图,右侧是在 Android 26 上截取的屏幕截图 曲线差异 。Xfermode 在 Android P 测试版上的工作方式似乎不一致。

下面是对应的代码。

public class CropView extends View {

    private Paint paint;
    private Path clipPath;
    private int arcHeight;

    public CropView(Context context) {
        super(context);
        init();
    }

    public CropView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CropView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        arcHeight = getResources().getDimensionPixelOffset(R.dimen.row_size);
        setLayerType();
    }

    private void setLayerType() {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            // looks like this is happening in some devices with lollipop and kitkat
            // trying to fix https://github.com/lifesum/bugs/issues/7040
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        } else {
            setLayerType(LAYER_TYPE_HARDWARE, null);
        }
    }

    private Path createClipPath(int height, int width) {
        final Path path = new Path();

        path.moveTo(0, 0);
        path.lineTo(0, height);
        path.quadTo(width / 2, height + arcHeight, width, height - arcHeight);

        path.lineTo(width, 0);
        path.close();

        return path;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            int height = getMeasuredHeight();
            int width = getMeasuredWidth();
            clipPath = createClipPath(height, width);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (clipPath != null) {
            canvas.drawPath(clipPath, paint);
        }
    }
}

以下是运行来自 android 示例的Apidemos时的屏幕截图

安卓 P 安卓P 安卓 27 安卓 27

4

2 回答 2

2

根据谷歌给出的答案,这是预期的行为。
https://issuetracker.google.com/issues/111819103

他们给出了 3 个选项

  1. 使用 android.view.ViewOutlineProvider (按照这里的示例https://github.com/googlesamples/android-ClippingBasic)。
  2. 将 ImageView 替换为 BitmapShader,该方法速度快(每个像素绘制一次)且更便携,因为 API 级别 1 支持 BitmapShader。 Paint paint = new Paint(); 油漆.setColor(颜色。黑色);paint.setStyle(Paint.Style.FILL); 位图 bm = BitmapFactory.decodeResource(getResources(), R.drawable.spiderman); 着色器着色器 = new BitmapShader(bm, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 油漆.setShader(着色器);路径角 = 新路径();corners.addRoundRect(bounds, radii, Path.Direction.CW); canvas.drawPath(角,油漆);
  3. 在 ImageView 上使用 SRC_OVER 绘制反向路径。如果背景是纯色并且速度较慢,则此方法有效,因为某些像素被绘制了两次。

其中第一个不能用于渲染凸复杂路径,如https://issuetracker.google.com/issues/37064491中所回答

尝试其他两个选项并在此处发布结果。

于 2018-08-08T08:28:42.730 回答
1

是的,我也遇到了同样的问题。

也许Android-Pie似乎不支持drawPath上的drawerDuff.Mode ...

但是,您仍然可以使用 drawBitmap 来切割图层。

例如:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (clipPathBitmap != null) {
        canvas.drawBitmap(clipPathBitmap, 0, 0, paint);
    }
}

private void makeClipPathBitmap() {
    clipPathBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(mShapeBitmap);
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    p.setColor(Color.BLACK);
    clipPath.draw(c, p);
}
于 2018-08-24T05:57:30.043 回答