4

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

没问题,效果很好。

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

4

2 回答 2

11

我认为您可以简单地使用 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);
于 2011-05-23T19:04:17.543 回答
2

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