Objective
Hello everyone, to bring you Android开发工程师文集-1 小时学会各种Drawable
an overview, I hope you like
drawable Introduction
Drawable is an abstract class, where drawable differs from view in that it embodies events and interactivity.
View is a user-oriented, visible control that can add a click event to it. Drawable on the contrary, cannot be oriented, nor visible, nor can you add click events.
Drawable have a lot of subclasses, drawable are abstract classes, so there are many subclasses to inherit it.
Main principles and methods
Bitmapdrawable Batme drawable
The subclass is a bitmap wrapper that preserves the original picture size, padding, and stretching.
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/ic_launcher" android:tileMode="mirror"//平铺 android:antialias="true"//消除锯齿 android:dither="true"></bitmap>
Layerdrawable Le drawable
This subclass is to manage a set of drawable, the first picture of the placement, then the second picture will be on the first picture, and then placed in turn.
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:top="40dp" android:left="40dp" android:src="@drawable/picture_1" android:gravity="center"> </item> <item android:top="40dp" android:left="40dp" android:src="@drawable/picture_2" android:gravity="center"> </item></layer-list>
Statelistdrawable
This class of statelistdrawable provides different images depending on the state.
<?xml version="1.0" encoding="utr-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:drawable="@drawable/botton_1"/> <item android:state_pressed="true" android:drawable="@drawable/botton_2"/> <item android:state_selected="true" android="@drawable/botton_3"/> <item android:drawable="@drawable/botton_4"/></selector>
Levellistdrawable
The subclass has different item-setlevel ();
<level-list xmlns:android="http://schemas.android.om/apk/res/android"> <item android:drawable="@drawable/off" android:minLevel="5" android:maxLevel="10"> </item> <item android:drawable="@drawable/on" android:minLevel="10" android:maxLevel="20"> </item></level-list>
public void On(View v){ iv.setImageLevel( //int );}
transitiondrawable---transi tion drawable
For Layerdrawable subclasses, only two picture changes, no multiple pictures, layerdrawable can have multiple item, and transitiondrawable only has two item.
<transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/off"> </item> <item android:drawable="@drawable/on"> </item></transition>
public void On(View v){ TransitionDrawable drawable=iv.getDrawable(); //开启 startTransition(); drawable.startTransition(2000);}public void Off(View v){ TranstionDrawable drawable=iv.getDrawable(); //关闭 reverseTransition(); drawable.reverseTransition(2000);}
Insertdrawable
Insertdrawable represents an embedded picture ==padding
<inset xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/background" android:insetLeft="50dp" android:insetRight="50dp" android:insetTop="50dp" android:insetBottom="50dp"></inset>
Clipdrawable can be tired drawable
The Clipdrawabel is a form of clipping, like a progress bar.
<clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/picture" android:clipOrientation="horizontal" android:gravity="left"/>
ClipDrawable drawable=iv.getDrawable();drawable.setLevel(5000);//范围0-10000;
DIY drawable
Try it yourself.
Comb:
Bitmapdrawable,
Layerdrawable,
Statelistdreawable,
Levellistdrawable,
Transitiondrawable,
Insertdrawable,
Clipdrawable.
Summarize
- This article speaks about the Android development engineer Anthology-1 hours to learn all kinds of drawable, and if you have a better understanding, welcome to communicate
- Positioning: Sharing
Android
& Java
knowledge points, interested to continue to follow
Android Development Engineer Anthology-1 hours to learn various drawable