android欢迎界面渐入,渐出效果制作

来源:互联网 发布:linux run as root 编辑:程序博客网 时间:2024/06/08 09:37

app通常有一个欢迎界面,常用的实现方法代码如下:

  try {            Thread.sleep(3000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        startActivity(new Intent(WelcomActivity.this,MainActivity.class));        finish();

这样会出现一个问题,那就是welcomActivity到MainActivity的过程中会有一个空白的部分,很不雅观,升级以后的代码如下:

 protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);         View rootView = LayoutInflater.from(this).inflate(R.layout.activity_welcomne, null);          setContentView(rootView);        new Handler().postDelayed(r, 3000);  }      Runnable r = new Runnable() {        @Override        public void run() {    Intent intent = new Intent(WelcomActivity.this,MainActivity.class);            startActivity(intent);            finish();        }    };

上面的方法不会出现留白的情况,但是还是有点问题,如果我想实现渐入渐出的效果怎么办?代码实现由两种方法:
第一种:

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);         View rootView = LayoutInflater.from(this).inflate(R.layout.activity_welcome, null);          setContentView(rootView);        new CountDownTimer(3000,1000) {            @Override            public void onTick(long millisUntilFinished) {                // TODO Auto-generated method stub            }            @Override            public void onFinish() {                startActivity(new Intent(WelcomeActivity.this,MainActivity.class));                WelcomeActivity.this.overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);            }        }.start();

这种方法是通过设置activity之间的切换方式,从而实现淡出本activity,淡入下一个activity,我测试了一下如果,是竖屏切换到横屏,这样的设置方式就不太明显,这中种情况的时候就不推荐。这种方法运用了倒计时的功能,这里不做论述可以参考这里:使用CountDownTimer类轻松实现倒计时功能
第二种:

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);         View rootView = LayoutInflater.from(this).inflate(R.layout.activity_welcome, null);          setContentView(rootView);        Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_out);        animation.setAnimationListener(new AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {                // TODO Auto-generated method stub            }            @Override            public void onAnimationRepeat(Animation animation) {                // TODO Auto-generated method stub            }            @Override            public void onAnimationEnd(Animation animation) {                startActivity(new Intent(WelcomeActivity.this,MainActivity.class));            }        });        rootView.startAnimation(animation);    }    }

第二种通过设置本activity的动画实现的,先绑定动画,在设置监听器,当动画完成的时候就用Intent跳转从而实现了渐入渐出的效果。这种方式在竖横屏切换的时候也有用,但是只能实现一种效果,要么淡出,要么淡入,无法同时实现淡入淡出。
除了上面的两个之外,还需要在res文件夹中建立anim文件夹,建立两个文件
alpha_in.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">   <alpha        android:fromAlpha="0.0"       android:toAlpha="1.0"       android:duration="2000"/></set>

alpha_out.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha        android:fromAlpha="1.0"       android:toAlpha="0.0"       android:duration="3000"/></set>

android:fromAlpha表示动画开始的透明度,android:toAlpha表示动画结束时的透明度,
android:duration表示持续的时间。

1 0
原创粉丝点击