泊特-达夫算法:不同形状有不同的行为表现?

13

我有以下布局:

            <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 );

结果看起来像这样:

输入图像描述

然后我将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滤镜正确地将边框涂成橙色? 还有其他选项吗?


按下时它应该变成橙色吗? - Eugen Pechanec
不,边框应始终保持橙色。 - injecteer
2
也许吧。我想保留XML中的形状。我的原始问题是,为什么“椭圆形”可以工作,而“矩形”不行? - injecteer
你刚刚尝试了每一种波特-达夫模式吗?例如,我认为有一种模式可以替换黑色并忽略白色。 - David Medenjak
@DavidMedenjak,目前最好的结果是使用MULTIPLY,边框颜色保持为黑色,填充颜色变为橙色。 - injecteer
显示剩余4条评论
1个回答

6

波特/达夫滤镜依赖于图像的 alpha 通道。如果您只想绘制形状的边框(而不是形状的其他部分),则应将形状的背景从白色更改为透明:

<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

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

更新:

使用SRC_IN,边框将被绘制为橙色,但填充部分仍将保持透明...

你可以将选择器更改为像这样的layer-list drawable:

<?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);

使用 SRC_IN 时,边框被涂成橙色,但填充仍保持透明... - injecteer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接