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:
<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:
<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? Are there any other options?
Porter/Duff filtering depends on image alpha channel. To paint shapes border only (without other shape space) you should change shape background from white to transparent:
and correct
PorterDuff.Mode
for this case should beSRC_IN
.But I don't know why oval-shape painted correctly.
UPD:
You can change your selector to
layer-list
drawable like this:and set color filter only for border item: