android - Porter-Duff:不同形状的不同行为?

标签 android shapes porter-duff

我有以下布局:

            <LinearLayout
                android:id="@+id/myButton"
                android:layout_width="@dimen/logo_radius"
                android:layout_height="match_parent"
                android:background="@drawable/myShape">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/driver_half"/>
            </LinearLayout>

和以下 myShape 可绘制对象:

    <selector>   
        <item><shape android:shape="oval">
            <stroke android:color="@android:color/black" android:width="4dp"/>
            <solid android:color="@android:color/white" />
        </shape></item>
    </selector>

我应用了以下过滤器:

myButton.getBackground().setColorFilter( orange, PorterDuff.Mode.ADD );

结果是这样的:

enter image description here

然后我将 myShape 更改为圆角矩形:

    <selector>
        <item>
            <shape
                android:shape="rectangle">
                <corners android:bottomLeftRadius="@dimen/logo_radius" android:bottomRightRadius="2dp" android:topLeftRadius="@dimen/logo_radius" android:topRightRadius="2dp"/>
                <stroke
                    android:width="4dp"
                    android:color="@android:color/black"/>
                <solid android:color="@android:color/white"/>
            </shape>
        </item>
    </selector>

结果是这样的:

enter image description here

左边没有应用滤镜,右边有滤镜。

我想得到的:

enter image description here

我应该怎么做才能使用 Porter-Duff 滤镜正确绘制橙色边框?还有其他选择吗?

最佳答案

Porter/Duff 过滤依赖于图像的 alpha channel 。要仅绘制形状边框(没有其他形状空间),您应该将形状背景从白色更改为透明:

<selector>
    <item>
        <shape
            android:shape="rectangle">
            <corners android:bottomLeftRadius="@dimen/logo_radius" android:bottomRightRadius="2dp" android:topLeftRadius="@dimen/logo_radius" android:topRightRadius="2dp"/>
            <stroke
                android:width="4dp"
                android:color="@android:color/black"/>
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>
</selector>

对于这种情况,正确的 PorterDuff.Mode 应该是 SRC_IN

但我不知道为什么椭圆形绘制正确。

更新:

with SRC_IN the border is painted orange, but the filling remains transparent...

您可以像这样将选择器更改为 layer-list 可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/background">
            <shape android:shape="rectangle">
                <corners
                    android:bottomLeftRadius="32dp"
                    android:bottomRightRadius="2dp"
                    android:topLeftRadius="32dp"
                    android:topRightRadius="2dp" />
                <solid android:color="@android:color/white" />
            </shape>
        </item>
        <item android:id="@+id/border">
            <shape android:shape="rectangle">
                <corners
                    android:bottomLeftRadius="32dp"
                    android:bottomRightRadius="2dp"
                    android:topLeftRadius="32dp"
                    android:topRightRadius="2dp" />
                <stroke
                    android:width="4dp"
                    android:color="@android:color/black" />
                <solid android:color="@android:color/transparent" />
            </shape>
        </item>
</layer-list>

只为边框项设置颜色过滤器:

    LayerDrawable background = LayerDrawable.class.cast(findViewById(R.id.target).getBackground());
    background.findDrawableByLayerId(R.id.border).setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_IN);

关于android - Porter-Duff:不同形状的不同行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35399637/

相关文章:

java - 立即选择微调器后禁用编辑文本

android - 在代码中设置android TextViews和Buttons

image-processing - 不规则形状的 OpenCV 质心

jquery - 使用 CSS 创建响应式三 Angular 形

android - ComposeShader中的PorterDuff.Mode.MULTIPLY无法正常工作

Android Canvas 中的橡皮擦无法删除

javascript - Cordova 不支持 Threejs?

java - 多列上的 DISTINCT 查询不起作用 - Google App Engine 数据存储区

shapes - 将(主)形状添加到页面时,在 Microsoft Visio 2013 x64 中将一些(主)形状添加到另一个(主)形状

android - 在填充的 android Canvas 上绘制一个透明圆圈