Switch between views with crossfade animation

后端 未结 2 1435
日久生厌
日久生厌 2021-02-15 16:43

I\'ve wrote a small activity that is able to switch between two views. Now I am trying to add some animation (fade-in/fade-out effect). Can anybody explain me how to do that rig

2条回答
  •  [愿得一人]
    2021-02-15 17:33

    The way I have done this in the past is by using the ViewFlipper class and utilizing the built-in animation functions that the package provides.

    Here is an example on how to do this; in my experience the transitions have been very smooth:

    The XML File

    
                
                
                
                
        
    
    

    Please note that you do not have to use relative layouts, I simply used them for the sake of clarity.

    Implementing The Animations

    Get a reference to the ViewFlipper in your activity:

    ViewFlipper v = (ViewFlipper) findViewById(R.id.[your_id]);
    

    Set the animations as necessary:

    v.setInAnimation(AnimationUtils.loadAnimation([your_activity_name].this, R.anim.[your_in_animation here]));
    v.setOutAnimation(AnimationUtils.loadAnimation([your_activity_name].this, R.anim.[your_out_animation here]));
    

    Please note that you can find some really good prebuilt animations in the Android class files located in the following directory:

    [android-sdks]/samples/android-[VERSION_NUMBER_HERE]/ApiDemos/res/anim
    

    I highly recommend using these if you can - it will save you much time.

    Now, if you wish to switch between the views, use the following commands:

    v.showNext();
    v.showPrevious();
    

    You might have to change the animation files slightly to make sure the animations transition properly (i.e. make a fade right and left animation).

    Hope this helps!

提交回复
热议问题