Splash闪屏页的使用方法

方法一(静态):使用Handler,在handleMessage中使用Intent实现跳转

MainActivity

package com.example.forherplayer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


public class SplashActivity extends Activity {

	private static final int START_ACTIVITY = 0x1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        handler.sendEmptyMessageDelayed(START_ACTIVITY, 3000);
    }


    private Handler handler = new Handler(){
    	public void handleMessage(Message msg) {
    		switch(msg.what){
    			case START_ACTIVITY:
    				startActivity(new Intent(SplashActivity.this,MainActivity.class));
    				finish();
    				break;
    		}
    	};
    };
}

其他代码与下文相同,详见方法二

方法二(动态):使用AlphaAnimation,在监听最后结束动画后实现Intent跳转

MainActivity

package com.example.splashtestdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;


/*
 * 方法一:静态闪屏页
 * 使用Handler,在handleMessage中实现Intent跳转
 */
//public class SplashActivity extends Activity {
//
//	private static final int START_ACTIVITY = 0x1;
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_splash);
//        handler.sendEmptyMessageDelayed(START_ACTIVITY, 3000);
//    }
//
//
//    private Handler handler = new Handler(){
//    	public void handleMessage(Message msg) {
//    		switch(msg.what){
//    			case START_ACTIVITY:
//    				startActivity(new Intent(SplashActivity.this,MainActivity.class));
//    				finish();
//    				break;
//    		}
//    	};
//    };
//}

/*
 * 方法二:动态闪屏页
 * 使用AlphaAnimation,在监听最后结束动画后实现Intent跳转
 */
public class SplashActivity extends Activity {
    
	private ImageView iv_splash;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加去标题和隐藏状态栏代码,测试时发现标题与状态栏瞬间状态可视,建议在清单文件SplashActivity的them添加为
        //android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
//        //去标题
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        //隐藏状态栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        iv_splash = (ImageView) findViewById(R.id.iv_splash);
        //设置透明度动画由10%到100%
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
        //设置动画时长3秒
        alphaAnimation.setDuration(3000);
        //开始动画
        iv_splash.startAnimation(alphaAnimation);
        //为动画设置监听器
        alphaAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				startActivity(new Intent(SplashActivity.this,MainActivity.class));
				finish();
			}
		});
    }
        
}


activity_splash

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.splashtestdemo.SplashActivity" >

    <ImageView
        android:id="@+id/iv_splash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash"
        android:scaleType="centerCrop" />

</LinearLayout>

跳转后的MainActivity

package com.example.splashtestdemo;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}


activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="账号"
        android:textSize="16sp" />
    <AutoCompleteTextView
        android:id="@+id/user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:completionThreshold="1"
        android:inputType="textEmailAddress"
        android:singleLine="true" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="密码"
        android:textSize="16sp" />
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:password="true"
        android:singleLine="true" />
    <Button
        android:id="@+id/loginButton"
        android:layout_width="180dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:dividerHeight="2.0dp"
        android:text="登陆" />
</LinearLayout>
 


AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.splashtestdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>
    </application>

</manifest>

由于没有找到合适手机gif截屏app,所以不上传效果图了,然而闪屏页素材还是可以上传的


参考:http://blog.csdn.net/junjunguoguo/article/details/7798796


加餐:http://blog.csdn.net/u011067360/article/details/24176679?utm_source=tuicool&utm_medium=referral

http://blog.csdn.net/ye_scofield/article/details/43269311

http://blog.csdn.net/wangjinyu501/article/details/7643396

你可能感兴趣的:(Splash闪屏页的使用方法)