Android Splash Activity Demo


效果图:

  


首先,  在drawable下放入XML splash_animation.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
	android:oneshot="false">
	<item android:drawable="@drawable/wait_1" android:duration="200"></item>
	<item android:drawable="@drawable/wait_2" android:duration="200"></item>
	<item android:drawable="@drawable/wait_3" android:duration="200"></item>
	<item android:drawable="@drawable/wait_4" android:duration="200"></item>
</animation-list>

建立 splash_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RootLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/splash_bg"
    android:gravity="center_vertical|center_horizontal"
    android:padding="0dip"
    android:visibility="visible" >

    <LinearLayout
        android:id="@+id/login_loading_secondary_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:layout_margin="30dip"
        android:background="@drawable/bg_01"
        android:orientation="vertical" >
    </LinearLayout>

    <ImageView
        android:id="@+id/wait_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dip"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="30dip"
        android:background="@drawable/splash_animation"
        android:contentDescription="@null" />

</RelativeLayout>



建立 SplashDemoActivity

public class SplashDemoActivity extends Activity {
	public final static String tag = SplashDemoActivity.class.getSimpleName();
	public final static int SPLASH_DURATION = 2000;
	private ImageView imageView = null;	
	private AnimationDrawable animationDrawable = null;	
	private boolean isPressBack = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_view);

		imageView = (ImageView) findViewById(R.id.wait_image_view);
		imageView.setBackgroundResource(R.drawable.splash_animation);
		animationDrawable = (AnimationDrawable) imageView.getBackground();
		 
	}

	protected void onResume() {
		super.onResume();
		// 开始动画 -- 用在此处,不能保证每次动画都行运行,
		//because the AnimationDrawable is not yet fully attached to the window
		//startSplashAnimation(SPLASH_DURATION);
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if (hasFocus) {
			// 开始动画
			startSplashAnimation(SPLASH_DURATION);
		}
	}

	/**
	 * Description: Run the splash screen for given time limit
	* @param limit
	 */
	protected void startSplashAnimation(final int limit) {
		Thread splashThread = new Thread() {
			@Override
			public void run() {
				try {	
					//Thread.currentThread().sleep(50);//
					//不能在onCreate中立即执行,because the AnimationDrawable is not yet fully attached to the window
					animationDrawable.start();
					int waited = 0;
					while (waited < limit) {
						sleep(100);
						waited += 100;
					}
				} catch (Exception e) {
					Log.w(tag, e.getMessage().toString());
					finish();
				} finally {
					if (!isPressBack) {//解决在SPLASH_DURATION期间内, 用户点击back键,应用会在再次自动重启						
						forwardActivity();
					}
				}
			}
		};
		splashThread.start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		imageView = null;
		animationDrawable = null;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
			isPressBack = true;
			finish();
			return true;
		}
		return false;
	}
	
	private void forwardActivity(){
		Intent intent = new Intent(SplashDemoActivity.this, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		
		finish();
		//SplashDemoActivity.this.overridePendingTransition(R.anim.translucent_enter, R.anim.translucent_exit);
		//SplashDemoActivity.this.overridePendingTransition(R.anim.fade, R.anim.hold);
		//这个overridePendingTransition效果经常不好使, 不知为何???
		overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	}
		
}

zoom_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/decelerate_interpolator">
    <scale android:fromXScale="2.0" android:toXScale="1.0"
           android:fromYScale="2.0" android:toYScale="1.0"
           android:pivotX="50%p" android:pivotY="50%p"
           android:duration="@android:integer/config_mediumAnimTime" />
</set>

zoom_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:zAdjustment="top">
    <scale android:fromXScale="1.0" android:toXScale=".5"
           android:fromYScale="1.0" android:toYScale=".5"
           android:pivotX="50%p" android:pivotY="50%p"
           android:duration="@android:integer/config_mediumAnimTime" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
</set>





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值