Android Splash Activity Demo

来源:互联网 发布:怀化快照优化 编辑:程序博客网 时间:2024/05/21 11:13


效果图:

  


首先,  在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;@Overrideprotected 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);}@Overridepublic 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() {@Overridepublic void run() {try {//Thread.currentThread().sleep(50);////不能在onCreate中立即执行,because the AnimationDrawable is not yet fully attached to the windowanimationDrawable.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();}@Overrideprotected 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>