Android起動ページSplashScreen実装

6215 ワード

SplashScreenは起動ページとも呼ばれ、通常はプログラムの起動時にブートとして使用され、必要に応じていくつかのデータの初期化、自動ログインの操作などを行うこともできます.最近のプロジェクトでは起動ページの機能を追加しますが、実現は非常に簡単で、皆さんと共有してください.まずxmlファイルにimageviewを配置して画像を表示します.

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

画像に適切なアニメーション効果を追加します.

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    
    <alpha
        android:duration="3000"
        android:fromAlpha="0.5"
        android:interpolator="@android:res/anim/accelerate_decelerate_interpolator"
        android:toAlpha="1.0" >
    alpha>

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

3秒滞在して自動的にログインまたは他のインタフェースに入ります.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);  
    }  
}