Set gradient on stroke android

Viktor :

I have a circular drawable on which I set a 8dp white stroke, like this:

    circleImage = (ImageView) rootView.findViewById(R.id.image);
    circleImage.setOnClickListener(clickListener);
    drawable = (GradientDrawable) circleImage.getBackground();
    drawable.setStroke(8, getResources().getColor(R.color.colorWhite));

The XML for circleImage looks like this:

 <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerInParent="true"
            android:id="@+id/image"
            android:background="@drawable/roundcorner"
            android:clickable="true"/>

What I want to do now is to change the drawable.setStroke to include a gradient color like this

1

I suppose the easiest way to achieve this is to write something in the drawable XML, but I don't quite know how to achieve this.

roundcorner.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle" >
        <corners
            android:topLeftRadius="100dp"
            android:topRightRadius="100dp"
            android:bottomLeftRadius="100dp"
            android:bottomRightRadius="100dp"
            />
        <padding
            android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp"
            />
        <size
            android:width="100dp"
            android:height="100dp"
            />
    </shape>
Fr099y :

You should do something like this. Use layer-list with 2 shapes. First one is for gradient stroke and second one is for solid.

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval" >
            <gradient
                android:angle="360"
                android:startColor="#543456"
                android:endColor="#ff00b5"
                android:type="linear" />
            <size android:width="24dp"
                  android:height="24dp"/>
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">
        <shape android:shape="oval" >
            <solid android:color="#fff" />
        </shape>
    </item>

</layer-list>

This code looks like this enter image description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=470818&siteId=1