【问题标题】:Porter-Duff: different behavior for different shapes?Porter-Duff:不同形状的不同行为?
【发布时间】:2016-05-25 18:55:57
【问题描述】:

我有以下布局:

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

还有以下myShapedrawable:

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

结果如下:

左边部分没有应用过滤器,右边部分有过滤器。

我想得到什么:

我应该怎么做才能使用 Porter-Duff 过滤器正确地将边框涂成橙色?还有其他选择吗?

【问题讨论】:

  • 按下时会变成橙色吗?
  • 不,边框应始终保持橙色。
  • 为什么不直接使用橙色而不是@android:color/black?
  • 因为它必须像皮肤一样以编程方式设置
  • 也许吧。我想将形状保留在 xml 中。我最初的问题是,为什么oval 有效,而rectangle 无效?

标签: android shape porter-duff


【解决方案1】:

Porter/Duff 过滤依赖于图像 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

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

UPD:

使用 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 的边框被涂成橙色,但填充保持透明...
猜你喜欢
  • 2012-05-02
  • 2013-07-02
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
相关资源
最近更新 更多