View animation and Frame Animation, view animation Frame Animation
Reprinted please indicate the source: http://blog.csdn.net/ZhouLi_CSDN/article/details/45971421
View Animaiton
- Xml files are usually used to define animations, which are easy to read and reuse.
- Xml is written in res/anim/path.
The following is an example:
<set android:shareInterpolator="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:pivotX="50%" android:pivotY="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:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:duration="400" android:fillBefore="false" /> <rotate android:fromDegrees="0" android:toDegrees="-45" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:duration="400" /> </set></set>
Relative TX: "50" for 50% "for" 50% "for 50%" for itself relative to the parent view. Usage:
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);spaceshipImage.startAnimation(hyperspaceJumpAnimation);
Set the animation start time:
Animation. setStartTime (), and then View. setAnimation ().
Note: No matter how the animation moves or the size changes, the view with the animation will not be adjusted to accommodate it, and the view will not be dropped out of its range. When its parent view is exceeded, it is cut.
Drawable Animation
Class: AnimationDrawable
If the xml definition is put under res/drawable/path:
Example:
<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>
- OneShot: Set to true. It only plays the video once and stops at the last frame.
- Set to false for loop playback.
- Usage:
It can be added as the background of a view and then called to start the animation.
- Example:
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);}
Note: The start () method cannot be used in the oncreate method, because the AnimationDrawable has not been associated with the window. If you want the animation to take effect after it is started, you can start () in the onWindowFocusChanged () method of the activity. It is called when android sets your window to focus.