Android crossfading animation fade fade in animation

Source: Internet
Author: User

Fade in and fade out animation is what we often say fade animation, one interface gradually disappear when another gradually appear. This animation works well when you need to switch two views in the app. The animation is short but very delicate, subtly bridging the switch of the view. If you don't use this animation, you'll make the whole switching process look stiff and short-winded.

Ready to start 1. Create a member variable to link to the view where you need time animation. 2. Let the post-display view gone off first, avoid it occupying layout space, avoid the calculation of the system resources wasted, speed up the interface loading speed. 3. Use a member variable to cache the system's Config_shortanimtime variable, which is a system-provided animation time that ensures that the animation executes more consistently and more beautifully. There are also config_longanimtime and config_mediumanimtime that are similar in function to this variable. (Reference method: int mshortanimationduration = Getresources (). Getinteger (Android). R.integer.config_shortanimtime);)
The code is marked with:
public class    Crossfadeactivity extends Activity {private View mcontentview;    Private View Mloadingview;    private int mshortanimationduration;        ... @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_crossfade);        Mcontentview = Findviewbyid (r.id.content);        Mloadingview = Findviewbyid (R.id.loading_spinner);        Initially hide the content view.        Mcontentview.setvisibility (View.gone);        Retrieve and cache the system ' s default "short" animation time. Mshortanimationduration = Getresources (). Getinteger (Android.    R.integer.config_shortanimtime); }
Fade This view above we are ready to work, and then we will come to realize that it is gradually hidden. 1. Change the alpha value of the view we are going to display to 0 and change it to visible visibility. Now this view can be displayed, but you can't see it because he is transparent. 2. Create two alpha animations, one for the alpha from 0 to 1, and the other to set the alpha value from 1 to 0. 3. The Animator.animatorlistener Listener callback method Onanimationend () gone off the view that was initially displayed. Although his alpha value is 0, we can't see him, but we still need to set its visibility property to gone. This reduces the amount of space he occupies and calculates the layout, thus increasing the speed of the operation.
Here's a look at the code:
Private view Mcontentview;private view mloadingview;private int mshortanimationduration;...private void Crossfade () {/ /Set The content view to 0% opacity but visible, so it's visible//(but fully transparent) during the animation    .    Mcontentview.setalpha (0f);    Mcontentview.setvisibility (view.visible);    Animate the content view to 100% opacity, and clear any animation//listener set on the view. Mcontentview.animate (). Alpha (1f). Setduration (mshortanimationduration). Setlistener (null    ); Animate the loading view to 0% opacity. After the animation ends,//set its visibility to GONE as a optimization step (it won ' t//participate in Layout P    Asses, etc.) Mloadingview.animate (). Alpha (0f). Setduration (mshortanimationduration). Setlistener (New                    Animatorlisteneradapter () {@Override public void Onanimationend (Animator animation) { MloadIngview.setvisibility (View.gone); }            });}

--------------This article ends---------------topic:1. Animation is best used framelayout instead of linearlayout or relativelayout. Because the animation involves a lot of coordinate problems, absolute position and relative position. Using other layouts makes it difficult and difficult to figure out the coordinates. So it's best to use the framelayout with animated layouts. 2. From API12 to Andoird3.1 view the animate () method can be used to perform animations just like the example above without loading an XML animation or creating a animation object, but it's still a personal habit.

Android crossfading animation fade fade in animation

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.