Let's take a look at the animation effect of a good Android boot menu made by foreigners.
1. First, place some images for animation in the drawable directory.
2 splash. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/thesplashlayout"
Android: layout_gravity = "center"
>
<Imageview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/splashimageview"
Android: layout_gravity = "center"
>
</Imageview>
3. Main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</Linearlayout>
3 splashscreen. Java
Here is the core part of the welcome startup class
Public class splashscreen extends activity {
/**
* The thread to process splash screen events
*/
Private thread msplashthread;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Splash screen view
Setcontentview (R. layout. Splash );
// Start animating the image
Final imageview splashimageview = (imageview) findviewbyid (R. Id. splashimageview );
Splashimageview. setbackgroundresource (R. drawable. Flag );
Final animationdrawable frameanimation = (animationdrawable) splashimageview. getbackground ();
Splashimageview. Post (New runnable (){
Public void run (){
Frameanimation. Start ();
}
});
Final splashscreen = this;
// The thread to wait for splash screen events
Msplashthread = new thread (){
@ Override
Public void run (){
Try {
Synchronized (this ){
// Wait given period of time or exit on touch
Wait (5000 );
}
}
Catch (interruptedexception ex ){
}
Finish ();
// Run next activity
Intent intent = new intent ();
Intent. setclass (splashscreen, mainactivity. Class );
Startactivity (intent );
Stop ();
}
};
Msplashthread. Start ();
}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Super. oncreateoptionsmenu (menu );
Return false;
}
/**
* Processes splash screen touch events
*/
@ Override
Public Boolean ontouchevent (motionevent EVT)
{
If (EVT. getaction () = motionevent. action_down)
{
Synchronized (msplashthread ){
Msplashthread. policyall ();
}
}
Return true;
}
4. Add a style file under the values directory for better looks.
Styles. xml:
<Resources>
<Style name = "animations" parent = "@ Android: Animation"/>
<Style name = "animations. splashscreen">
<Item name = "Android: windowenteranimation"> @ anim/appear </item>
<Item name = "Android: javaswexitanimation"> @ anim/disappear </item>
</Style>
<Style name = "theme. Transparent" parent = "Android: Theme">
<Item name = "Android: javaswistranslucent"> true </item>
<Item name = "Android: windowbackground"> @ Android: color/transparent </item>
<Item name = "Android: windowcontentoverlay"> @ null </item>
<Item name = "Android: windownotitle"> true </item>
<Item name = "Android: incluwisfloating"> true </item>
<Item name = "Android: backgrounddimenabled"> false </item>
<Item name = "Android: windowanimationstyle"> @ style/animations. splashscreen </item>
</Style>
</Resources>
Note <style name = "animations" parent = "@ Android: Animation"/>
<Style name = "animations. splashscreen">
<Item name = "Android: windowenteranimation"> @ anim/appear </item>
<Item name = "Android: javaswexitanimation"> @ anim/disappear </item>
</Style>
Defines the animation effect when entering and exiting. More details
The anim directory contains the following XML definitions:
Appear. xml:
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Alpha
Android: interpolator = "@ Android: anim/accelerate_interpolator"
Android: fromalpha = "0.0" Android: toalpha = "1.0"
Android: duration= "800"
/>
</Set>
Disppear. xml:
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Alpha
Android: interpolator = "@ Android: anim/decelerate_interpolator"
Android: fromalpha = "1.0" Android: toalpha = "0.0"
Android: duration= "800"
/>
</Set>
Here is the setting of transparency:
Then, in manifest. XML, set the style to be used:
<Activity
Android: Name = "splashscreen"
Android: theme = "@ style/theme. Transparent"
>
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"> </Action>
<Category Android: Name = "android. Intent. Category. launcher"> </Category>
</Intent-filter>
</Activity>
You can.
5. Set the animation effect:
Splashimageview. setbackgroundresource (R. drawable. Flag );
Final animationdrawable frameanimation = (animationdrawable) splashimageview. getbackground ();
Splashimageview. Post (New runnable (){
Public void run (){
Frameanimation. Start ();
}
});
Because the animation effect settings cannot be directly set in oncreate, but must be created from the GUI thread
Use the POST method of the imageview class to display the animation when the GUI is created.
Detailed code can be downloaded from the attachment
: