Android Animation translate (displacement animation)

Source: Internet
Author: User

Activity switching effect using Android animation effect, Android animation in the official relevant information: http://developer.android.com/guide/topics/graphics/ Animation.html and Http://developer.android.com/guide/topics/resources/animation-resource.html, Activity's toggle animation is actually the Tween Animation effect in Android's view Animation, Tween Animation is divided into 4 animation effects, namely: alpha (transparent change) translate (position Move) scale (zoom) rotate (rotation), while the left and right slide switch uses the effect of translate (position movement), in the next we will discuss the alpha (transparent change), scale (scaling), rotate (rotation) Three effects, this article only Translate (position movement).

Translate animations are very well understood, which is to define a starting position and an end position, define the move time, and then automatically create a motion animation. The translate moving direction of Android has horizontal (X) vertical (y), left and right sliding using the lateral movement effect, for vertical (Y) position as follows:

The animation definition file that defines an upward exit animation (from position 2 to position 3) and entry (from position 1 to move position 2) is as follows:

Out_to_up.xml (exit from top of screen)

<?xml version="1.0" encoding="Utf-8"?>

<translate xmlns:android="Http://schemas.android.com/apk/res/android"

Android:interpolator="@android: Anim/accelerate_interpolator"

Android:fromydelta="0%p"

android:toydelta=" -100%p"

android:duration=">"

</translate>

In_from_down.xml (enter from below the screen)

<?xml version="1.0" encoding="Utf-8"?>

<translate xmlns:android="Http://schemas.android.com/apk/res/android"

Android:interpolator="@android: Anim/accelerate_interpolator"

Android:fromydelta="100%p"

Android:toydelta="0%p"

android:duration=">"

</translate>

Next, let's talk about several important attributes of translate:

Android:interpolator: Accelerators, very useful properties, can be simply understood as the speed of the animation, can be more and more fast, or it can be more and more slow, or is fast after the busy, or even the speed, and so on, for the values as follows:

@android: anim/accelerate_interpolator: Getting faster

@android: anim/decelerate_interpolator: Getting slower

@android: Anim/accelerate_decelerate_interpolator: Fast and slow first

@android: anim/anticipate_interpolator: Take a small step back and speed it forward.

@android: Anim/overshoot_interpolator: Quickly reach the end of a small step and return to the end

@android: Anim/anticipate_overshoot_interpolator: Reach the end of a small step and return to the end

@android: Anim/bounce_interpolator: Reach the end to produce a pinball effect and bounce back to the end

@android: anim/linear_interpolator: Uniform speed.

Android:duration: Animation run time, defined in multiple time (MS) to complete the animation

Android:startoffset: Run animations after a certain amount of delay

Fromxdelta:x axis direction Start position, can be%, also can be specific pixels specific see figure

Toxdelta:x axis end position, either% or specific pixel

Fromydelta:y axis direction Start position, can be%, can also be specific pixels

Toydelta:y axis end position, either% or specific pixel

Once you understand the above attributes, you can combine a lot of interesting position movement effects. For example, using an accelerator: @android: The anim/bounce_interpolator can produce the effect of a bouncing ball when it lands.

In the actual project you need to use only "left and right swipe" effect may not be good enough, I hope that the switch can produce more dynamic effect, such as the beginning of the switch when the 1th activity first step back, and then left to exit the screen. The 2nd activity is then followed by the right-hand entry screen, which has a dynamic effect when it reaches the end point. After understanding the animation accelerator, we all know that can be used: Anticipate_overshoot_interpolator, Anticipate_interpolator, Overshoot_ Interpolator These three accelerators realize the dynamic effect of starting or ending when the left and right slide switch.

The animation file is defined as follows:

Dync_out_to_left.xml

<?xml version="1.0" encoding="Utf-8"?>

<translate xmlns:android="Http://schemas.android.com/apk/res/android"

android:duration="

Android:fromxdelta="0%p"

Android:interpolator="@android: Anim/anticipate_interpolator"

Android:toxdelta=" -100%p" />

Dync_in_from_right.xml

<?xml version="1.0" encoding="Utf-8"?>

<translate xmlns:android="Http://schemas.android.com/apk/res/android"

android:duration="

Android:fromxdelta="100%p"

Android:interpolator="@android: Anim/anticipate_overshoot_interpolator"

Android:toxdelta="0%p" />

However, with the above animation configuration, the following issues occur:

1. When the 1th activity uses anticipate_interpolator, it moves back one step at a time before moving forward. But to step back is too big, slightly exaggerated, the actual application only hope to step back a little steps on the line.

2. Since the beginning of the step backward, the additional move time, resulting in the previous activity and the last activity time is not synchronized.

To solve the above problem, redefine the dynamic effect by dividing the movement of the 1th activity into 2 animation effects: (1) Move the 2%p position backward by 200 milliseconds (2) and move forward from the 2%p position to the -100%p position after a delay of 200 milliseconds. The 2nd activity then moves from the 102%p position to the 0%p position from a delay of 200 milliseconds.

The animation is defined as follows:

New_dync_out_to_left.xml

<?xml version="1.0" encoding="Utf-8"?>

<set xmlns:android="Http://schemas.android.com/apk/res/android"

Android:shareinterpolator="false" >

<translate

android:duration="

Android:fromxdelta="0%p"

Android:interpolator="@android: Anim/accelerate_decelerate_interpolator"

Android:toxdelta="2%p" />

<translate

android:duration="+"

Android:fromxdelta="2%p"

Android:interpolator="@android: Anim/accelerate_interpolator"

android:startoffset="

Android:toxdelta=" -100%p" />

</set>

New_dync_in_from_right.xml

<?xml version="1.0" encoding="Utf-8"?>

<translate xmlns:android="Http://schemas.android.com/apk/res/android"

android:duration="+"

Android:fromxdelta="102%p"

Android:interpolator="@android: Anim/anticipate_overshoot_interpolator"

android:startoffset="

Android:toxdelta="0%p" />

Package code: http://www.oschina.net/code/snippet_97118_7734

Android Animation translate (displacement animation)

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.