繁体   English   中英

Android:使用 animation 启动应用程序

[英]Android: start the app with animation

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

没问题,效果很好。

然而,应用程序主屏幕只是有点颠簸浏览器。 我想补充一点 animation。 也许它可以淡入或什么的。 我已经在 ImageView 上完成了补间动画,但不知道如何在整个布局屏幕上执行此操作。 有任何想法吗?

我认为您可以简单地使用 AlphaAnimation,并将其应用于您的 Activity 布局,如下所示,在 onCreate 方法中:

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

这段代码来自我的一个项目。 随意使用它。 您的活动应该扩展它,并按照 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();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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