View/drawable animation

Source: Internet
Author: User

Whether it is an xml animation or an android code animation, you can define an animation for continuous playback. we recommend that you use xml file animation because it is easier to complete, reuse, and modify. place the xml animation file in the res/anim folder. this file must have a heel element (<alpha/scale/translate/rotateinterpolator element/set>). By default, all animations are simultaneously executed to allow them to perform one by one, you can define the startoffset attribute for control, just like the following code:

[Java] <SPAN style = "FONT-SIZE: 16px" data-mce-style = "font-size: 16px;"> <set android: Using interpolator = "false">
<Scale
Android: interpolator = "@ android: anim/accelerate_decelerate_interpolator"
Android: fromXScale = "1.0"
Android: toXScale = "1.4"
Android: fromYScale = "1.0"
Android: toYScale = "0.6"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: fillAfter = "false"
Android: duration = "700"/>
<Set android: interpolator = "@ android: anim/decelerate_interpolator">
<Scale
Android: fromXScale = "1.4"
Android: toXScale = "0.0"
Android: fromYScale = "0.6"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: startOffset = "700"
Android: duration= "400"
Android: fillBefore = "false"/>
<Rotate
Android: fromDegrees = "0"
Android: toDegrees = "-45"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: startOffset = "700"
Android: duration = "400"/>
</Set>
</Set> </SPAN>

<Set android: Using interpolator = "false">
<Scale
Android: interpolator = "@ android: anim/accelerate_decelerate_interpolator"
Android: fromXScale = "1.0"
Android: toXScale = "1.4"
Android: fromYScale = "1.0"
Android: toYScale = "0.6"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: fillAfter = "false"
Android: duration = "700"/>
<Set android: interpolator = "@ android: anim/decelerate_interpolator">
<Scale
Android: fromXScale = "1.4"
Android: toXScale = "0.0"
Android: fromYScale = "0.6"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: startOffset = "700"
Android: duration= "400"
Android: fillBefore = "false"/>
<Rotate
Android: fromDegrees = "0"
Android: toDegrees = "-45"
Android: toYScale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: startOffset = "700"
Android: duration = "400"/>
</Set>
</Set> the screen coordinates are (0, 0) in the upper left corner, and are increased from top to right. there are some values. For example, when itx can be assigned a relative value, such as 50%, it means 50% relative to itself. 50 indicates 50% of the parent control. you can also define the Interpolator, that is, the speed insertor, which is described in detail in the previous article property animation.

 

[Java] ImageView spaceshipImage = (ImageView) findViewById (R. id. spaceshipImage );
Animation hyperspaceJumpAnimation = AnimationUtils. loadAnimation (this, R. anim. hyperspace_jump );
SpaceshipImage. startAnimation (hyperspaceJumpAnimation );

ImageView spaceshipImage = (ImageView) findViewById (R. id. spaceshipImage );
Animation hyperspaceJumpAnimation = AnimationUtils. loadAnimation (this, R. anim. hyperspace_jump );
SpaceshipImage. startAnimation (hyperspaceJumpAnimation );

You can also define the animation directly without calling the startAnimation function: animation. setStarttime (). After that, the animation is automatically executed.

 

Drawable Animation can help you play Drawable resources one by one, just like traditional animations. The AnimationDrawable class is the foundation of Drawable animation.

Drawable Animation is an Animation, but it is still composed of Drawable resources. Therefore, its xml file is generally placed in res/drawable, the xml file consists of the <animation-list> element and its root node. <item> the framework is composed. The following is an example:

[Java] <SPAN style = "FONT-SIZE: 16px" data-mce-style = "font-size: 16px;"> <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> </SPAN>

<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>

Android: oneshot = "true" indicates that it only plays the video once and stops at the last drawable. When oneshot is set to false, it will play the video continuously. the following is an example. When the screen is touched, the animation will be loaded on this imageview and run [java] view plaincopyprint? <SPAN style = "FONT-SIZE: 16px" data-mce-style = "font-size: 16px;"> 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 );
RocketAnimation = (AnimationDrawable) rocketImage. getBackground ();
}
Public boolean onTouchEvent (MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_DOWN ){
RocketAnimation. start ();
Return true;
}
Return super. onTouchEvent (event );
} </SPAN>

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 );
RocketAnimation = (AnimationDrawable) rocketImage. getBackground ();
}
Public boolean onTouchEvent (MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_DOWN ){
RocketAnimation. start ();
Return true;
}
Return super. onTouchEvent (event );
} In the above example, we can see that the call of method start () cannot be called in the onCreate () method, because the view to be animated has not been added to the window, if you want to run the animation immediately, you can rewrite the onWindowfocuschanged () method to automatically execute the activity when it is placed at the frontend.

 

Related Article

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.