Viktor
Viktor

Reputation: 326

Set gradient on stroke android

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>

Upvotes: 12

Views: 21891

Answers (2)

pckill
pckill

Reputation: 3759

If you need inner color to be transparent, then you can use a ring shape:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:thickness="2dp"
    android:useLevel="false">
    <gradient
        android:startColor="@color/sea_green"
        android:endColor="@color/black"
        android:angle="270" />
</shape>

enter image description here

Upvotes: 6

Fr099y
Fr099y

Reputation: 774

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

Upvotes: 34

Related Questions