此splash欢迎页,需要依赖SmartImageView开源项目
jar依赖http://download.csdn.net/detail/xj55646/7099539
github地址https://github.com/loopj/android-smart-image-view
1.定义布局
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.loopj.android.image.SmartImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/siv"
android:src="@drawable/splash_load"
android:scaleType="fitXY"/>
</RelativeLayout>
activity_main.xml
<LinearLayout
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MainActivity"
android:textSize="50sp"/>
</LinearLayout>
2.开始写实现
自定义一个MyTimer作为可以暂停的timer,为了点击splash网络图片时跳转到相关页面,暂停计时器,使返回来时还是原来的splash页面,再继续计时,跳转到Mainactivity。。。
MyTimer.java
/**
* @author Ju4tin
*
*/
public class MyTimer {
private int mDelayTime;
private Runnable mRun;
private int mCounter = 0;
private boolean bPause = false;
private boolean bStart = false;
private Thread mThread;
/**
* @param iDelayTime
* 要执行任务延迟时间 单位为:秒 second
* @param run
* runnable对象
*/
public MyTimer(int delaytime, Runnable run) {
this.mDelayTime = delaytime;
this.mRun = run;
}
public void start() {
if (bStart) {
return;
}
bStart = true;
Runnable r = new Runnable() {
@Override
public void run() {
while (true) {
if (mCounter >= mDelayTime * 10) {
break;
}
if (!bPause) {
mCounter = mCounter + 1;
System.out.println(mCounter);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mRun.run();
}
};
mThread = new Thread(r);
mThread.start();
}
/**
* 暂停计时器
*/
public void pause() {
bPause = true;
}
/**
* 继续
*/
public void doContinue() {
bPause = false;
}
/**
* 从新计时
*/
public void restart(){
mCounter=0;
bPause = false;
}
/**
* 马上执行任务
*/
public void doRightNow(){
mCounter=mDelayTime * 10-1;
bPause = false;
}
}
NetSplash.java //splash页面检查网络,点击splash图片打开浏览器进行跳转,等用户返回时,继续跳转到MainActivity
/**
* @author Ju4tin
*
*/
public class NetSplash extends Activity {
private SmartImageView mSIV = null;//需依赖SmartImageView
private String strURL = "http://www.baidu.com/";
private String strImgUrl = "http://img.159.com/desk/user/2011/12/12/Jiker20111112221378.jpg";
public static final int REQUSET = 1;
public boolean bShowDialog = false;
private MyTimer mTimer = new MyTimer(4, new Runnable() {
@Override
public void run() {
// 跳转。。。
Intent intent = new Intent(NetSplash.this, MainActivity.class);
startActivity(intent);
NetSplash.this.finish();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate");
setContentView(R.layout.splash);
init();
checkNetworkAnddoNext();
}
/**
* 检查网络,并做出相应对策
*/
private void checkNetworkAnddoNext() {
if (!isNetworkAvailable()) {
showDialog();
} else {
mSIV.setImageUrl(strImgUrl);
mSIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 点击splash上的图片,进行跳转。。。
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(strURL);
intent.setData(content_url);
startActivity(intent);
}
});
mTimer.start();
}
}
/**
* 显示Dialog
*/
private void showDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("设置网络");
builder.setMessage("当前没有联接网络,请检查网络状态!");
builder.setPositiveButton("网络", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");// 使用隐式意图兼容性更好
// 类名要包含包名,兼容性不佳
// intent.setClassName("com.android.settings",
// "com.android.settings.WirelessSettings");
startActivity(intent);
bShowDialog=false;
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show();
bShowDialog=true;
}
/**
* 检查网络是否可用
*
*/
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
boolean result = false;
if (info != null && info.isConnected()) {
result = true;
} else {
result = false;
}
return result;
}
/**
* 初始化控件
*/
private void init() {
mSIV = (SmartImageView) findViewById(R.id.siv);
}
@Override
protected void onResume() {
super.onResume();
System.out.println("onResume");
if (!bShowDialog) {
checkNetworkAnddoNext();
}
mTimer.doContinue();
}
@Override
protected void onPause() {
super.onPause();
System.out.println("onPause");
mTimer.pause();// 暂停计时器
}
@Override
protected void onStart() {
super.onStart();
System.out.println("onStart");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestory");
mSIV.destroyDrawingCache();
}
}
MainActivity.java
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}