Android引导页面

实践是知识的母亲,知识是生活的明灯。这篇文章主要讲述Android引导页面相关的知识,希望能为你提供帮助。
 
【Android引导页面】应用引导页是现今App的必需品,通过引导页可以第一时间知道此款软件的特色,如何使用。并且能了解到最新的动态和更新。
  第一页,Logo页面或者欢迎页面,判断是否需要进入引导页。

1 package com.zhang.viewpagerdemo; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.content.SharedPreferences; 6 import android.os.Bundle; 7 import android.os.Handler; 8 import android.os.Message; 9 10 public class WelcomeAct extends Activity { 11 12private boolean isFirstIn = false; 13private static final int TIME = 2000; 14private static final int GO_HOME = 1000; 15private static final int GO_GUIDE = 1001; 16 17private Handler handler = new Handler() { 18 19@Override 20public void handleMessage(Message msg) { 21super.handleMessage(msg); 22 23switch (msg.what) { 24case GO_HOME: 25goHome(); 26break; 27case GO_GUIDE: 28goGuide(); 29break; 30} 31} 32}; 33 34@Override 35protected void onCreate(Bundle savedInstanceState) { 36super.onCreate(savedInstanceState); 37setContentView(R.layout.welcome); 38 39init(); 40} 41 42private void init() { 43SharedPreferences preferences = getSharedPreferences("myWelcome", MODE_PRIVATE); 44isFirstIn = preferences.getBoolean("isFirstIn", true); 45if (!isFirstIn) { 46handler.sendEmptyMessageDelayed(GO_HOME, TIME); 47} else { 48handler.sendEmptyMessageDelayed(GO_GUIDE, TIME); 49SharedPreferences.Editor editor = preferences.edit(); 50editor.putBoolean("isFirstIn", false); 51editor.apply(); 52} 53} 54 55private void goHome() { 56Intent i = new Intent(WelcomeAct.this, MainActivity.class); 57startActivity(i); 58finish(); 59} 60 61private void goGuide() { 62Intent i = new Intent(WelcomeAct.this, Guide.class); 63startActivity(i); 64finish(); 65} 66 }

1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:orientation="vertical" 7tools:context="com.zhang.viewpagerdemo.WelcomeAct"> 8 9< ImageView 10android:layout_width="match_parent" 11android:layout_height="match_parent" 12android:background="@drawable/welcome_android" /> 13 14 < /LinearLayout>

 
引导页面:
1 package com.zhang.viewpagerdemo; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.v4.view.ViewPager; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.widget.Button; 10 import android.widget.ImageView; 11 12 import java.util.ArrayList; 13 import java.util.List; 14 15 /** 16* Created by Mr.Z on 2016/5/22 0022. 17*/ 18 public class Guide extends Activity implements ViewPager.OnPageChangeListener { 19 20private ViewPager vp; 21private ViewPagerAdapter vpAdapter; 22private List< View> views; 23private ImageView[] dots; 24private int[] ids = {R.id.iv1, R.id.iv2, R.id.iv3}; 25private Button btnStart; 26 27@Override 28protected void onCreate(Bundle savedInstanceState) { 29super.onCreate(savedInstanceState); 30setContentView(R.layout.guide); 31 32initViews(); 33 34initDots(); 35} 36 37private void initViews() { 38 39LayoutInflater inflater = LayoutInflater.from(this); 40 41views = new ArrayList< > (); 42views.add(inflater.inflate(R.layout.one, null)); 43views.add(inflater.inflate(R.layout.two, null)); 44views.add(inflater.inflate(R.layout.three, null)); 45 46vpAdapter = new ViewPagerAdapter(views, this); 47 48btnStart = (Button) views.get(2).findViewById(R.id.start_btn); 49 50vp = (ViewPager) findViewById(R.id.viewpager); 51vp.setAdapter(vpAdapter); 52vp.setOnPageChangeListener(this); 53 54btnStart.setOnClickListener(new View.OnClickListener() { 55@Override 56public void onClick(View v) { 57Intent i = new Intent(Guide.this,MainActivity.class); 58startActivity(i); 59finish(); 60} 61}); 62 63} 64 65private void initDots() { 66dots = new ImageView[views.size()]; 67for (int i = 0; i < views.size(); i++) { 68dots[i] = (ImageView) findViewById(ids[i]); 69} 70} 71 72@Override 73public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 74 75} 76 77@Override 78public void onPageSelected(int position) { 79 80} 81 82@Override 83public void onPageScrollStateChanged(int state) { 84for (int i = 0; i < ids.length; i++) { 85if (state == i) { 86dots[i].setImageResource(R.drawable.login_point_selected); 87} else { 88dots[i].setImageResource(R.drawable.login_point); 89} 90} 91} 92 }

1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:layout_width="match_parent" 4android:layout_height="match_parent"> 5 6< android.support.v4.view.ViewPager 7android:id="@+id/viewpager" 8android:layout_width="match_parent" 9android:layout_height="match_parent" 10android:background="@android:color/black" 11android:flipInterval="30" 12android:persistentDrawingCache="animation" /> 13 14< LinearLayout 15android:layout_width="match_parent" 16android:layout_height="wrap_content" 17android:layout_alignParentBottom="true" 18android:gravity="center_horizontal" 19android:orientation="horizontal"> 20 21< ImageView 22android:id="@+id/iv1" 23android:layout_width="wrap_content" 24android:layout_height="wrap_content" 25android:src="https://www.songbingjia.com/android/@drawable/login_point" /> 26< ImageView 27android:id="@+id/iv2" 28android:layout_width="wrap_content" 29android:layout_height="wrap_content" 30android:src="https://www.songbingjia.com/android/@drawable/login_point" /> 31< ImageView 32android:id="@+id/iv3" 33android:layout_width="wrap_content" 34android:layout_height="wrap_content" 35android:src="https://www.songbingjia.com/android/@drawable/login_point" /> 36< /LinearLayout> 37 38 < /RelativeLayout>

1 package com.zhang.viewpagerdemo; 2 3 import android.content.Context; 4 import android.support.v4.view.PagerAdapter; 5 import android.support.v4.view.ViewPager; 6 import android.view.View; 7 8 import java.util.List; 9 10 /** 11* Created by Mr.Z on 2016/5/22 0022. 12*/ 13 public class ViewPagerAdapter extends PagerAdapter { 14 15private List< View> views; 16private Context context; 17 18public ViewPagerAdapter(List< View> views, Context context) { 19this.views = views; 20this.context = context; 21} 22 23@Override 24public void destroyItem(View container, int position, Object object) { 25((ViewPager) container).removeView(views.get(position)); 26} 27 28@Override 29public Object instantiateItem(View container, int position) { 30 31((ViewPager) container).addView(views.get(position)); 32 33return views.get(position); 34} 35 36@Override 37public int getCount() { 38return views.size(); 39} 40 41@Override 42public boolean isViewFromObject(View view, Object object) { 43return (view == object); 44} 45 }

 
主页面:
1 package com.zhang.viewpagerdemo; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 6 public class MainActivity extends AppCompatActivity { 7 8@Override 9protected void onCreate(Bundle savedInstanceState) { 10super.onCreate(savedInstanceState); 11setContentView(R.layout.activity_main); 12} 13 }

 

    推荐阅读