Android欢迎界面自动跳转至主页的几种方式

第一种:Handler延时实现,多数Demo项目简单且最常用的.

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
        finish();
    }
},3000);

第二种:利用Timer和TimerTask来实现.

Timer mTimer = new Timer();
TimerTask mTimerTask = new TimerTask() {
    @Override
    public void run() {
        startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
        finish();
    }
};
mTimer.schedule(mTimerTask,3000);

第三种:利用动画实现页面跳转.

private void startAnimation() {
    mAnimationSet = new AnimationSet(false);
    //旋转动画
    RotateAnimation mRotateAnimation = new RotateAnimation(
            0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF,0.5f);//设置锚点为图片的中心点
    // 设置动画播放时间
    mRotateAnimation.setDuration(4000);
    mRotateAnimation.setFillAfter(true);//动画播放完之后,停留在当前状态
    //添加到动画集
    mAnimationSet.addAnimation(mRotateAnimation);
    // 渐变动画
    AlphaAnimation mAlphaAnimation = new AlphaAnimation(0, 1);//完全透明到不透明
    mAlphaAnimation.setDuration(4000);
    mAlphaAnimation.setFillAfter(true);//
    mAnimationSet.addAnimation(mAlphaAnimation);
    // 缩放动画
    ScaleAnimation mScaleAnimation = new ScaleAnimation(
            0, 1, 0, 1,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    mScaleAnimation.setDuration(4000);
    mScaleAnimation.setFillAfter(true);//
    mAnimationSet.addAnimation(mScaleAnimation);
    //开始播放动画
    imageView.startAnimation(mAnimationSet);
}
//动画结束后自动跳转
private void initEvent() {
    mAnimationSet.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
            startActivity(new Intent(BlankWelcomActivity.this,MainActivity.class));
            finish();
        }
    });
}
第四种:利用rxjava2来实现页面跳转.
CompositeDisposable compositeDisposable = new CompositeDisposable();
/**
 * 构建倒计时
 * @param countTime
 * @return
 */
private Observable countDown(int countTime) {
    if (countTime < 0) countTime = 0;
    return Observable.interval(0,1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .map(new Function() {
                @Override
                public Integer apply(Long aLong) throws Exception {
                    return countTime - aLong.intValue();
                }
            }).take(countTime + 1);
}
assert countDown(4) != null;
compositeDisposable.add(countDown(4).doOnSubscribe(new Consumer() {
    @Override
    public void accept(Disposable disposable) throws Exception {
        页面显示
    }
}).subscribeWith(new DisposableObserver() {
    @Override
    public void onNext(Integer integer) {
        //实现倒计时页面显示
    }
    @Override
    public void onError(Throwable e) {
    }

    @Override
    public void onComplete() {
        toMainPage();
    }
}));

private void sendMainPage() {
    if (compositeDisposable != null) {
        compositeDisposable.dispose();
    }
    startActivity(new Intent(this,MainActivity.class));
    finish();
}

 

你可能感兴趣的:(Android,页面跳转)