View animation for Android animation, androidview

Source: Internet
Author: User

View animation for Android animation, androidview

In order to make your use more comfortable, it is necessary to use animation in some cases. Before Android 3.0, two types of animation Tween and Frame Animation were supported.Tween animation supports Simple translation, scaling, rotation, gradient, and Frame animations, just like Gif images, which simulate the animation effect through a series of images, property animation was introduced after Android 3.0. Android shares a simple and interesting animation effect that uses property animation.


Today, we mainly want to learn about Tween animation, that is, View animation.

View animation can only be applied to View objects, and only some attributes are supported. For View animation, it only changes the position of the View object, but does not change the View object itself, for example, the coordinate of a button is (200,200) to be moved to (200,500) through the Pan animation, but the button after you click the move button does not have any effect, for example:


Now that we know the basic usage of View animation, we can use java code or xml to write the animation.

1. Pan Animation

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="3000"    android:fillAfter="true"    android:fromXDelta="0%"    android:fromYDelta="0%"    android:repeatCount="10"    android:repeatMode="restart"    android:toXDelta="0%"    android:toYDelta="70%" ></translate>
Duration animation time. fillAfter maintains the state after the animation ends. fromXDelta starts from X position, fromYDelta starts from Y position, repeatCount repeatMode repeats, restart in repeatMode is in reverse order, and is used in java code.

Animation animation = AnimationUtils. loadAnimation (this, R. anim. tran_btn );

View. startAnimation (animation );


<span style="font-size:18px;">Animation animation2 = new TranslateAnimation(0, 20, 0, 0);animation2.setDuration(2000);animation2.setRepeatCount(10);animation2.setRepeatMode(Animation.RESTART);button.startAnimation(animation2);</span>
New TranslateAnimation (fromXDelta, toXDelta, fromYDelta, toYDelta)
I believe everyone knows what the parameters mean.


2. Rotating Animation

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="3000"    android:fromDegrees="0"    android:toDegrees="360"     android:pivotX="50%"    android:pivotY="50%"    ></rotate>

Roughly the same as above, the center of the rotate.

Animation animation2 = new RotateAnimation(0, 360, 0, 0);animation2.setDuration(2000);button.startAnimation(animation2);



3. Gradient Animation

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:fromAlpha="1.0"    android:toAlpha="0.0" ></alpha></span>
Actually, we can see that all the rest is via analogy.
<span style="font-size:18px;">Animation animation = new AlphaAnimation(1, 0);animation.setDuration(3000);button.startAnimation(animation);</span>


4. Zoom Animation

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:fromXScale="0.0"    android:fromYScale="1.0"    android:pivotX="50%"    android:pivotY="50%"    android:toXScale="1.0"    android:toYScale="1.0" ></scale>



Okay, the four basic animations are all simple, but sometimes we may have some special requirements, such as playing a group of animations. Now we can use the set

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <rotate        android:duration="2000"        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:toDegrees="360" />    <alpha        android:duration="2000"        android:fromAlpha="1.0"        android:toAlpha="0.0" /></set>

This is to rotate the gradient and play the video at the same time. If you play the video in sequence, you only need to add startOffset.

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <rotate        android:duration="2000"        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:toDegrees="360" />    <alpha        android:duration="2000"        android:fromAlpha="1.0"        android:startOffset="2000"        android:toAlpha="0.0" /></set>

In this way, you can play the video in sequence, but sometimes we want special effects such as the acceleration of the animation. At this time, we can use interpolator, animation. setInterpolator (new AccelerateInterpolator ());

The AccelerateDecelerateInterpolator speed changes slowly between the animation start and end, accelerating

AccelerateInterpolator changes slowly at the beginning of the animation, and then starts acceleration.

AnticipateInterpolator starts and then throws backward.

AnticipateOvershootInterpolator returns the final value after a certain value is pushed forward.

When the BounceInterpolator animation ends

CycleInterpolator: specifies the number of times the animation is played cyclically, and the speed changes along the sine curve.

DecelerateInterpolator is fast and slow at the beginning of the animation

LinearInterpolator changes at constant rate

OvershootInterpolator throws a value forward and returns to its original position.


Thank you for your patience. You can leave a message if you have any questions ..







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.