This article shares the specific code of Android simple implementation boot page for your reference. The specific content is as follows
1. Ideas
We choose ViewPager + View + ImageView to achieve the boot page effect, ViewPager is used to achieve sliding, View is used to display the images of each page, and ImageView is used to implement the small red dots below.
2.XML code
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro xmlns:app="/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <. android: android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:layout_alignParentBottom="true"> <ImageView android: android:layout_width="15dp" android:layout_height="15dp" android:layout_margin="10dp"/> <ImageView android: android:layout_width="15dp" android:layout_height="15dp" android:layout_margin="10dp"/> <ImageView android: android:layout_width="15dp" android:layout_height="15dp" android:layout_margin="10dp"/> <ImageView android: android:layout_width="15dp" android:layout_height="15dp" android:layout_margin="10dp"/> </LinearLayout> </RelativeLayout>
Write as many ImageView as you have. The main reason for using relative layout here is to make the small red dots located at the bottom of the parent layout.
3. Implement code
1. Customize ViewPagerAdapter
class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return (); // List<View> list = new ArrayList<>(); } @Override public boolean isViewFromObject(View p1, Object p2) { return p1 == p2; // Determine whether the current object corresponds to the corresponding view and returns a Boolean value } @Override public Object instantiateItem(ViewGroup container, int position) { ((position)); // Add a View return (position); // Return a View } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((View)object); // Destroy a View because it is Object type, so it needs to be transformed into a View } }
2. Custom setImageView is used to realize the red dot display below
private void setImageView(boolean bool1, boolean bool2, boolean bool3, boolean bool4){ if(bool1){ (); (); (); (); }else if(bool2){ (); (); (); (); }else if(bool3){ (); (); (); (); }else if(bool4){ (); (); (); (); } }
It is easy to understand here. If there are several pages, you will pass in several parameters. The parameter types are all Boolean. When the first page is, the first parameter should be true and all the following are false. The principle behind them are the same. Then the ImageView display can be directly set with two images, but I use the color directly without the image.
3. Set ViewPager listening
(new (){ @Override public void onPageScrolled(int p1, float p2, int p3) { } @Override public void onPageSelected(int p1) { switch(p1){ case 0: setImageView(true,false,false,false); break; case 1: setImageView(false,true,false,false); break; case 2: setImageView(false,false,true,false); break; case 3: setImageView(false,false,false,true); break; } } @Override public void onPageScrollStateChanged(int p1) { } });
A switch is written in onPageSelected to obtain the current corresponding page and let the small red dots below follow the changes.
4. Complete code
public class MainActivity extends AppCompatActivity { ViewPager viewPager; List<View> list = new ArrayList<>(); View view1, view2, view3, view4; ImageView image1, image2, image3, image4; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); initView(); } private void initView(){ view1 = (this, .view1, null); view2 = (this, .view2, null); view3 = (this, .view3, null); view4 = (this, .view4, null); image1 = findViewById(.image1); image2 = findViewById(.image2); image3 = findViewById(.image3); image4 = findViewById(.image4); viewPager = findViewById(); (view1); (view2); (view3); (view4); (new ViewPagerAdapter()); setImageView(true,false,false,false); (new (){ @Override public void onPageScrolled(int p1, float p2, int p3) { } @Override public void onPageSelected(int p1) { switch(p1){ case 0: setImageView(true,false,false,false); break; case 1: setImageView(false,true,false,false); break; case 2: setImageView(false,false,true,false); break; case 3: setImageView(false,false,false,true); break; } } @Override public void onPageScrollStateChanged(int p1) { } }); } private void setImageView(boolean bool1, boolean bool2, boolean bool3, boolean bool4){ if(bool1){ (); (); (); (); }else if(bool2){ (); (); (); (); }else if(bool3){ (); (); (); (); }else if(bool4){ (); (); (); (); } } class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return (); } @Override public boolean isViewFromObject(View p1, Object p2) { return p1 == p2; } @Override public Object instantiateItem(ViewGroup container, int position) { ((position)); return (position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((View)object); } } }
4. Summary
We used ViewPager + View + ImageView to simply implement the boot page effect. Of course, we can also use ViewPager + Fragment + ImageView. This depends on personal habits. The implementation of boot page is not difficult. As long as we can master the usage method of ViewPager.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.