简述:本篇总结欢迎页. ViewPager加载欢迎页,当滑动到最后一页显示按钮,点击进入主界面. 整个欢迎页带小圆点指示器,闪屏页默认显示三秒.
PART_A闪屏页
public class SplashActivity extends Activity {
private static final String IS_FIRST_IN = "isFirstIn";
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
TextView tv = new TextView(this);
tv.setText("splash page");
setContentView(tv);
isFirstIn(); // 是否第一次进入APP
}
/**
* 判断是否第一次进入APP
*/
private void isFirstIn() {
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
new Handler() {
public void handleMessage(android.os.Message msg) {
if (sp.getBoolean(IS_FIRST_IN, true)) {
sp.edit().putBoolean(IS_FIRST_IN, false).commit();
startActivity(new Intent(SplashActivity.this, NevigateActivity.class));
finish();
} else {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
};
}.sendEmptyMessageDelayed(0, 3000); // 固定3秒闪屏页面展示
}
}
PART_B欢迎页布局及指示器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" > <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/bt_toMain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:text="开始体验" android:textSize="20sp" android:visibility="gone" /> <LinearLayout android:id="@+id/ll_pointer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" android:gravity="center" android:orientation="horizontal" /> </RelativeLayout>
-
小圆点指示器
-
shape_defualt
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <size android:height="5dp" android:width="5dp" /> <solid android:color="#8000" /> </shape>
-
shape_enable
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <size android:height="5dp" android:width="5dp" /> <solid android:color="#cfff" /> </shape>
-
小圆点选择器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/shape_selector_enable" android:state_enabled="true"></item> <item android:drawable="@drawable/shape_selector_default"></item> </selector>
-
PART_C欢迎页JAVA实现
- 可以修改欢迎页ImageView图片集合、小圆点指示器样式及间距等、进入主界面的按钮样式.
public class NevigateActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.guide);
// 初始化欢迎页并加入集合中
initPagers();
// 初始化指示器
initIndicator();
// 设置ViewPager
initViewPager();
}
/**
* 初始化欢迎页并加入集合<mImgs>中
*/
private ArrayList<View> mImgs;
private void initPagers() {
ImageView iv1 = new ImageView(this);
iv1.setBackgroundResource(R.drawable.p1);
ImageView iv2 = new ImageView(this);
iv2.setBackgroundResource(R.drawable.p2);
mImgs = new ArrayList<View>();
mImgs.add(iv1);
mImgs.add(iv2);
}
/**
* 初始化指示器
*/
private LinearLayout ll_indicators;
private void initIndicator() {
ll_indicators = (LinearLayout) findViewById(R.id.ll_pointer);
for (int i = 0; i < mImgs.size(); i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.selector_pointer);
if (i == 0) {
iv.setEnabled(true);
} else {
iv.setEnabled(false);
}
// 初始化参数
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i != 0) {
params.leftMargin = 12; // 指示器点间距
}
iv.setLayoutParams(params);
ll_indicators.addView(iv);
}
}
/**
* ViewPager设置适配器和监听
*/
private ViewPager mViewPager;
private NavigateAdapter mAdapter;
private Button btToMain;
private int lastPos;
private void initViewPager() {
mViewPager = (ViewPager) findViewById(R.id.view_pager);
btToMain = (Button) findViewById(R.id.bt_toMain);
mAdapter = new NavigateAdapter();
mViewPager.setAdapter(mAdapter);
// 控制进入主页的按钮
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override public void onPageSelected(int position) {
// 设置按钮
if (position == mImgs.size() - 1) {
btToMain.setVisibility(View.VISIBLE);
} else {
btToMain.setVisibility(View.GONE);
}
// 设置指示器
int pos = 0;
if (mImgs != null) {
pos = position % mImgs.size();
} else {
pos = position % mImgs.size();
}
// 将本页设置为选中。
ImageView iv = (ImageView) ll_indicators.getChildAt(pos);
iv.setEnabled(true);
// 将上一页设置为未选中。
ImageView lastImage = (ImageView) ll_indicators.getChildAt(lastPos);
lastImage.setEnabled(false);
lastPos = pos;
}
@Override public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override public void onPageScrollStateChanged(int arg0) {}
});
// 点击进入主页
btToMain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(NevigateActivity.this, MainActivity.class));
finish();
}
});
}
/** 固定写法 */
class NavigateAdapter extends PagerAdapter {
@Override public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView(mImgs.get(position));
}
@Override public Object instantiateItem(ViewGroup container, int position) {
((ViewPager) container).addView(mImgs.get(position));
return mImgs.get(position);
}
@Override public int getCount() {
return mImgs.size();
}
@Override public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}
}
以上。如有错误和疑问,欢迎指正提出。 catface.wyh@gmail.com