From the previous entry into Android, step by step to the present, the process is really difficult to describe, only experienced people know it, understand the initial learning of Android pain, so now in The Android glimpses of the path, back to write some small Demo, I hope to help people in need. I’ll write a lot of examples later when I have time.

Without further ado, this time to explain the APP boot page boot page, I use in the code to add detailed comments to explain the way, for the following API not often appear I have made annotations, next straight to start.

First, the effect map (PS: dynamic map did not have time to do, the time is very late, you can directly download my project running, attached to GitHub link) :

The Java package looks like this:

 

WelcomeActivity.java

package com.example.power.welcomepage;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;

import com.example.power.welcomepage.Activity.MainActivity;
import com.example.power.welcomepage.Activity.WelcomeGuideActivity;
import com.example.power.welcomepage.Util.SharedPreferencesUtil;

public class WelcomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* Start the Activity first and determine if it is the first time you have started the Activity. Note that you need to add default values
        boolean isFirstOpen = SharedPreferencesUtil.getBoolean(this, SharedPreferencesUtil.FIRST_OPEN, true);
        if(isFirstOpen){
            Intent intent = new Intent(this, WelcomeGuideActivity.class);
            startActivity(intent);
            /* Note that you need to destroy the activity with Finish; otherwise, when you press the phone's back button, it will return to the start page */
            finish();
            return;
        }
        /* If the app is not started for the first time, start the page */
        setContentView(R.layout.activity_welcome);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run(a) {
                /*2 seconds after the main page */enterHomeActivity(); }},2000);
    }

    private void enterHomeActivity(a){
        Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); }}Copy the code

WelcomeGuideActivity.java

package com.example.power.welcomepage.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.power.welcomepage.Adapter.GuideViewPagerAdapter;
import com.example.power.welcomepage.R;
import com.example.power.welcomepage.Util.SharedPreferencesUtil;
import com.example.power.welcomepage.WelcomeActivity;

import java.util.ArrayList;
import java.util.List;

/** * Created by Power on 2018/11/2. */

public class WelcomeGuideActivity extends Activity implements View.OnClickListener {
    private ViewPager viewPager;
    private GuideViewPagerAdapter adapter;
    private List<View> views;
    private Button startBtn;

    /* Boot page image resources */
    private static final int[] pics = {  R.layout.guid_view1,
            R.layout.guid_view2, R.layout.guid_view3, R.layout.guid_view4 };

    /* Bottom dot image */
    private ImageView[] dots;

    /* Records the current selected position */
    private int currentIndex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        views = new ArrayList<View>();

        /* Initializes the bootpage view list, which requires processing of resources */
        for(int i = 0; i < pics.length; i++){
            View view = LayoutInflater.from(this).inflate(pics[i], null);

            if(i == pics.length - 1){
                startBtn = (Button)view.findViewById(R.id.btn_login);
                /* The setTag method is used here. SetTag (Onbect) in a View means adding an extra piece of data to the View, which can then be retrieved using getTag(). You can add a listener to multiple buttons, each with a different setTag. The listener uses getTag to determine which Button is pressed. * /
                startBtn.setTag("enter");
                startBtn.setOnClickListener(this);
            }
            views.add(view);
        }

        viewPager = (ViewPager)findViewById(R.id.vp_guide);
        /* Initializes adapter*/
        adapter = new GuideViewPagerAdapter(views);
        viewPager.setAdapter(adapter);
        /* Create a PageChangeListener and implement the OnPageChangeListener interface */
        viewPager.addOnPageChangeListener(new PageChangeListener());
        initDots();
    }

    @Override
    protected void onResume(a) {
        super.onResume();
    }

    @Override
    protected void onPause(a) {
        super.onPause();
        /* If you switch to the background, set the next time not to enter the function boot page */
        SharedPreferencesUtil.setBoolean(WelcomeGuideActivity.this, SharedPreferencesUtil.FIRST_OPEN, false);
        finish();
    }

    @Override
    protected void onStop(a) {
        super.onStop();
    }

    @Override
    protected void onDestroy(a) {
        super.onDestroy();
    }

    private void initDots(a){
        LinearLayout linearLayout = (LinearLayout)findViewById(R.id.ll);
        dots = new ImageView[pics.length];

        /* Loop to get small dots */
        for(int i = 0; i < pics.length; i++){
            /* Get a LinearLayout below each sub-element */
            dots[i] = (ImageView)linearLayout.getChildAt(i);
            dots[i].setEnabled(false);// Set it to grey
            dots[i].setOnClickListener(this);
            dots[i].setTag(i);// Set the position tag, easy to retrieve the current position corresponding to the principle of the above
        }
        currentIndex = 0;
        dots[currentIndex].setEnabled(true); // Set it to white, which means it is selected
    }

    /** * sets the current view **@param position
     */
    private void  setCurrentView(int position){
        if(position < 0 || position > pics.length){
            return;
        }
        viewPager.setCurrentItem(position);
    }

    /** * sets the current indicator **@param position
     */
    private void setCurDot(int position) {
        if (position < 0 || position > pics.length || currentIndex == position) {
            return;
        }
        dots[position].setEnabled(true);
        dots[currentIndex].setEnabled(false);
        currentIndex = position;
    }

    @Override
    public void onClick(View v) {
        if(v.getTag().equals("enter")){
            enterMainActivity();
            return;
        }
        int position = (Integer) v.getTag();
        setCurrentView(position);
        setCurDot(position);
    }

    private void enterMainActivity(a){
        Intent intent = new Intent(WelcomeGuideActivity.this, WelcomeActivity.class);
        startActivity(intent);
        SharedPreferencesUtil.setBoolean(WelcomeGuideActivity.this, SharedPreferencesUtil.FIRST_OPEN, false);
        finish();
    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener{
        /* Call */ when the sliding state changes

        @Override
        public void onPageScrollStateChanged(int state) {
            /*arg0 ==1 is sliding, arg0==2 is sliding, arg0==0 is doing nothing. * /
        }

        */ is called when the current page is swiped

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            Arg0: the current page, and the page you clicked to swipe
            Arg1: the percentage offset of the current page
            Arg2: the pixel position offset from the current page
        }

        */ is called when a new page is selected
        @Override
        public void onPageSelected(int position) { setCurDot(position); }}}Copy the code
SharedPreferencesUtil.java
Copy the code
package com.example.power.welcomepage.Util;

import android.content.Context;
import android.content.SharedPreferences;

/** * Created by Power on 2018/11/2. */

/* We use SharedPreferences for data storage, so we create this tool class for encapsulation operation, more convenient and feasible. * I'm going to use getBoolean and setBoolean as examples here, but the rest work the same way */

public class SharedPreferencesUtil {
    private static final String FILE_NAME = "welcomePage";
    public static final String FIRST_OPEN = "first_open";

    public static Boolean getBoolean(Context context, String strKey, Boolean strDefault){
        /* Create a shared preference file using context. MODE_PRIVATE mode, which means that only the application * can use it, including MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE mode, In both * modes, any other app can access the file by name. * /
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE
        );
        /* Get a Boolean value in the file. StrDefault is the default and can be omitted. It means the return value of the function if the key is not found. * /
        Boolean result = sharedPreferences.getBoolean(strKey, strDefault);

        return result;
    }

    public static Boolean getBoolean(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        Boolean result = setPreferences.getBoolean(strKey, false);
        return result;
    }

    public static void setBoolean(Context context, String strKey, Boolean strData){
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE
        );
        /* To write a value to the shared preference file, we need SharedPreferences.Editor*/
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(strKey, strData);
        editor.commit();
    }

    public static String getString(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        String result = setPreferences.getString(strKey, "");
        return result;
    }

    public static String getString(Context context, String strKey, String strDefault) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        String result = setPreferences.getString(strKey, strDefault);
        return result;
    }

    public static void setString(Context context, String strKey, String strData) {
        SharedPreferences activityPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putString(strKey, strData);
        editor.commit();
    }

    public static int getInt(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        int result = setPreferences.getInt(strKey, -1);
        return result;
    }

    public static int getInt(Context context, String strKey, int strDefault) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        int result = setPreferences.getInt(strKey, strDefault);
        return result;
    }

    public static void setInt(Context context, String strKey, int strData) {
        SharedPreferences activityPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putInt(strKey, strData);
        editor.commit();
    }

    public static long getLong(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        long result = setPreferences.getLong(strKey, -1);
        return result;
    }

    public static long getLong(Context context, String strKey, long strDefault) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                FILE_NAME, Context.MODE_PRIVATE);
        long result = setPreferences.getLong(strKey, strDefault);
        return result;
    }

    public static void setLong(Context context, String strKey, long strData) { SharedPreferences activityPreferences = context.getSharedPreferences( FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = activityPreferences.edit(); editor.putLong(strKey, strData); editor.commit(); }}Copy the code
GuideViewPagerAdapter.java
Copy the code
package com.example.power.welcomepage.Adapter;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/** * Created by Power on 2018/11/2. */

/* We need to use an adapter to make it easier to use ViewPager*/

public class GuideViewPagerAdapter extends PagerAdapter {
    private List<View> views;

    public GuideViewPagerAdapter(List<View> views){
        super(a);this.views = views;
    }

    @Override
    public int getCount(a) {
        if(views ! =null) {return views.size();
        }
        return 0;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        ((ViewPager)container).removeView(views.get(position));
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == ((View)object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ((ViewPager)container).addView(views.get(position), 0);
        returnviews.get(position); }}Copy the code

XML resource file

guid_view1.xml

<? xml version="1.0" encoding="utf-8"? > <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="centerCrop"
        android:src="@mipmap/welcomimg3"/ > <! In order for all images to fill the entire ImageView, use this property. It fills the ImageView with their original proportions and crops out the parts that are out of the screen. --> </LinearLayout>Copy the code

guid_view2.xml

<? xml version="1.0" encoding="utf-8"? > <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="centerCrop"
        android:src="@mipmap/welcomimg5"/>
</LinearLayout>
Copy the code

guid_view3.xml

<? xml version="1.0" encoding="utf-8"? > <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="centerCrop"
        android:src="@mipmap/welcomimg6"/>
</LinearLayout>
Copy the code

guid_view4.xml

<? xml version="1.0" encoding="utf-8"? > <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <! Use FrameLayout for the fourth View layout (the layout where the buttons are placed). Because you need a Button on top of the ImageView, a normal linear layout cannot do this, so use FrameLayout. <ImageView Android :layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@mipmap/welcomimg11"/ > <! <Button android:id= Button android:id= Button android:id="@+id/btn_login"
        android:layout_width="160dp"
        android:layout_height="42dp"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="100dp"
        android:background="@drawable/button_shape"
        android:text="Experience it now"
        android:textColor="#ce102c"
        android:textSize="18sp"
        android:visibility="visible"/>

</FrameLayout>
Copy the code

activity_guide.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">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:id="@+id/vp_guide"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24.0 dip"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:clickable="true"
            android:padding="10.0 dip"
            android:src="@drawable/dot_selector" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:clickable="true"
            android:padding="10.0 dip"
            android:src="@drawable/dot_selector" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:clickable="true"
            android:padding="10.0 dip"
            android:src="@drawable/dot_selector" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:clickable="true"
            android:padding="10.0 dip"
            android:src="@drawable/dot_selector" />
    </LinearLayout>

</RelativeLayout>
Copy the code

The above is the core code, want to understand the whole project partners, you can download the source code to view oh, there are detailed annotations.