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
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>
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).
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.
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.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With