android in practice_Displaying splash screens with timers(MyMovies project)

来源:互联网 发布:java正则匹配时间格式 编辑:程序博客网 时间:2024/06/06 05:08

You want to execute a delayed task that executes its logic only after a certain amount of time has passed.

The splash image can be dropped in the res/drawables folder:splash.png

The layout for a splash screen Activity:splash_screen.xml:

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android">  <ImageView     android:layout_width="fill_parent"    android:layout_height="fill_parent"     android:scaleType="fitXY"    android:src="@drawable/splash" /></merge>
We also need to define the new Activity in the manifest file. Because it’ll be the first Activity that’s launched, it’ll take the place of the MyMovies Activity.
......... <activity            android:name=".SplashScreen"            android:label="@string/title_myMovie"             android:theme="@style/SplashScreen">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>         <activity android:name=".MyMovies" />.........

We want the splash screen to be fullscreen, so add the following code to your styles.xml:

<style name="SplashScreen" parent="@android:style/Theme.Black"><item name="android:windowNoTitle">true</item></style>

create the activity class SplashScreen

public class SplashScreen extends Activity { public static final int SPLASH_TIMEOUT = 2000;   @Override   protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.splash_screen);            new Timer().schedule(new TimerTask(){@Overridepublic void run() {// TODO Auto-generated method stubproceed();}            }, SPLASH_TIMEOUT);   }   public boolean onTouchEvent(MotionEvent event){   if(event.getAction()==MotionEvent.ACTION_DOWN){   proceed();   }   return super.onTouchEvent(event);   }   private void proceed() {   if(this.isFinishing()){   return;   }   startActivity(new Intent(SplashScreen.this,MyMovies.class));   finish();   }}

The task itself creates an Intent to launch our landing screen (the MyMovies main Activity).

原创粉丝点击