This time we mainly explain the various drawable under the Android platform, here in the SDK Android.graphics.drawable package can see a variety of drawable classes as many as more than 10 kinds, what is the relationship between them and the difference ?
First, animationdrawable
As the name suggests, this class mainly represents animation graphics classes, you can achieve the effect of frame play, the following code shows such as
1. Define a cwj_animation.xml to be placed in the Res/drawable directory, where the defined attribute duration is a delay, in milliseconds, and the OneShot property indicates whether to play only once and the content is:
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/
-->1
2
3
4
5
6
7
8animation-list>
9
10
2. Calling in Java is also simple
ImageView img = (imageview) Findviewbyid (r.id.cwj_image); First declare a ImageView object in the XML layout file
Img.setbackgroundresource (r.drawable.cwj_animation); The XML file that we just animation defined
Animationdrawable frameanimation = (animationdrawable) img.getbackground (); Constructing Animationdrawable objects
Frameanimation.start ()//start to play animation
3. The Animationdrawable class also provides some common methods as follows:
void Stop () stop
void Addframe (drawable frame, int duration) Adds a frame, similar to the layout in XML
drawable getframe (int index) returns the drawable graphic of a frame
int Getnumberofframes () returns the total number of animation frames
Boolean isoneshot () play only once
Whether the Boolean isrunning () is playing
Second, bitmapdrawable
The bitmap object for scaling and morphing in the Android platform is represented by the Bitmapdrawable class, and its construction method is simple, Because this class inherits from Android.graphics.drawable.Drawable, the relative drawable provides more information about how bitmaps are manipulated, and the main construction methods are as follows:
Bitmapdrawable ()//directly constructs an empty object, which is not recommended, and the SDK is labeled deprecated. May not be used in the future.
Bitmapdrawable (Resources Res)//constructed from a resource
Bitmapdrawable (Bitmap Bitmap)//is constructed directly from the Bitmap object, but is not recommended, but wants to use the next
Bitmapdrawable (Resources res, Bitmap Bitmap)//create set initial resolution from Bitmap from Res
Bitmapdrawable (String filepath)//is constructed from a specific file path and is not recommended, but is the next better
Bitmapdrawable (Resources res, String filepath)/IBID.
Bitmapdrawable (InputStream is)/is constructed from the input stream, the following method is also recommended
Bitmapdrawable (Resources Res, InputStream is)//IBID.
In the Bitmapdrawable class, relative to the Drawable class mainly add the following methods, are more practical:
Final Bitmap Getbitmap () gets a Bitmap object
int getopacity ()//Gain Transparency
void Setantialias (Boolean AA)//anti-aliasing
void Settargetdensity (Canvas Canvas)//Set Target Canvas density
void Settargetdensity (Displaymetrics metrics)
Third, clipdrawable
Colordrawable
Drawable
Gradientdrawable
Insetdrawable
Layerdrawable
Levellistdrawable
Ninepatchdrawable
Paintdrawable
Picturedrawable
Rotatedrawable
Scaledrawable
Shapedrawable
Statelistdrawable
Transitiondrawable
The above types are generally less common in development, mainly based on the use of the base class, the Android interior of the multiple widget base control used, interested users can view open source git in the relevant content.