Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 787
Viktor Avatar asked Aug 07 '17 09:08

Viktor


People also ask

What is angle in gradient Android?

Gradient basically represents the variation in space(in a direction) of any quantity. With color it represents the variation of color intensity in a direction represented by angle. Here are some diagrams to represent this concept: Here the figure shows the color variation in horizontal direction (angle is set 0).

What is gradient in Android mobile application development?

Gradient refers to the mysterious and romantic color transition of an object from bright to dark, deep to shallow, or from one color to another.

What is the stroke in Android?

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.


2 Answers

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

like image 64
pckill Avatar answered Sep 18 '22 09:09

pckill


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

like image 38
Fr099y Avatar answered Sep 17 '22 09:09

Fr099y