Porter-Duff:不同形状的不同行为?
Porter-Duff: different behavior for different shapes?
我有以下布局:
<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>
结果如下:
左边没有应用滤镜,右边有滤镜。
我想得到的:
我应该怎么做才能使用 Porter-Duff 滤镜正确绘制橙色边框?还有其他选择吗?
Porter/Duff 过滤取决于图像 alpha 通道。要仅绘制形状边框(没有其他形状 space),您应该将形状背景从白色更改为透明:
<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);
我有以下布局:
<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>
结果如下:
左边没有应用滤镜,右边有滤镜。
我想得到的:
我应该怎么做才能使用 Porter-Duff 滤镜正确绘制橙色边框?还有其他选择吗?
Porter/Duff 过滤取决于图像 alpha 通道。要仅绘制形状边框(没有其他形状 space),您应该将形状背景从白色更改为透明:
<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);