android程序启动画面
?方法一:
很多应用都会有一个启动界面。欢迎画面慢慢隐现,然后慢慢消隐。实现这种效果的方法有两种(暂时只发现两种)
1、使用两个Activity,程序启动时候load第一张Activity,然后由tick触发N秒钟后startActivity另外一张Activity。
2、使用一个Activity,可以用到View.gone() 这个方法。把Acitivity的某些元素移除。
1、两个Activity:
首先是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
??????package="com.sunshine.splash"
??????android:versionCode="1"
??????android:versionName="1.0">
????<application android:icon="@drawable/icon" android:label="@string/app_name">
????????<activity android:name=".Splash"
??????????????????android:label="@string/app_name">
????????????<intent-filter>
????????????????<action android:name="android.intent.action.MAIN"/>
????????????????<category android:name="android.intent.category.LAUNCHER" />
????????????</intent-filter>
????????</activity>
????<activity android:name="Main">
????</activity>
</application>
????<uses-sdk android:minSdkVersion="3" />
</manifest>
然后是JAVA代码:
package net.hlovey.splash;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
?
public class Splash extends Activity {???
?
????private final int SPLASH_DISPLAY_LENGHT = 3000; //延迟三秒?
?
[email protected]
????public void onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.splash);
????????new Handler().postDelayed(new Runnable(){
?
[email protected]
?????????public void run() {
?????????????Intent mainIntent = new Intent(Splash.this,Main.class);
?????????????Splash.this.startActivity(mainIntent);
?????????????????Splash.this.finish();
?????????}
???????????
????????}, SPLASH_DISPLAY_LENGHT);
????}
}
加载后使用Handler的postDelayed方法,2秒后执行跳转到主视图。
当然可以再Splash中加入动画效果。(我觉得先要布局好AndroidManifest.xml。因为那才是工程的索引文件。首先在那要有一个统筹!而不是先写java code。然后逐步往xml中增加?,这说明对整个项目没有一个统筹的设计)
方法二:
在原来的工作layout上增加一个全屏的LinearLayout,注意,原来的工作界面跟在下面,原来怎样就怎样,一段时间后,隐藏这个全屏的LinearLayout,工作界面就自然而然显示出来了。
public class MyActivity extends Activity implements Runnable {???
????private boolean isOver=false;
????private LinearLayout screenup;
????Handler handler=new Handler(){
[email protected]
????????public void handleMessage(Message msg) {
????????????// TODO Auto-generated method stub
????????????switch(msg.what){
????????????case 0:
????????????????//隐藏启动的View
????????????????screenup.setVisibility(View.GONE);
????????????????//取消全屏
????????????????getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
????????????????
????????????????break;
????????????case 1:
????????????????Toast.makeText(getApplicationContext(), "加载中", Toast.LENGTH_SHORT).show();
????????????????break;
????????????}
????????????super.handleMessage(msg);
????????}
????????
????};
[email protected]
????public void onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
???????
????????getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
????????????????WindowManager.LayoutParams.FLAG_FULLSCREEN);
?????????
????????requestWindowFeature(Window.FEATURE_NO_TITLE);
????????setContentView(R.layout.main);
????????screenup=(LinearLayout)findViewById(R.id.screenup);
????????new Thread(this).start();
????????
????}
????private void show(){
????????Message msg=new Message();
????????if(isOver){
????????????msg.what=0;
????????????handler.sendMessage(msg);
????????}else{
????????????msg.what=1;
????????????handler.sendMessage(msg);
????????}
????}
[email protected]
????public void run() {
????????// TODO Auto-generated method stub
????????//加载数据。。。。。。。。此处没有数据加载,延时2秒
????????????try {
????????????????Thread.sleep(2000);
????????????} catch (InterruptedException e) {
????????????????// TODO Auto-generated catch block
????????????????e.printStackTrace();
????????????}
????????????isOver=true;
????????????show();
????????????
????????}
????}