Welcome.java
//此功能曾由于只提供一套图片,没有区分xhml而在部分机型上发生过内存溢出问题.
public class Welcome extends SFBaseActivity {
private ArrayList pageViews;
private IndicatorViewPager indicatorViewPager;
private LayoutInflater inflater;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置无标题窗口
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.welcome);
inflater = LayoutInflater.from(this);
ViewPager viewPager = (ViewPager) findViewById(R.id.guidePages);
Indicator guide_indicator = (Indicator) findViewById(R.id.guide_indicator);
indicatorViewPager = new IndicatorViewPager(guide_indicator, viewPager);
pageViews = new ArrayList<>();
View v1 = inflater.inflate(R.layout.welcome_1, null, false);
View v2 = inflater.inflate(R.layout.welcome_2, null, false);
View v3 = inflater.inflate(R.layout.welcome_3, null, false);
View v4 = inflater.inflate(R.layout.welcome_4, null, false);
pageViews.add(v1);
pageViews.add(v2);
pageViews.add(v3);
pageViews.add(v4);
indicatorViewPager.setAdapter(new MyAdapter());
Button btn_start = (Button) v4.findViewById(R.id.btn_start);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
goHome();
}
});
//过渡页
v4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
goHome();
}
});
}
private void goHome() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Welcome.this.finish();
}
private class MyAdapter extends IndicatorViewPager.IndicatorViewPagerAdapter {
@Override
public View getViewForTab(int position, View convertView, ViewGroup container) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.tab_guide, container, false);
}
return convertView;
}
@Override
public View getViewForPage(int position, View convertView, ViewGroup container) {
return pageViews.get(position);
}
@Override
public int getItemPosition(Object object) {
//这是ViewPager适配器的特点,有两个值 POSITION_NONE,POSITION_UNCHANGED,默认就是POSITION_UNCHANGED,
// 表示数据没变化不用更新.notifyDataChange的时候重新调用getViewForPage
return PagerAdapter.POSITION_NONE;
}
@Override
public int getCount() {
return pageViews.size();
}
}
}
welcome.xml(activity对应的界面):
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/guidePages"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center_horizontal">
<com.sf.sdk.viewpagerindicator.view.indicator.FixedIndicatorView
android:id="@+id/guide_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="78dp"/>
RelativeLayout>
merge>
每次切屏的界面:welcome_1.xml(其他welcome_2.xml, welcome_3.xml, welcome_4.xml类似,都是一个layout里面设置一个背景,当然welcome_4.xml除外,welcome_4.xml有一个按钮,点击之后进入APP主界面):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@mipmap/welcome_1">
LinearLayout>
welcome_4.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/welcome_4"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:gravity="bottom">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_start"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="48dp"
android:layout_marginBottom="10dp"
android:text="开始体验"
android:textColor="@color/red"
android:textSize="18sp"
android:background="@drawable/self_btn_vip"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
LinearLayout>
RelativeLayout>
指示器的UI:tab_guide.xml
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="@drawable/tab_selector"
android:orientation="vertical" />
指示器的两种状态:
tab_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="oval">
<solid android:color="@color/colorPrimaryDarkReal">solid>
shape>
item>
<item>
<shape android:shape="oval">
<solid android:color="@color/gray_line">solid>
shape>
item>
selector>