Android启动页SplashScreen实现

来源:互联网 发布:广联达软件最新版本 编辑:程序博客网 时间:2024/06/13 17:05

SplashScreen也叫做启动页,通常用于在程序启动时作为引导,有需要的话也可以进行一些数据的初始化,自动登陆的操作等等。最近项目中要添加一个启动页的功能,实现起来非常简单,跟大家分享一下。
首先在xml文件里放置一个imageview用来显示图片:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/iv_splash"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        android:src="@drawable/splash_screen"        android:contentDescription="@string/app_name" /></LinearLayout>

给图片加上相应的动画效果:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false" >    <!-- 透明度从0.5到1 -->    <alpha        android:duration="3000"        android:fromAlpha="0.5"        android:interpolator="@android:res/anim/accelerate_decelerate_interpolator"        android:toAlpha="1.0" >    </alpha>    <!-- 从中心放大,从1到1.2 -->    <scale        android:duration="3000"        android:fromXScale="1.0"        android:fromYScale="1.0"        android:interpolator="@android:res/anim/accelerate_decelerate_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1.2"        android:toYScale="1.2" >    </scale></set>

停留三秒钟自动进入登陆或者其他的界面。Java代码:

public class SplashScreenActivity extends Activity {    //持续时间    private static final int DELAY_TIME = 3000;    private ImageView splashImageView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_splash_screen);            splashImageView = (ImageView) findViewById(R.id.iv_splash);        Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_scale_translate);        animation.setFillAfter(true);//保持动画不还原        splashImageView.setAnimation(animation);    //设置动画         new Handler().postDelayed(new Runnable() {              public void run() {                  Intent i = new Intent(SplashScreenActivity.this, LoginActivity.class);                  // 跳转到登陆界面                 SplashActivity.this.startActivity(i); // 启动登陆界面                  finish(); // 结束Acitivity            }          }, DELAY_TIME);      }  }
0 0