Android launch screen to achieve left and right sliding switch view function _android

Source: Internet
Author: User

This article introduces one of the most common features of the app, that is, the new features of the introduction and launch screen, generally how to achieve it, this is not going to tell you.

Logic first.

    • First to determine whether the first launch of the app, if it is, then enter the function to use navigation (the simplest way is, left and right sliding switch view, slide to the last page click button to enter the homepage).
    • If not, display the splash screen, and then enter the home page in 2 seconds.

The logic is very simple, what if there is advertising? Advertising must be taken from the server, but will be cached to the local, no network can be displayed, you can use WebView to display ads, anyway, the author is doing so, concrete implementation first.

Look at the effect

On the 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; public class Splashactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super.onc
 Reate (savedinstancestate);
 Judge whether it is the first time to open the application Boolean Isfirstopen = Sputils.getboolean (this, appconstants.first_open);
  If this is the first time you start, go to the Functional boot page if (!isfirstopen) {Intent Intent = new Intent (this, welcomeguideactivity.class);
  StartActivity (Intent);
  Finish ();
 Return

 ///If it is not the first time the app is started, the boot screen Setcontentview (r.layout.activity_splash) is displayed normally;
  New Handler (). postdelayed (New Runnable () {@Override public void run () {enterhomeactivity ();
 }, 2000);
 private void Enterhomeactivity () {Intent Intent = new Intent (this, mainactivity.class);
 StartActivity (Intent);
 Finish ();
 }
}

Code Resolution: use Sharedpreference to save the app startup state and, if true, go to function navigation, or delay 2 seconds before entering the main page.

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;
 public class Welcomeguideactivity extends activity implements Onclicklistener {private Viewpager VP;
 Private Guideviewpageradapter adapter;
 Private list<view> views;

 Private Button startbtn; Guide page picture resources private static final int[] Pics = {r.layout.guid_view1, r.layout.guid_view2, R.LAYOUT.GUID_VIEW3, R.layout.

 GUID_VIEW4};

 Bottom dot picture private imageview[] dots; Record currently selected location private int CurrenTindex;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

 Setcontentview (R.layout.activity_guide);

 views = new arraylist<view> ();

  Initializes a list of boot page views 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 adapter = new Guideviewpageradapter (views);
 Vp.setadapter (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]; Loop to get the dot picture for (int i = 0; i < pics.length i++) {//Get a linearlayout below each child element dots[i] = (ImageView) ll.getchild
  at (i);
  Dots[i].setenabled (false);//all set to Gray dots[i].setonclicklistener (this);
 Dots[i].settag (i)//Set position tag, convenient to remove the corresponding with the current position} currentindex = 0; Dots[currentindex].setenabled (TRUE); Set to White, select State}/** * Set Current View * * @param position/private void Setcurview (int position) {if position ; 0 | |
 Position >= pics.length) {return;
 } vp.setcurrentitem (position); /** * Sets the current indication 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.putboolean (Welcomeguideactivity.this, Appconstants.first_open, true);
 Finish (); Private class Pagechangelistener implements Onpagechangelistener {//When the sliding state is changed, call @Override public void Onpagescroll

 statechanged (int position) {//arg0 ==1 's Hour is slipping, the arg0==2 's Hour is slipping, and arg0==0 's Hour is silent.
  ///The current page is sliding when called @Override public void onpagescrolled (int position, float arg1, int arg2) {//arg0: Current page, and you click on the sliding page  Arg1: Percent of current page offset//arg2: Pixel position of current page offset}//When new page is selected @Override public void onpageselected (int position) {//
 Set the bottom small point selected state setcurdot (position);

 }

 }
}

Code Analysis: left-right sliding is used Viewpager to do, switch 4 different view, monitor the Viewpager page toggle event to change the bottom point of the switch, slide to the last page, set the button click event, click to enter the home page.

This is the entire content of this article, I hope to learn more about Android software programming help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.