[英]Porter-Duff: different behavior for different shapes?
I have the following layout: 我有以下布局:
<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>
and the following myShape
drawable: 和以下myShape
drawable:
<selector>
<item><shape android:shape="oval">
<stroke android:color="@android:color/black" android:width="4dp"/>
<solid android:color="@android:color/white" />
</shape></item>
</selector>
I applied the following filter: 我应用了以下过滤器:
myButton.getBackground().setColorFilter( orange, PorterDuff.Mode.ADD );
the result looked that way: 结果看起来那样:
Then I changed myShape to be a rectangle with rounded corners: 然后我将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>
the result looked like: 结果看起来像:
left part is without the filter applied, the right part with the filter. 左边部分没有应用过滤器,右边部分没有过滤器。
what I want to get: 我想得到什么:
What should I do to properly paint the border orange using the Porter-Duff filter? 如何使用Porter-Duff过滤器正确绘制边框橙色? Are there any other options? 还有其他选择吗?
Porter/Duff filtering depends on image alpha channel. Porter / Duff过滤取决于图像alpha通道。 To paint shapes border only (without other shape space) you should change shape background from white to transparent: 要仅绘制形状边框(没有其他形状空间),您应该将形状背景从白色更改为透明:
<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>
and correct PorterDuff.Mode
for this case should be SRC_IN
. 对于这种情况,正确的PorterDuff.Mode
应该是SRC_IN
。
But I don't know why oval-shape painted correctly. 但我不知道为什么椭圆形正确涂漆。
UPD: UPD:
with SRC_IN the border is painted orange, but the filling remains transparent... 使用SRC_IN边框涂成橙色,但填充物保持透明...
You can change your selector to layer-list
drawable like this: 您可以将选择器更改为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>
and set color filter only for border item: 并为边框项设置颜色过滤器:
LayerDrawable background = LayerDrawable.class.cast(findViewById(R.id.target).getBackground());
background.findDrawableByLayerId(R.id.border).setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_IN);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.