Android: start the app with animation

烈酒焚心 提交于 2020-02-14 20:44:10

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/6101578/android-start-the-app-with-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!