PorterDuff在ImageView上的路径上相乘将透明度变为黑色

时间:2015-11-11 23:42:53

标签: android

我想在图像上绘制路径,并使用使用PorterDuff乘法xfermode的颜色填充该路径。但是,Android似乎完成了包含应该透明的黑色路径的矩形部分。只有在活动打开硬件加速时才会发生这种情况 - 当它关闭时,它会按预期工作。

What I want (and what happens with hardware acceleration is off)

What I get

到目前为止我的(简化)代码:

public class ImageOverlayTest extends ImageView {

    private Paint mOverlayPaint;
    private Path mOverlayPath;

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

    private void init() {
        mOverlayPaint = new Paint();
        mOverlayPaint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
        mOverlayPaint.setColor(Color.RED);
    }

    protected void createOverlayPath() {
        mOverlayPath = new Path();
        mOverlayPath.moveTo(0, 0);
        mOverlayPath.lineTo(0, 400);
        mOverlayPath.lineTo(getWidth() / 3, 600);
        mOverlayPath.lineTo(getWidth() * 2 / 3, 600);
        mOverlayPath.lineTo(getWidth(), 400);
        mOverlayPath.lineTo(getWidth(), 0);
        mOverlayPath.lineTo(0, 0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        createOverlayPath();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mOverlayPath, mOverlayPaint);
    }
}

关闭硬件加速会真正杀死我活动的其他方面(简单的动画),所以我需要避免这种情况。同样,一个更简单但更丑陋的解决方案是只用一些透明度覆盖颜色,但这不是我喜欢的效果。

感谢您的任何见解。

1 个答案:

答案 0 :(得分:0)

自己想出来,你可以通过调用

来禁用View自身的硬件加速

<style name="FontSize"> </style> <style name="FontSize.Small"> <item name="font_small">14sp</item> <item name="font_medium">20sp</item> <item name="font_large">26sp</item> <item name="font_xlarge">32sp</item> </style> <style name="FontSize.Medium"> <item name="font_small">20sp</item> <item name="font_medium">26sp</item> <item name="font_large">32sp</item> <item name="font_xlarge">38sp</item> </style> <style name="FontSize.Large"> <item name="font_small">26sp</item> <item name="font_medium">32sp</item> <item name="font_large">38sp</item> <item name="font_xlarge">44sp</item> </style>