Android 引导页面 欢迎页面

来源:互联网 发布:广告字体设计软件 编辑:程序博客网 时间:2024/05/16 07:56

利用viewpager实现的引导页面,欢迎大家study

引导图可谓是宅男福音啊,下面是gif图展示



1:首先是创建一个guideActivity

public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {    private ViewPager viewPager;    private GuidePagerAdapter pageAdapter;    private List<View> views;    private Button enter_btn;    private ImageView[] points;    private int pointIds[] = {R.id.point1, R.id.point2, R.id.point3};    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_guide);        initView();    }    private void initView() {        viewPager = (ViewPager) findViewById(R.id.viewpager);        initViews();    }    private void initViews() {        LayoutInflater inflate = LayoutInflater.from(this);        views = new ArrayList<>();        views.add(inflate.inflate(R.layout.guide_one, null));        views.add(inflate.inflate(R.layout.guide_two, null));        views.add(inflate.inflate(R.layout.guide_three, null));        initPoints();    }    private void initPoints() {        points = new ImageView[views.size()];        for (int i = 0; i < views.size(); i++) {            points[i] = (ImageView) findViewById(pointIds[i]);        }        initPager();    }    private void initPager() {        pageAdapter = new GuidePagerAdapter(views, this);        viewPager.setAdapter(pageAdapter);        viewPager.addOnPageChangeListener(this);        enter_btn = (Button) views.get(2).findViewById(R.id.enter);        enter_btn.setOnClickListener(this);    }    @Override    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {    }    @Override    public void onPageSelected(int position) {        for (int i = 0; i < pointIds.length; i++) {            if (position == i) {                points[i].setImageResource(R.drawable.page_select);            } else {                points[i].setImageResource(R.drawable.page_unselect);            }        }    }    @Override    public void onPageScrollStateChanged(int state) {    }    @Override    public void onClick(View v) {        startActivity(new Intent(this, MainActivity.class));        //保存浏览过引导页的标记        UserSaveUtils.saveBoolean(this, Constant.bunterkey.GUIDE, true);        finish();    }}

2:该activity的布局文件

<?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/viewpager"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/white"        android:flipInterval="30"        android:persistentDrawingCache="animation" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:gravity="center_horizontal"        android:orientation="horizontal">        <ImageView            android:id="@+id/point1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="5dp"            android:src="@drawable/page_select" />        <ImageView            android:id="@+id/point2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="5dp"            android:src="@drawable/page_unselect" />        <ImageView            android:id="@+id/point3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="5dp"            android:src="@drawable/page_unselect" />    </LinearLayout></RelativeLayout>

3:然后是这个viewpager的adapter,注意要复写ondestoryItem和instantteItem()这两个方法

public class GuidePagerAdapter extends PagerAdapter {    private List<View> views;    private Context context;    public GuidePagerAdapter(List<View> views, Context context) {        this.views = views;        this.context = context;    }    @Override    public int getCount() {        return views.size();    }    @Override    public void destroyItem(ViewGroup container, int position, Object object) {        container.removeView(views.get(position));    }    @Override    public Object instantiateItem(ViewGroup container, int position) {        container.addView(views.get(position));        return views.get(position);    }    @Override    public boolean isViewFromObject(View view, Object object) {        return view == object;    }}

4:viewpager的每个页面都是一个view,这个view很简单,布局文件,小编就给出一个吧,因为都是一样的,样式可以自己修改,引导页面最后一张可以添加一个进入主页的按钮,点击的时候保存浏览过引导页的标记

<?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:orientation="vertical">    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/guide_three" />    <Button        android:id="@+id/enter"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentEnd="true"        android:layout_alignParentTop="true"        android:background="@null"        android:text="进入"        android:textSize="15sp" /></RelativeLayout>

5:点击进入的时候保存标记


6:添加一个welcomeActivity作为中转,根据保存的标记用于判断是跳入主页还是引导页面

public class WelcomeActivity extends AppCompatActivity {    private static final int TIME = 2000;    private static final int GOHOME = 1000;    private static final int GUIDE = 3000;    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case GOHOME:                    goHome();                    break;                case GUIDE:                    goGuide();                    break;            }        }    };    private void goHome() {        startActivity(new Intent(this, MainActivity.class));        finish();    }    private void goGuide() {        startActivity(new Intent(this, GuideActivity.class));        finish();    }    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_welcome);        boolean isGuide = UserSaveUtils.getBoolean(this, Constant.bunterkey.GUIDE);        if (isGuide) {            handler.sendEmptyMessageDelayed(GOHOME, TIME);        } else {            handler.sendEmptyMessageDelayed(GUIDE, TIME);        }    }}

7:在数据存储方面,用的是我自己封装好的sharepreference的一个工具类,相信选择Android开发的朋友都使用过,简单。这里也给出来给大家参考啦

public class SaveInfoUtils {    /**     * 全局唯一id     */    private static final String INSTANCE_ID = "InstanceID";    private SaveInfoUtils() {    }    public static SharedPreferences getSharedPreferences(Context context,                                                         String spName) {        SharedPreferences sp;        if (TextUtils.isEmpty(spName)) {            sp = PreferenceManager.getDefaultSharedPreferences(context);        } else {            sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);        }        return sp;    }    /**     * 获取应用全局id     *     */    public static String getInstanceID(Context context) {        return getString(context, null, INSTANCE_ID);    }    /**     * SharedPreferences保存全局id     *     * @param value value     */    public static void saveInstanceID(Context context, String value) {        saveString(context, null, INSTANCE_ID, value);    }    /**     * SharedPreferences保存boolean类型的     *     * @param name  key     * @param value value     */    public static void saveBoolean(Context context, String name, boolean value) {        saveBoolean(context, null, name, value);    }    public static void saveBoolean(Context context, String spName, String name,                                   boolean value) {        SharedPreferences sp = getSharedPreferences(context, spName);        Editor editor = sp.edit();        editor.putBoolean(name, value);        editor.apply();    }    /**     * 获取保上面存的boolean类型的     *     * @param name   key     * @param defuat 默认值     */    public static boolean getBoolean(Context context, String name, boolean defuat) {        return getBoolean(context, null, name, defuat);    }    public static boolean getBoolean(Context context, String spName, String name, boolean defuat) {        SharedPreferences sp = getSharedPreferences(context, spName);        return sp.getBoolean(name, defuat);    }    /**     * SharedPreferences保存int类型的     *     * @param name  key     * @param value value     */    public static void saveInt(Context context, String name, int value) {        saveInt(context, null, name, value);    }    public static void saveInt(Context context, String spName, String name,                               int value) {        SharedPreferences sp = getSharedPreferences(context, spName);        Editor editor = sp.edit();        editor.putInt(name, value);        editor.apply();    }    /**     * 获取保上面存的int类型的     *     * @param name   key     * @param defuat 默认值     */    public static int getInt(Context context, String name, int defuat) {        return getInt(context, null, name, defuat);    }    public static int getInt(Context context, String spName, String name, int defuat) {        SharedPreferences sp = getSharedPreferences(context, spName);        return sp.getInt(name, defuat);    }    /**     * SharedPreferences保存Long类型的     *     * @param name  key     * @param value value     */    public static void saveLong(Context context, String name, long value) {        saveLong(context, null, name, value);    }    public static void saveLong(Context context, String spName, String name,                                long value) {        SharedPreferences sp = getSharedPreferences(context, spName);        Editor editor = sp.edit();        editor.putLong(name, value);        editor.apply();    }    /**     * 获取保上面存的Long类型的     *     * @param name key key     */    public static long getLong(Context context, String name, long defuat) {        return getLong(context, null, name, defuat);    }    public static long getLong(Context context, String spName, String name, long defuat) {        SharedPreferences sp = getSharedPreferences(context, spName);        return sp.getLong(name, defuat);    }    /**     * SharedPreferences保存Float类型的     *     * @param name  key key     * @param value value value     */    public static void saveFloat(Context context, String name, float value) {        saveFloat(context, null, name, value);    }    public static void saveFloat(Context context, String spName, String name,                                 float value) {        SharedPreferences sp = getSharedPreferences(context, spName);        Editor editor = sp.edit();        editor.putFloat(name, value);        editor.apply();    }    /**     * 获取保上面存的Float类型的     *     * @param name key     */    public static float getFloat(Context context, String name, float defuat) {        return getFloat(context, null, name, defuat);    }    public static float getFloat(Context context, String spName, String name, float defuat) {        SharedPreferences sp = getSharedPreferences(context, spName);        return sp.getFloat(name, defuat);    }    /**     * SharedPreferences保存String类型的     *     * @param name  key     * @param value value     */    public static void saveString(Context context, String name, String value) {        saveString(context, null, name, value);    }    public static void saveString(Context context, String spName, String name,                                  String value) {        SharedPreferences sp = getSharedPreferences(context, spName);        Editor editor = sp.edit();        editor.putString(name, value);        editor.apply();    }    /**     * 获取保上面存的String类型的     *     * @param name key     */    public static String getString(Context context, String name) {        return getString(context, null, name);    }    public static String getString(Context context, String spName, String name) {        SharedPreferences sp = getSharedPreferences(context, spName);        String res = sp.getString(name, "");        if (TextUtils.isEmpty(res)) {            return "";        }        return res;    }    /**     * 删除name对应的一项     *     * @param name key     */    public static void deleteData(Context context, String name) {        deleteData(context, null, name);    }    public static void deleteData(Context context, String spName, String name) {        SharedPreferences sp = getSharedPreferences(context, spName);        Editor editor = sp.edit();        editor.remove(name);        editor.apply();    }    /**     * 清除所有保存的SharedPreference的内容     */    public static void clear(Context context) {        clear(context, null);    }    /**     * 清空指定的数据库的所有     * @param context     * @param spName     */    public static void clear(Context context, String spName) {        SharedPreferences sp = getSharedPreferences(context, spName);        Editor editor = sp.edit();        editor.clear();        editor.apply();    }

8:具体代码已经给出来了,希望大家能够看懂,也希望能够直接拿来使用。觉得赞的朋友可以顶一下哦。谢谢!

阅读全文
0 0
原创粉丝点击