Android tip 1: enable screen + new features left-right navigation
Preface
I haven't written a blog for a long time. If I don't want to write anything, I can't say that I can't go over it. I want to sort out some valuable work and share some valuable work. Now I have some timeliness, or sooner or later it will get worse. I still remember that Xiao Wu had an open-source plan to develop an app for a week. Now I picked it up. The plan was not implemented because of my laziness, all great things can be achieved only through powerful execution. It's okay to create something slowly. It's worthwhile to create something.
This blog first introduces the most common features of an app, that is, the introduction of new function attributes and startup screens. How can this be achieved? I am not going to tell you about it.
First, let's use the logic to determine whether to start the app for the first time. If yes, go to the function usage navigation page (the simplest way is to slide between left and right to view the page, move to the last page, and click the button to enter the homepage ). If not, the startup screen is displayed, and the homepage is displayed 2 seconds later.
The logic is simple. What if there is an advertisement? The advertisement is definitely taken from the server, but it will be cached locally. It can be displayed when there is no network. You can use webView to display the advertisement. I did this anyway. Let's not talk about the specific implementation first.
View results
Code SplashActivity. java
Package com. devilwwj. featureguide; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. OS. handler; import com. devilwwj. featureguide. global. appConstants; import com. devilwwj. featureguide. utils. spUtils;/*** @ desc startup screen * Created by devilwwj on 16/1/23. */public class SplashActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {Super. onCreate (savedInstanceState); // determines whether to enable the application boolean isFirstOpen = SpUtils for the first time. getBoolean (this, AppConstants. FIRST_OPEN); // if this is the first startup, go to the feature boot page first if (! IsFirstOpen) {Intent intent = new Intent (this, WelcomeGuideActivity. class); startActivity (intent); finish (); return;} // If the app is not started for the first time, the startup screen setContentView (R. layout. activity_splash); new Handler (). postDelayed (new Runnable () {@ Override public void run () {enterHomeActivity () ;}, 2000) ;} private void enterHomeActivity () {Intent intent Intent = new Intent (this, mainActivity. class); startActivity (intent); finish ();}}
Code parsing: Use SharedPreference to save the app startup status. If it is set to true, go to the function navigation page. Otherwise, the page will be displayed after 2 seconds.
WelcomeGuideActivity. java
Package com. devilwwj. featureguide; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. linearLayout; import com. devilwwj. featureguide. global. appConstants; import com. devilwwj. featureguide. utils. spUtils; import java. util. arrayList; import java. util. list;/*** welcome page ** @ author wwj_748 **/public class WelcomeGuideActivity extends Activity implements OnClickListener {private ViewPager vp; private GuideViewPagerAdapter adapter; private List
Views; private Button startBtn; // picture resources on the boot page private static final int [] pics = {R. layout. guid_view1, R. layout. guid_view2, R. layout. guid_view3, R. layout. guid_view4}; // The image private ImageView [] dots at the bottom; // record the selected position private int currentIndex; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_guide); views = new ArrayList
(); // Initialize the boot Page View list 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); startBtn. setTag ("enter"); startBtn. setOnClickListener (this);} views. add (view);} vp = (ViewPager) findViewById (R. id. vp_guide); // initialize adapter = new GuideViewPagerAdapter (views); vp. setAdapte R (adapter); vp. setOnPageChangeListener (new PageChangeListener (); initDots () ;}@ Override protected void onResume () {super. onResume () ;}@ Override protected void onPause () {super. onPause (); // If you switch to the background, set the next time you do not enter the function boot page SpUtils. putBoolean (WelcomeGuideActivity. this, AppConstants. FIRST_OPEN, true); finish () ;}@ Override protected void onStop () {super. onStop () ;}@ Override protected void onDestroy (){ Super. onDestroy ();} private void initDots () {LinearLayout ll = (LinearLayout) findViewById (R. id. ll); dots = new ImageView [pics. length]; // cyclically retrieve the smaller image for (int I = 0; I <pics. length; I ++) {// obtain the dots [I] = (ImageView) ll sub-element under LinearLayout. getChildAt (I); dots [I]. setEnabled (false); // both are set to gray dots [I]. setOnClickListener (this); dots [I]. setTag (I); // set the location tag for easy retrieval.} currentIndex = 0; dots [curren TIndex]. setEnabled (true); // set to white, that is, the selected status}/*** set the current view ** @ param position */private void setCurView (int position) {if (position <0 | position> = pics. length) {return;} vp. setCurrentItem (position);}/*** sets the current indicator point ** @ 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 (); setCurView (position); setCurDot (position);} private void enterMainActivity () {Intent intent = new Intent (WelcomeGuideActivity. this, SplashActivity. class); startActivity (intent); SpUtils. put Boolean (WelcomeGuideActivity. this, AppConstants. FIRST_OPEN, true); finish ();} private class PageChangeListener implements OnPageChangeListener {// call @ Override public void onPageScrollStateChanged (int position) when the sliding status changes) {// arg0 = 1 when the implied is sliding, arg0 = 2 when the implied sliding is completed, arg0 = 0 when the implied nothing.} // Call @ Override public void onPageScrolled (int position, float arg1, int arg2) when the current page is slide {// arg0: Current page, and the page you click to slide // arg1: Percentage of the current page offset // arg2: the pixel position of the current page offset} // when a new page is selected, @ Override public void onPageSelected (int position) is called) {// setCurDot (position );}}}
Code parsing: ViewPager is used for sliding between the left and right. Four different views are switched, and the page switching events of ViewPager are monitored to change the switch of the bottom indicator point and slide to the last page, click the button event to go to the homepage.
Github
For more details about the code, see the source project. The Code has been uploaded to github. You are welcome to go down and use it.
One-week app development