简体   繁体   中英

Android: How to animate different objects in different time duration?

How to animate different set of objects in different time duration one after the other?

JAVA Code:

ImageButton home = (ImageButton)findViewById(R.id.homeicon);
ImageButton settings = (ImageButton)findViewById(R.id.settingsicon); 

Animation alpha_anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
home.startAnimation(alpha_anim);
settings.startAnimation(alpha_anim);

Animation File:

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

can anyone help me?

You can add a delay to the second animation for the amount of time the first should take. Please realize that this may not be exact enough for you needs, if not then you might need to go the route of an AnimationListener

home.startAnimation(alpha_anim);
alpha_anim.setStartOffset(8000);
settings.startAnimation(alpha_anim);

I had one screen where I need to animate first layout and as soon as its finish, I wanted to start animation on second layout.

So I had used handler at that time to do so, like this

Handler handler = new Handler(); // create Handler object
handler.post(homeRun);
Runnable homeRun = new Runnable() { // create runnable to start home anim
    public void run() {
        home.startAnimation(alpha_anim);
        handler.postDelayed(settingsRun, 1000); // start setting anim after the time the home takes to animate
    }
};

Runnable setingsRun = new Runnable() { // runnable to start settings anim
    public void run() {
        settings.startAnimation(alpha_anim);
    }
};

If you want to perform one after the other you can use this code. BY using listener you are sure that animation is finished.

animation.setDuration(8000);
    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationEnd(Animation animation) {
            animation.setDuration(6000);
                                 animation.setAnimationListener(null);
            settings.startAnimation(animation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationStart(Animation animation) {
        }

    });
    home.startAnimation(alpha_anim);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM