Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android to implement gradient startup page and boot page with indicator

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use Android to achieve a gradient startup page and a guide page with indicators. I hope you will get something after reading this article. Let's discuss it together.

Implementation steps:

1. First of all, let's make a startup page SplashActivity with gradient animation.

Set the core method setAlphaAnimation () in onCreate

Public void setAlphaAnimation () {/ / generate the AlphaAnimation object AlphaAnimation animation= new AlphaAnimation (this); / / set the duration of the animation animation.setDuration (3000); / / animate the controls to be graded, such as ll.setAnimation (animation) such as imageview,textview,linearLayout / / set animated listening, and jump to the next page at the end (the first opening is the boot page, and vice versa) animation.setAnimationListener (new Animation.AnimationListener () {public void onAnimationStart (Animation animation) {} public void onAnimationEnd (Animation animation) {jump2Activity ();} public void onAnimationRepeat (Animation animation) {}});}

Analyze this jump method jump2Activity (), we use SharedPeference here to determine whether the application is opened for the first time, set the default value of isFirst to 0, enter the boot page to jump to the home page and then set this value to 1, so that each jump to determine the value of isFirst, if it is still the default value of 0 for the first time to open to enter the boot page, otherwise enter the home page.

Public void jump2Activity () {SharedPreferences sharedPreference= getSharedPreferences ("data", MODE_PRIVATE); String isFirst= sharedPreferences.getString ("isFirst", "0"); Intent intent= new Intent (); if ("0" .equals (isFirst)) {intent.setClass (this, GuideActivity.class);} else {intent.setClass (this. MainActivity.class);} startActivity (intent); finish ();}

two。 Next, let's do the guide page.

The guide page is made up of three controls, Viewpager, the linear layout linearlayout of the dot indicator, and the "enter application" button on the last page.

In GuideActivity, initialize the boot picture first

/ * initialize the picture * / private void initImgs () {ViewPager.LayoutParams params= new ViewPager.LayoutParams (); imageViews= new ArrayList (); for (int I = 0; I < imgs.length; iTunes +) {ImageView imageView= new ImageView (this); imageView.setLayoutParams (params); imageView.setImageResource (imgs [I]); imageView.setScaleType (ImageView.ScaleType.FIT_XY); imageViews.add (imageView);}}

Initialize the bottom dot indicator, it is worth mentioning that we set the corresponding click event for each dot. When clicking on the dot of a certain location, viewpager automatically switches to the picture of the corresponding location, but in practical application, the practicality here is not very great, because the dot is too small, the touch range is limited, and the click event is not easy to trigger.

/ * initialize the bottom dot indicator * / private void initDots () {LinearLayout layout= findViewById (R.id.guide_ll); LinearLayout.LayoutParams params= new LinearLayout.LayoutParams (20,20); params.setMargins (10,0,10,0); dotViews= new ImageView [imgs.length]; for (int I = 0; I < imageViews.size (); iDot +) {ImageView imageView= new ImageView (this); imageView.setLayoutParams (params); imageView.setImageResource (R.drawable.dotselector); if (iDot = 0) {imageView.setSelected (true) } else {imageView.setSelected (false);} dotViews [I] = imageView; final int finalI = I; dotViews [I] .setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View view) {vp.setCurrentItem (finalI);}}); layout.addView (imageView);}}

Set the sliding event for viewpager

Vp.addOnPageChangeListener (this)

Generate three methods, we mainly do the operation in the onPageSelected () method, when the dot in a certain position is selected, the selected picture is displayed, and the other dots show the unselected picture, here the selector controller is mainly applied, as for the corresponding unselected dot picture, you need to find it. When you swipe to the last page, the "enter the app" button is displayed, otherwise hidden.

@ Overridepublic void onPageScrolled (int I, float v, int i1) {} @ Overridepublic void onPageScrollStateChanged (int I) {} @ Overridepublic void onPageSelected (int arg0) {for (int I = 0; I < dotViews.length; I +) {if (arg0== I) {dotViews [I] .setSelected (true);} else {dotViews [I] .setSelected (false);} if (arg0== dotViews.length- 1) {btn.setVisibility (View.VISIBLE);} else {btn.setVisibility (View.GONE);}

DotSelector.xml

When you click the "enter App" button on the last page to jump to the home page, change the isFirst data in the cache to 1, and when you open the app later, you will no longer enter the boot page.

@ Overridepublic void onClick (View view) {switch (view.getId ()) {case R.id.guide_btn: setisFirst (); Intent intent= new Intent (GuideActivity.this, MainActivity.class); startActivity (intent); finish (); break;}} / * * change the first open state * / private void setisFirst () {SharedPreferences.Editor editor= getSharedPreferences ("data", MODE_PRIVATE). Edit (); editor.putString ("isFirst", "1"); editor.commit ();}

After reading this article, I believe you have a certain understanding of "how to use Android to achieve gradient startup page and guide page with indicator". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report