Android:用动画启动应用程序

时间:2011-05-23 18:54:58

标签: android android-animation

我有一个通过点击网页中的链接启动的应用。

没问题,工作正常。

但是,应用程序主屏幕只是让浏览器崩溃。我想添加一点动画。也许它可以淡入或什么的。我已经在ImageView上完成了动画,但不确定如何在完整的布局屏幕上进行动画制作。有什么想法吗?

2 个答案:

答案 0 :(得分:9)

我认为你可以简单地使用AlphaAnimation,并在onCreate方法中将它应用到Activity的布局中:

super.onCreate(savedInstace);
this.setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.idLayout);
AlphaAnimation animation = new AlphaAnimation(0.0f , 1.0f ) ;
animation.setFillAfter(true);
animation.setDuration(1200);
//apply the animation ( fade In ) to your LAyout
layout.startAnimation(animation);

答案 1 :(得分:1)

此代码来自我的一个项目。随意使用它。您的活动应该扩展它,并按照HOWTO中的描述进行。

    /**
     * FadedActivity is a subclass of Activity. The difference with a
     * standard activity is the fade in/out animation launched when
     * the activity begins/ends.
     * 
     * <p>
     * HOWTO:
     * <ul>
     * <li>layout's main layer must have android:id="root"
     * <li>derived class must call FadedActivity's super.onCreate() instead of Activity's
     * <li>use finishFade() instead of finish()
     * </ul>
     * 
     * @author      Joel
     * @version     1.0
     * @since       1.0
     */

    public static class FadedActivity extends Activity {

        private static final int FADEIN_DELAY_MS = 1000;
        private static final int FADEOUT_DELAY_MS = 500;

        private View root;

        private void runFadeAnimationOn(Activity ctx, View target, boolean in, int delay) {
            int start, finish;
            if (in) {
                start = 0;
                finish = 1;
            } else {
                start = 1;
                finish = 0;
            }
            AlphaAnimation fade = new AlphaAnimation(start, finish); 
            fade.setDuration(delay); 
            fade.setFillAfter(true); 
            target.startAnimation(fade);
        }    

        public void onCreate(Bundle savedInstanceState, int layoutId) {
            super.onCreate(savedInstanceState);
            setContentView(layoutId);
            root = (View)findViewById(R.id.root);
            runFadeAnimationOn(this, root, true, FADEIN_DELAY_MS);
        }

        public void finishFade() {
            final int delay = FADEOUT_DELAY_MS;
            runFadeAnimationOn(this, root, false, delay);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(delay);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    FadedActivity.super.finish();
                }
            }).start();
        }
    }