Android UI development topic (3) Various Drawable
This section describes the various Drawable types on the Android platform. graphics. under the drawable package, we can see that there are more than a dozen Drawable classes. What are the relationships and differences between them?
I. AnimationDrawable
As the name suggests, this class mainly represents the animation graphics class, which can achieve frame-by-frame playback. The following code example is as follows:
1. Define a cwj_animation.xml file to be placed in the res/drawable directory. The defined attribute duration is latency, in milliseconds, and the oneshot attribute indicates whether to play only once. 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 very simple.
ImageView img = (ImageView) findViewById (R. id. cwj_image); // first declare an ImageView object in the xml layout File
Img. setBackgroundResource (R. drawable. cwj_animation); // xml file defined by the animation
AnimationDrawable frameAnimation = (AnimationDrawable) img. getBackground (); // constructs the AnimationDrawable object
FrameAnimation. start () // start playing the 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 image of a frame.
Int getNumberOfFrames () returns the total number of animation frames.
Boolean isOneShot () Whether to play only once
Boolean isRunning () is playing?
Ii. BitmapDrawable
In the Android platform, BitmapDrawable class represents the scaled and deformed Bitmap objects, and its construction method is also very simple, because this class inherits from android. graphics. drawable. drawable provides more bitmap operations than Drawable. The main construction method is as follows:
BitmapDrawable () // construct an empty object directly. This method is not recommended and the SDK is marked as deprecated. It may not be available in the future.
BitmapDrawable (Resources res) // construct from the Resource
BitmapDrawable (Bitmap bitmap) // construct a Bitmap object directly, but it is not recommended.
BitmapDrawable (Resources res, Bitmap bitmap) // create an initial resolution from bitmap from res
BitmapDrawable (String filepath) // It is constructed from a specific file path and is not recommended, but a better one.
BitmapDrawable (Resources res, String filepath) // same as above
BitmapDrawable (InputStream is) // constructed from the input stream. The following method is also recommended.
BitmapDrawable (Resources res, InputStream is) // same as above
Compared with the Drawable class, BitmapDrawable mainly adds the following methods, which are more practical:
Final Bitmap getBitmap () gets a Bitmap object
Int getOpacity () // gets the transparency
Void setAntiAlias (boolean aa) // specifies whether the image is anti-aliasing.
Void setTargetDensity (Canvas canvas) // sets the target Canvas density.
Void setTargetDensity (DisplayMetrics metrics)
3. ClipDrawable
ColorDrawable
Drawable
GradientDrawable
InsetDrawable
LayerDrawable
LevelListDrawable
NinePatchDrawable
PaintDrawable
PictureDrawable
RotateDrawable
ScaleDrawable
ShapeDrawable
StateListDrawable
TransitionDrawable
The above types are rarely used in common development, mainly for the construction and use of base classes, and Multiple widgets in Android are used, interested users can view related content in open-source GIT.