Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

enter image description here

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:

enter image description here

left part is without the filter applied, the right part with the filter.

what I want to get:

enter image description here

What should I do to properly paint the border orange using the Porter-Duff filter? Are there any other options?

like image 560
injecteer Avatar asked Feb 15 '16 00:02

injecteer


Video Answer


1 Answers

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:

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

But I don't know why oval-shape painted correctly.

UPD:

with SRC_IN the border is painted orange, but the filling remains transparent...

You can change your selector to layer-list drawable like this:

<?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);
like image 187
Kirill Avatar answered Oct 27 '22 18:10

Kirill