Android_AnimationDrawable introduction and usage

Source: Internet
Author: User

Drawable animation can load Drawable resources to implement frame animation. AnimationDrawable is the basic class for implementing Drawable animations. We recommend that you use the XML file method to implement Drawable animation, which is not recommended in code. This XML file is stored in the res/drawable/directory of the project. The instructions (attributes) of an XML file are the sequence and interval of animation playback.

In the XML file, the <animation-list> element is the root node. The <item> node defines each frame, indicating the frame and frame interval of a drawable resource. The following is an example of an XML file:

[Java]
<Animation-list xmlns: android = "http://schemas.android.com/apk/res/android"
 
Android: oneshot = "true">
 
<Item android: drawable = "@ drawable/rocket_thrust1" android: duration = "200"/>
 
<Item android: drawable = "@ drawable/rocket_thrust2" android: duration = "200"/>
 
<Item android: drawable = "@ drawable/rocket_thrust3" android: duration = "200"/>
 
</Animation-list>
If the Android: oneshot attribute is set to true, the animation is executed only once and remains at the last frame. If it is set to false, the animation is played cyclically. The file can be added as an Image background and played when triggered.

Usage:

Method 1: Drawable Animation itself is a Drawable resource file, so you can directly set it as the background of the specified View in xml. Animation. start ().

Method 2: View. setBackgroundResource (resID). animation. start ().

The following is an example:

[Java]
AnimationDrawable rocketAnimation;
 
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
ImageView rocketImage = (ImageView) findViewById (R. id. rocket_image );
RocketImage. setBackgroundResource (R. drawable. rocket_thrust); // The XML file defined by roket_trust
RocketAnimation = (AnimationDrawable) rocketImage. getBackground ();
 
}
 
Public boolean onTouchEvent (MotionEvent event ){
 
If (event. getAction () = MotionEvent. ACTION_DOWN ){
RocketAnimation. start ();
Return true;
}
 
Return super. onTouchEvent (event );
 
}
Note: Once Drawable Animation is set for a specified View, its BackGround becomes an AnimationDrawable object. The Code is as follows: rocketAnimation = (AnimationDrawable) rocketImage. getBackground ();

The start () method cannot be called in the onCreate () function. Because the AnimationDrawable is not fully associated with the Window, the View is not displayed in the onCreate () method (similarly, the width and height of a View are measured in this method, and the value 0 is often obtained. Also, SurfaceHolder needs to add the Callback method ). To start the animation as soon as possible, use the onWindowFoucsChanged () Listener method ().

More: suddenly, the component width and height cannot be obtained because the component is not fully associated with the Window test: In this listening method, the width and height of the specified component (TextView) are obtained.

The Xml file is as follows:

[Html]
<TextView
 
Android: id = "@ + id/textView"
 
Android: layout_width = "50dip"
 
Android: layout_height = "100dip"
 
Android: text = "@ string/special_character"/>
The Code is as follows:

[Java]
@ Override
 
Public void onWindowFocusChanged (boolean hasFocus ){
 
// TODO Auto-generated method stub
Super. onWindowFocusChanged (hasFocus );
 
SpecialCharacterStr = (String) mTextView. getText ();
Log. d ("special_character", "specialCharacterStr is:" + specialCharacterStr );
 
Int width = mTextView. getMeasuredWidth ();
Int height = mTextView. getMeasuredHeight ();
 
Log. d ("window_focus", "textview width is:" + width );
Log. d ("window_focus", "textview height is:" + height );
}
 
 
 
 
You can obtain the width and height. That is, only when the View is fully associated with the Window can you obtain the width and height of the View and set the background for the View.
 
AnimationDrawable: android. graphic. drawable. AnimationDrawable
 
// Obtain the xml-defined AnimationDrawable
 
AnimDrawable = (AnimationDrawable) getResources (). getDrawable (R. anim. frame_animation );
 
Reference code:
 
@ Override
 
Publicvoid onWindowFocusChanged (boolean hasFocus ){
 
// TODO Auto-generated method stub
 
If (hasFocus ){
 
ImageView. setBackgroundResource (R. anim. frame_animation );
 
AnimDrawable = (AnimationDrawable) imageView. getBackground ();
 
AnimDrawable. start ();
 
AlphaAnimation aas = new AlphaAnimation (0.1f, 1.0f );
 
// Set the animation duration
 
Aas. setDuration (3500 );
 
// Start the animation
 
ImageView. startAnimation (aas );
 
// Set an animation listener
 
Aas. setAnimationListener (new AnimationListener ()
 
{
 
@ Override
 
Publicvoid onAnimationEnd (Animation arg0 ){
 
// Stop Frame Animation
 
ImageView. setVisibility (View. GONE );
 
Log. I (TAG, "FY_stop ");
 
AnimDrawable. stop ();
 
}
 
@ Override
 
Publicvoid onAnimationRepeat (Animation animation ){
 
}
 
@ Override
 
Publicvoid onAnimationStart (Animation animation ){
 
// Set the background of the imageView to an animation.
 
ImageView. setBackgroundResource (R. anim. frame_animation );
 
AnimDrawable = (AnimationDrawable) imageView. getBackground ();
 
// Sets the animation transparency.
 
AnimDrawable. setAlpha (80 );
 
// Start the animation
 
AnimDrawable. start ();
 
}
 
}
 
);
 
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.