簡體   English   中英

Android:如何在不同的持續時間內為不同的對象制作動畫?

[英]Android: How to animate different objects in different time duration?

如何一個接一個地在不同的持續時間內為不同的對象集制作動畫?

JAVA代碼:

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);

動畫文件:

<?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" /> 

誰能幫我?

您可以為第二個動畫添加延遲,以獲得第一個動畫應該花費的時間。 請注意,這可能不足以滿足您的需求,如果沒有,那么您可能需要去一個AnimationListener的路線

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

我有一個屏幕,我需要動畫第一個布局,一旦完成,我想在第二個布局上開始動畫。

所以我當時使用過handler來這樣做,就像這樣

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);
    }
};

如果要逐個執行,可以使用此代碼。 通過使用監聽器,您可以確定動畫已完成。

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM