1、欢迎页SplashActivity
public class SplashActivity extends AppCompatActivity {
private boolean isFirst = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
isFirst();
}
private void isFirst(){
SharedPreferences sharedPre = getPreferences(MODE_PRIVATE);
isFirst = sharedPre.getBoolean("isFirst", true);
if (isFirst){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
goGuide();
}
},2000);
//储存
SharedPreferences.Editor editor = sharedPre.edit();
editor.putBoolean("isFirst",false);
//提交
editor.commit();
}else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
goHome();
}
},2000);
}
}
private void goHome(){
Intent intent_main=new Intent(this,MainActivity.class);
startActivity(intent_main);
finish();
}
public void goGuide(){
Intent intent = new Intent(this, GuideActivity.class);
startActivity(intent);
finish();
return;
}
}
2、activity_splash页面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@mipmap/zero"/>
</LinearLayout>
3、导航页GuideActivity
public class GuideActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG="GuideActivity";
private ViewPager viewPager;
private ViewPagerAdapter adapter;
private List<View> views;
private Button going_main;
private static final int[] pics = { R.layout.guid_one,
R.layout.guid_two, R.layout.guid_three};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcom);
initData();
}
private void initData() {
viewPager = (ViewPager) findViewById(R.id.view_pager);
views = new ArrayList<View>();
for(int i = 0; i < pics.length; i++){
View view = LayoutInflater.from(this).inflate(pics[i], null);
if(i == pics.length - 1){
going_main = (Button)view.findViewById(R.id.going_main);
going_main.setTag("enter");
going_main.setOnClickListener(this);
}
views.add(view);
}
adapter = new ViewPagerAdapter(this,views);
viewPager.setAdapter(adapter);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.going_main:
Intent intent=new Intent(GuideActivity.this,MainActivity.class);
startActivity(intent);
finish();
break;
}
}
}
4、R.layout.guid_two举例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/two"/>
</LinearLayout>
5、 导航页的布局activity_welcom
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
6、AndroidManifest.xml
<application
android:name=".NettyAppliction"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GuideActivity"/>
<activity android:name=".MainActivity"/>
</application>
7、ViewPagerAdapter适配器
public class ViewPagerAdapter extends androidx.viewpager.widget.PagerAdapter { private final List<View> views; public ViewPagerAdapter(Context context, List view) { views = view; } @Override public int getCount() { return views.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(views.get(position),0); return views.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
注意:引入的图尽量是jpg格式