Android mobile view animation Problems

Source: Internet
Author: User
It is not an ordinary trouble to write an animation for Android. After searching for it online for a long time, I finally solved the animation problem and summarized the record to encourage everyone. The vertical direction is similar.

 

Complete animation function code: 

1 Public void slideview (final float P1, final float P2 ){
2 translateanimation animation = new translateanimation (P1, P2, 0, 0 );
3 animation. setinterpolator (New overshootinterpolator ());
4 animation. setduration (durationmillis );
5 animation. setstartoffset (delaymillis );
6 animation. setanimationlistener (new animation. animationlistener (){
7 @ override
8 Public void onanimationstart (animation ){
9}
10
11 @ override
12 public void onanimationrepeat (animation ){
13}
14
15 @ override
16 public void onanimationend (animation ){
17 int left = view. getleft () + (INT) (P2-P1 );
18 int Top = view. gettop ();
19 int width = view. getwidth ();
20 int Height = view. getheight ();
21 view. clearanimation ();
22 view. layout (left, top, left + width, top + height );
23}
24 });
25 view. startanimation (animation );
26}

 

 

Call example:Move to the target location slideview (0, distance); move back from the target location to the original location

Slideview (0,-distance );

 

Problems encountered in the process:

1. After the animation is executed, the view is returned to the original position.

1 translateanimation animation = new translateanimation (P1, P2, 0, 0 );
2 animation. setinterpolator (New overshootinterpolator ());
3 animation. setduration (durationmillis );
4 animation. setstartoffset (delaymillis );
5 view. startanimation (animation );

At the beginning, the animation effect is only written so much that the view will return to the original position after the animation is executed.

After checking the information, try to use animation. setfillafter (true); view no longer returns the original position, but there are another 2nd problems.

2. When you click the button, the view will flash at the initial position before executing the animation.

After checking the information, it is found that animation. setfillafter (true); only moves the view to the target location, but the Click Event bound to the view is still in the original location. As a result, the click will flash first.

Find a solution by checking the information: Without setfillafter, set the view location to achieve the effect. Add the following code: 1 animation. setanimationlistener (new animation. animationlistener (){
2 @ override
3 Public void onanimationstart (animation ){
4}
5
6 @ override
7 public void onanimationrepeat (animation ){
8}
9
10 @ override
11 Public void onanimationend (animation ){
12 INT left = view. getleft () + (INT) (P2-P1 );
13 int Top = view. gettop ();
14 int width = view. getwidth ();
15 int Height = view. getheight ();
16 view. clearanimation ();
17 view. layout (left, top, left + width, top + height );
18}
19 });

 

After the animation is executed (onanimationend), set the view position and clearanimation ()

Note: clearanimation () must be executed before layout (L, t, R, B); otherwise, an error may occur ~ This is a success ~
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.