How To Use Android Gradientdrawable
Solution 1:
use this xml as background to the imageview.
<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"><gradientandroid:angle="90"android:startColor="#7c0000"android:endColor="#A71C1C"/></shape>
thats it.
Solution 2:
I will give the same answer as Praveen, but will try to explain the settings as well.
<shapexmlns:android="http://schemas.android.com/apk/res/android"><gradientandroid:type="linear"android:angle="-90"android:startColor="#7c0000"android:endColor="#A71C1C" /></shape>
android:type
There are 3 types of gradients, the default and the one for this question is "linear". The other 2 are "radial" and "sweep".
android:angle
Counter-clockwise rotation of the gradient, where 0 is | start color --> end color | (horizontally).
android:startColor
Color the gradient starts from, start is defined by the rotation.
android:endColor
Color the gradient ends with, end is defined by the rotation.
android:centerColor
There can also be a color in between the start and end color, if desired.
Solution 3:
Code
I originally found this question because I wanted to do it in code. Here is how to do it programmatically.
intstartColor=0xfff6ee19; // yellowintendColor=0xff115ede; // blueGradientDrawablegradientDrawable=newGradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
newint[] {startColor, endColor});
ViewmyView= findViewById(R.id.my_view);
myView.setBackgroundDrawable(gradientDrawable);
The different orientations in the image at the top can be achieved by changing the Orientation
in the constructor.
XML
As already answered, this is how you do it in xml.
my_gradient_drawable.xml:
<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"><gradientandroid:type="linear"android:angle="0"android:startColor="#f6ee19"android:endColor="#115ede" /></shape>
You set it to the background of some view. For example:
<Viewandroid:layout_width="200dp"android:layout_height="100dp"android:background="@drawable/my_gradient_drawable"/>
Post a Comment for "How To Use Android Gradientdrawable"