APP首次開啟的歡迎介面
阿新 • • 發佈:2019-02-12
Welcome.java
//此功能曾由於只提供一套圖片,沒有區分xhml而在部分機型上發生過記憶體溢位問題.
public class Welcome extends SFBaseActivity {
private ArrayList<View> 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對應的介面):
<?xml version="1.0" encoding="utf-8"?>
<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主介面):
<?xml version="1.0" encoding="utf-8"?>
<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:
<?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:background="@mipmap/welcome_4"
android:orientation="vertical">
<!--<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"/>-->
<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
<?xml version="1.0" encoding="utf-8"?>
<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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--<item android:drawable="@mipmap/tab_2" android:state_selected="true"></item>
<item android:drawable="@mipmap/tab_1"></item>-->
<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>