Summary: The project needs to add more than one boot page, so I collected some information on the Internet and sorted it out.
Step1
Add a guideactivity.
In fact, this guide page is nothing more than an activity, there is a viewpager only. Multiple pictures are put in, loaded into the viewpager inside.
Note: Do not set up the picture resource in activity, otherwise it will report Oom exception. Multiple images can cause memory overflow.
==> source code is as follows:
①guideactivity's Kotlin source code
Class Guideactivity:appcompatactivity (), Viewpager.onpagechangelistener {private var vp:viewpager? = null privat e var imageidarray:intarray? = null//An array of picture resources private Var viewlist:mutablelist<view>? = null//collection of picture resources private Var vg:viewgroup? = null//Place Dot//Instantiate Origin view private var iv_point:imageview? = null private var Ivpointarray:array<imageview?>? = NULL//Last page of the button private var Ib_start:button? = null override fun OnCreate (Savedinstancestate:bundle?) {super.oncreate (savedinstancestate) supportrequestwindowfeature (window.feature_no_title) SetContentV Iew (R.layout.activity_guide) mainapplication.getinstance (). Addactivity (This) statusbarutils.setwindowstatusb Arcolor (this, r.color.headline) Ib_start = Findviewbyid (R.id.guide_ib_start) as Button Ib_start!!. Setonclicklistener {val intent:intent=intent ([Email protected],navigationmainactivity::class.java) Intent.putextrA ("Openapp", True) overridependingtransition (r.anim.slide_right_in,r.anim.slide_left_out) StartActivi Ty (Intent) Finish ()}//Load Viewpager Initviewpager ()//Load Bottom dot Initpoint () /** * Load Bottom dot */private fun Initpoint () {//here instantiate linearlayout VG = Findviewbyid (r.id.guide_l L_point) as ViewGroup//instantiate an array based on the item number of Viewpager Ivpointarray = Arrayofnulls (viewlist!!. Size)//loop new Bottom dot ImageView, save the generated imageview to the array val size = Viewlist!!. Size for (i in 0..size-1) {Iv_point = ImageView (this) Iv_point!!. Layoutparams = Viewgroup.layoutparams (iv_point)!!. Setpadding (0,--)//left,top,right,bottom ivpointarray!! [I] = iv_point!! The first page needs to be set to the selected state, where two different images are used if (i = = 0) {Iv_point!!. Setbackgroundresource (R.mipmap.icon_point_pre)} else {Iv_point!!. SetbackgroUndresource (R.mipmap.icon_point)}//Add ImageView in the array to the ViewGroup VG!!. AddView (ivpointarray!! [I]}}}/** * Load picture Viewpager */Private Fun Initviewpager () {VP = Findviewbyid (r.id.guide_ VP) as Viewpager//instanced picture Resource Imageidarray = intarrayof (R.mipmap.intro_school, R.mipmap.intro_discovery_1, R.MI Pmap.intro_discovery_2, R.mipmap.intro_discovery_3,r.mipmap.intro_ask_1,r.mipmap.intro_ask_2,r.mipmap.intro_ask _3,r.mipmap.intro_mew_coin) Viewlist = ArrayList ()//loop CREATE view and add to the collection Val len = Imageidarray!!. Size for (i in 0..len-1) {//new ImageView and set full screen and picture resource Val ImageView = ImageView (This) Viewlist!!. Add (ImageView)}//view when the collection is initialized, set the adapter VP!!. adapter = Guidepageadapter (viewlist) VP!!. Offscreenpagelimit=9//Set Slide monitor VP!!. Setonpagechangelistener (This)} The override Fun Onpagescrolled (Position:int, Positionoffset: Float, Positionoffsetpixels:int) {}/** * swipe to listen * @param position */override Fun onpageselected ( Position:int) {//Loop sets the tag graph for the current page val length = Imageidarray!!. Size for (i in 0..length-1) {ivpointarray!! [Position]!!. Setbackgroundresource (R.mipmap.icon_point_pre) if (position! = i) {ivpointarray!! [I]!!. Setbackgroundresource (R.mipmap.icon_point)}}//To determine whether the last page, if the button is displayed if (position = = ImageId Array!!. size-1) {Ib_start!!. visibility = view.visible} else {Ib_start!!. visibility = View.gone}} override Fun Onpagescrollstatechanged (State:int) {}}
②activity_guide Layout Code:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" tools:context= "com.guangdamiao.www.mew_android_debug.navigation.GuideActivity" > < Android.support.v4.view.ViewPager android:id= "@+id/guide_vp" android:layout_width= "Match_parent" Android oid:layout_height= "Match_parent" > </android.support.v4.view.ViewPager> <linearlayout android:id= " @+id/guide_ll_point "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android : orientation= "Horizontal" android:layout_alignparentbottom= "true" android:layout_marginbottom= "150pt" android:gravity= "Center_horizontal" > </LinearLayout> <button android:layout_width= "500pt" android:layout_height= "55pt" AndroiD:id= "@+id/guide_ib_start" android:text= "Enter the University Meow" android:textsize= "30pt" Android:layout_centerhorizont Al= "true" android:layout_above= "@+id/guide_ll_point" android:background= "@drawable/button_shape_apply" Android:visibility= "Gone"/></relativelayout>
Step2
The second step is to write the Guidepageadapter.
Note Setting the background picture in Instanticateitem (viewgroup container,int position) increases the loading speed and resolves the oom problem.
The source code is as follows:
public class Guidepageadapter extends Pageradapter {private list<view> viewlist; Private int[] mresids=new int[]{r.mipmap.intro_school, R.mipmap.intro_discovery_1, R.MIP Map.intro_discovery_2, R.mipmap.intro_discovery_3, R.mipmap.intro_ask_1, r.mipmap.intro_a Sk_2, R.mipmap.intro_ask_3, R.mipmap.intro_mew_coin,}; Public Guidepageadapter (list<view> viewlist) {this.viewlist = viewlist; }/** * @return Returns the number of pages */@Override public int GetCount () {if (viewlist! = null) {RET Urn Viewlist.size (); } return 0; }/** * Determines whether an object generates an interface * @param view * @param object * @return */@Override public boolean Isview Fromobject (View view, Object object) {return view = = object; /** * Initialize Position location interface * @param container * @param position * @return * * @Override public O BJect Instantiateitem (viewgroup container, int position) {//Set background picture here, increase loading speed, resolve oom problem View View=viewlist.get (p Osition); int Count=getcount (); Viewgroup.layoutparams params = new Viewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); View.setbackgroundresource (Mresids[position%count]); View.setlayoutparams (params); Container.addview (Viewlist.get (position)); return Viewlist.get (position); } @Override public void Destroyitem (ViewGroup container, int position, object object) {Container.removeview ( Viewlist.get (position)); }}
Step3
The third step is to set up the Jump Guide page in the startup page.
The boot page is usually the first time you install the app before it appears, here you can write a sharepreferences file record is not the first installation.
Finally, you can use intent to jump to the boot page.
How to add more than one boot page to Android