Android GradientDrawable
We will use .9.png to set up background in general.
Besides using png, we can use shape tag to set up background.
GradientDrawable
Static Method
Create xml file in drawable folder.
1 |
|
android:shape = [“rectangle” | “oval” | “line” | “ring”]
The shape type, default type is rectangle, it can change to oval, line or ring.
android:radius
Gradient Color
android:startColor :
android:centerColor :
android:endColor:
android:useLevel
true: no gradient color
false: has gradient color
android:angle
The angle for the gradient, in degrees.
0 is left to right, 90 is bottom to top.
It must be a multiple of 45.
Default is 0.
android:type : gradient style [linear | radial | sweep]
linear (default style)
radial
sweep
android:centerX, android:centerY : The relative position
Value is from 0.0 to 1.0. In this case we set the point in center, so android:centerX and android:centerY are 0.5。
android:gradientRadius
When android:type=radial,it must setup this item, otherwise you will get the XmlPullParserException.
Dynamic Method
Setup shage in xml, it is change to GradientDrawable, not
ShapeDrawable or OvalShape,RoundRectShape and so on.
Source code as follows (Kotlin)
1 | var imageView = findViewById<ImageView>(R.id.photo) |
android:angle is deprecated in Kotlin, you must use GradientDrawable.Orientation.* to implamemnt.
1 | gd.orientation = GradientDrawable.Orientation.TOP_BOTTOM |
RunningTime Update
We can change background by using GradientDrawable at running time.
1 | var imageView = findViewById<ImageView>(R.id.photo) |
Android GradientDrawable
https://nickcarter9.github.io/2018/08/07/2018/2018_08_07-gradientdrawable/