开发者

Android: start the app with animation

I have an app that is launched by clicking a link in a webpage.

No problem, that works fine.

However, the app main screen just sort of bumps away the browser. I'd like to add a little animation. Maybe it can fade-in or开发者_运维百科 something. I have done tween animations on ImageView but not sure how to do it for the full layout screen. Any ideas?


i think you can simply use the AlphaAnimation , and apply it to the layout of your Activity like this , in the onCreate method :

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


This code comes from one of my projects. Feel free to use it in yours. Your activity should extend it, and do as described in 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();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜