以前做过app启动界面是直接启动SplashActivity,在SplashActivity中设置计时器,超过三秒之后自己进入MainActivity。
今天介绍一种用的更多地方式,先直接启动MainActivity,在onCreate方法中启动如下函数:
private void startSplash() {
Intent intent = new Intent(this, SplashActivity.class);
startActivity(intent);
}
在SplashActivity中启动一个handler用来判断finish该activity还是启动闪屏界面,主要业务逻辑代码如下:
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if (msg.what == MSG_FINISH)
finish();
else if(msg.what == MSG_SHOW_GUIDE)
showGuide();
}
};
private void initSplash() {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
guideVersion = prefs.getInt(PREFS_KEY_GUIDE_VERSION, 0);
ivSplash.setVisibility(View.VISIBLE);
if (guideVersion < StockApp.getInstance().getPackageInfo().versionCode) {
handler.sendEmptyMessageDelayed(MSG_SHOW_GUIDE, 3000);
} else {
handler.sendEmptyMessageDelayed(MSG_FINISH, 3000);
}
}
其中页面xml如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ivSplash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/icon_splash" />
<FrameLayout
android:id="@+id/layoutGuide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#70000000" >
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/layoutDots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >
</LinearLayout>
<!--
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:text="开始>>"
android:onClick="start"
android:clickable="true"
android:textColor="@color/white"
/> -->
</FrameLayout>
</FrameLayout>
SplashActivity代码如下:
package com.imfclub.stock.activity;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import com.imfclub.stock.Constants;
import com.imfclub.stock.R;
import com.imfclub.stock.StockApp;
import com.imfclub.stock.http.ApiClient;
import com.imfclub.stock.http.ResponseHandler;
/**
* 闪屏界面
*
*/
public class SplashActivity extends Activity {
private static final int MSG_FINISH = 0x01;
private static final int MSG_SHOW_GUIDE = 0x02;
private static final String PREFS_KEY_GUIDE_VERSION = "guide_version";
private static final int GUIDE_VERSION = 0;
private ImageView ivSplash;
private FrameLayout layoutGuide;
private LinearLayout layoutDots;
private ViewPager vp;
private int guideVersion;
private SharedPreferences prefs;
private int[] images = { R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3, R.drawable.guide_4};
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == MSG_FINISH)
finish();
else if(msg.what == MSG_SHOW_GUIDE)
showGuide();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
initViews();
initSplash();
}
private void showGuide(){
ivSplash.setVisibility(View.GONE);
layoutGuide.setVisibility(View.VISIBLE);
initPager();
}
private void initSplash() {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
guideVersion = prefs.getInt(PREFS_KEY_GUIDE_VERSION, 0);
ivSplash.setVisibility(View.VISIBLE);
if (guideVersion < StockApp.getInstance().getPackageInfo().versionCode) {
handler.sendEmptyMessageDelayed(MSG_SHOW_GUIDE, 3000);
} else {
handler.sendEmptyMessageDelayed(MSG_FINISH, 3000);
}
}
private void initPager() {
vp = (ViewPager) findViewById(R.id.vp);
for (int i = 0; i < images.length; i++) {
ImageView dotView = new ImageView(this);
LayoutParams params = new LayoutParams(10, 10);
params.setMargins(5, 0, 5, 0);
dotView.setLayoutParams(params);
if (i == 0) {
dotView.setImageResource(R.drawable.indicator_focused);
} else {
dotView.setImageResource(R.drawable.indicator_normal);
}
layoutDots.addView(dotView);
}
vp.setAdapter(new GuideViewAdapter(this, images));
vp.setOnPageChangeListener(pageChaneListener);
}
private void initViews() {
ivSplash = (ImageView) findViewById(R.id.ivSplash);
layoutGuide = (FrameLayout) findViewById(R.id.layoutGuide);
layoutDots = (LinearLayout) findViewById(R.id.layoutDots);
}
final ViewPager.OnPageChangeListener pageChaneListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < layoutDots.getChildCount(); i++) {
if (i == arg0) {
((ImageView) layoutDots.getChildAt(i))
.setImageResource(R.drawable.indicator_focused);
} else {
((ImageView) layoutDots.getChildAt(i))
.setImageResource(R.drawable.indicator_normal);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
class GuideViewAdapter extends PagerAdapter {
private Context context;
private int[] images;
private LayoutInflater inflater;
public GuideViewAdapter(Context context, int[] images) {
this.context = context;
this.images = images;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return images != null ? images.length : 0;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0.equals(arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = inflater.inflate(R.layout.pager_item_guide_view,
container, false);
assert view != null;
ImageView iv = (ImageView) view.findViewById(R.id.iv);
Button bt = (Button) view.findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
prefs.edit()
.putInt(PREFS_KEY_GUIDE_VERSION,
StockApp.getInstance().getPackageInfo().versionCode)
.commit();
((Activity) context).finish();
}
});
if (position == images.length - 1) {
bt.setVisibility(View.VISIBLE);
}
// Picasso.with(SplashActivity.this).load(images[position]).fit().centerCrop().into(iv);
iv.setImageResource(images[position]);
container.addView(view);
return view;
}
}
public void start(View view) {
prefs.edit()
.putInt(PREFS_KEY_GUIDE_VERSION,
StockApp.getInstance().getPackageInfo().versionCode)
.commit();
finish();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
}
}