Android Crossfading animation fades out of the animation and androidcrossfading

Source: Internet
Author: User

Android Crossfading animation fades out of the animation and androidcrossfading

Fade-in or fade-In animations are what we often call a fade-in animation. When an interface gradually disappears, it gradually appears. When you need to switch two views in the application, the animation effect is very practical. This animation is short but exquisite, cleverly linking the switching of views. If you do not use this animation, the entire switching process will become stiff and fast.

Prepare to start 1. Create a member variable to link to the view where you need time animation. 2. Let the view displayed after Gone drop first to avoid occupying layout space, avoid system resource waste caused by calculation, and speed up interface loading. 3. Use a member variable to cache the config_shortAnimTime variable of the system. This variable is an animation time provided by the system. It ensures that the animation execution is more consistent and refined. Similarly, config_longAnimTime and config_mediumAnimTime are used. (Reference Method: int mShortAnimationDuration = getResources (). getInteger (android. R. integer. config_shortAnimTime );)
Mark with code:
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);    }
We have prepared for this View. Next we will gradually hide it. 1. Change the alpha value of the view to 0 and Visibility to Visible. Now this view can be displayed, but you still cannot see it, because it is transparent. 2. Create two alpha animations. One is to set alpha from 0 to 1, and the other is to set alpha values from 1 to 0. 3. In the listening callback method of Animator. AnimatorListener, onAnimationEnd () drops the View gone that is initially displayed. Although the alpha value is 0, we can't see it, but we still need to set its visibility attribute to gone. This reduces the space occupied by layout and layout computing, thus improving the running speed.
Let's take a look at the code below:
private View mContentView;private View mLoadingView;private int mShortAnimationDuration;...private void crossfade() {    // Set the content view to 0% opacity but visible, so that it is 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 an optimization step (it won't    // participate in layout passes, etc.)    mLoadingView.animate()            .alpha(0f)            .setDuration(mShortAnimationDuration)            .setListener(new AnimatorListenerAdapter() {                @Override                public void onAnimationEnd(Animator animation) {                    mLoadingView.setVisibility(View.GONE);                }            });}

-------------- After this article, --------------- digress: 1. FrameLayout is recommended for animation instead of LinearLayout or RelativeLayout. Because animation involves many coordinate issues, such as absolute and relative positions. Using other la s makes it difficult and difficult to figure out how to calculate coordinates. Therefore, FrameLayout is recommended for animation la S. 2. since API12, I .e., Andoird3.1, the view has added the animate () method to execute an Animation directly in the same way as the above example, instead of loading an xml Animation or creating an Animation object, however, I am still reading my habits.

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.