Home » Android » android – Switch between views with crossfade animation

android – Switch between views with crossfade animation

Posted by: admin June 16, 2020 Leave a comment

Questions:

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 right?

My own attempt to do this works kinda buggy (if I will click buttons very fast, my application freezes). I use code listed below:

public class WelcomeActivity extends Activity {
private boolean isLogin = false;
private String KEY_IS_LOGIN = "KEY_IS_LOGIN";
private Animation anim_fadein;
private RelativeLayout welcome, login;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    welcome = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_welcome_menu, null);
    login = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_welcome_login, null);
    anim_fadein = AnimationUtils.loadAnimation(this, R.anim.anim_fadein);
    if (savedInstanceState != null)
        isLogin = savedInstanceState.getBoolean(KEY_IS_LOGIN, false);
    if (isLogin)
        setContentView(login);
    else
        setContentView(welcome);
}

@Override
public void onBackPressed() {
    if (isLogin) {
        setContentView(welcome);
        welcome.startAnimation(anim_fadein);
        isLogin = false;
    } else {
        super.onBackPressed();
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(KEY_IS_LOGIN, isLogin);
    super.onSaveInstanceState(outState);
}

public void onButton1Click(View v) {
    setContentView(login);
    login.startAnimation(anim_fadein);
}

public void onButtonLoginClick(View v) {
    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(i);
}

public void onButtonBackClick(View v) {
    setContentView(welcome);
    welcome.startAnimation(anim_fadein);
}

Animation XML file:

<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="800" />

Thanks in advance!

How to&Answers:

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

<LinearLayout
    //Ommitted...
    <ViewFlipper
        android:id="@+id/[your_id_here]"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
            <RelativeLayout
                <!--Your first layout XML here...-->
            </RelativeLayout>
            <RelativeLayout
                <!--Your second layout XML here...-->
            </RelativeLayout>
    </ViewFlipper>
</LinearLayout>

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!

Answer:

I think there are 2 main solution to this problem

  • The first one is using a ViewFlipper as suggested.
  • The other one is to go with the solution described here.

I prefer the second one cause it doesn’t need additional View object in your view hiearchy and second you can have your 2 view all across the view tree. Not only in a single place defined by the position of the ViewFlipper.