Drawable in Android is a representation of the image, note that it is an image, not a picture. A picture is a subset of the image. An image can contain a color in addition to a picture.
In other words, drawble is a concept that canvas can draw. The Android system comes with many kinds of drawable. We'd better have a familiar understanding of our own drawable,
This is good for our apk development, because the drawable use is very simple basically is the XML writing. The cost of customizing the view is much lower than your own.
and the non-image type of the drawable occupy the control is also small, help us reduce the size of the APK.
1.BitmapDrawable
The use of a lot of people, do not introduce more, the only thing to note is that the following parameters of a total of 3, 3 kinds of parameters corresponding to what effect to be clear.
1 <?xml version= "1.0" encoding= "Utf-8"? >2 <!--android:tilemode 3 kinds of parameters repeat clamp and mirror to be clear-->3 < Bitmap xmlns:android= "http://schemas.android.com/apk/res/android" 4 android:src= "@mipmap/shuqi" 5 Android : tilemode= "Repeat" >6 7 </bitmap>
Effect of repeat:
Mirror
Clamp effect is not released, because this effect I have not actually used, feel quite 2. Be interested to
2.ShapeDrawable
This familiar person should also be a lot of the rounded corners and horns that we usually use to make Editext background maps. Can save the UI a lot of effort, the programmer can take care of himself
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <shape xmlns:android= "http://schemas.android.com/apk/res/android "3 android:shape=" Rectangle "> 4 <corners android:radius=" 10DP "></corners> 5 < 6 android:angle= "7 android:endcolor=" #FFFF0000 "8 android:startcolor=" #FFFF0000 "/> 9 <padding android:bottom= "10DP" one android:left= "10DP" android:right= "10DP" 13 android:top= "10DP"/>14 <stroke android:width= "2DP" android:color= "# DCDCDC "/>17 <solid android:color=" #ffff9d77 "/>18 </shape>
Effect:
3.layerDrawable
This is not a lot of people, in fact, the back of the item will overwrite the previous item can make that kind of hierarchical display effect. For example, the input box background is doing this.
<?xml version= "1.0" encoding= "Utf-8"?> 2 <layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android "> 3 <item> 4 <shape android:shape=" Rectangle "> 5 <solid android:color=" # 0ac39e "></solid> 6 </shape> 7 </item> 8 9 <item android:bottom=" 6DP ">10 <shape android:shape= "Rectangle" >11 <solid android:color= "#ffffff" ></solid>12 </shape>13 </item>14 <item android:bottom= "1DP" android: left= "1DP" android:right= "1DP" >19 <shape android:shape= "Rectangle" >20 <solid Android:color= "#ffffff" ></solid>21 </shape>22 </item>23 </layer-list>
4.StateListDrawable
This example does not give, we use every day selector is him. React to changes in the view state.
5.LeverListDrawable
This is a simple Java code can be setimagelevel later can automatically switch pictures. Android's power change is done with this,
I write a simple example that you can feel. Suppose our requirement is to switch the picture background every 500ms.
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <level-list xmlns:android= "http://schemas.android.com/apk/res/ Android "> 3 4 <item 5 android:drawable=" @drawable/IC_ACCESSIBLE_BLACK_18DP "6 android: Maxlevel= "0" ></item> 7 <item 8 android:drawable= "@drawable/IC_ACCOUNT_CIRCLE_BLACK_18DP" 9 android:maxlevel= "1" ></item>10 <item11 android:drawable= "@drawable/ic_account_balance _WALLET_BLACK_18DP " android:maxlevel=" 2 "></item>13 <item14 android:drawable=" @ DRAWABLE/IC_ASPECT_RATIO_BLACK_18DP " android:maxlevel=" 3 "></item>16 <item17 android:drawable= "@drawable/ic_book_black_18dp" android:maxlevel= "4" ></item>19 </ Level-list>
Java code:
1 IV = (ImageView) Findviewbyid (R.ID.IV); 2 Iv.setonclicklistener (New View.onclicklistener () {3 4 @Override 5 public void OnClick ( View v) {6 new Thread () {7 8 @Override 9 public void Run () {10 for (int i = 0; i < 5; i++) {one final int II = I;12 try {All sleep (+); catch (Interruptedexcepti On e) {e.printstacktrace (); 16}17 18 Runonuithread (New Runnable () {@Override20 public void Run () {iv.setimagelevel (ii); 22}23 }); if (i = = 4) {26i = -1;27}28}29}30}.start (); 31 }32});
The effect is as follows:
Using this drawable can save us from writing so many set in Java code ... More convenient.
6.TransitionDrawable
This is particularly good, is a gradual gradually into the effect. Very convenient. If you're still in set alpha, it's going to explode.
1 <?xml version= "1.0" encoding= "Utf-8"? >2 <transition xmlns:android= "http://schemas.android.com/apk/res/ Android ">3 <item android:drawable=" @mipmap/shuqi "></item>4 <item android:drawable=" @ Mipmap/shuqi2 "></item>5 </transition>
Java code:
1 IV = (ImageView) Findviewbyid (R.ID.IV); 2 Iv.setonclicklistener (new View.onclicklistener () {3 4 @Override 5 public void OnClick (View v) {6 transitiondrawable td= (transitiondrawable) Iv.getbackground (); 7 td.starttransition (+); 8 // The reverse is also possible 9 //td.reversetransition (+); }11 });
Effect:
7 insetdrawable
Generally want to use this when the background is smaller than the actual area.
View Code
Effect:
8.ScaleDrawable
This is a bit like the level of the level, which can be directly scaled image
1 <?xml version= "1.0" encoding= "Utf-8"? >2 <scale xmlns:android= "http://schemas.android.com/apk/res/android "3 android:drawable=" @mipmap/shuqi "4 android:scaleheight=" 70% "5 android:scalegravity=" Center "6 >7 </scale>
1 IV = (ImageView) Findviewbyid (R.ID.IV); 2 //The following cannot be omitted because the level default is 0 will not show 3 scaledrawable scaledrawable= ( scaledrawable) Iv.getbackground (); 4 scaledrawable.setlevel (1);
9.ClipDrawable
In fact, and leveldrawable scaledrawable are very similar, but this is used to cut drawable.
You can flexibly set the orientation angle of the crop and so on.
The demo will not write the same as above, you can write a try.
Android drawable 9 Introduction to the Seed class