How Android uses Viewpager to implement user-led interface effects _android

Source: Internet
Author: User

This article is an example of Android using Viewpager to implement user-led interface effects. Share to everyone for your reference, specific as follows:

I believe there are many friends when the software is first opened, a lot of software has a software function introduction,

For example, just complete the micro-letter to open it, there are many pictures to introduce the micro-function, and at the bottom of the screen there are a lot of small dots to indicate the position of your current picture.

That's what I'm going to do today.

The functionality that is implemented:

1. Can slide the function picture.
2. The index of the picture shows the location of the current picture.
3. Can be recycled sliding.
4. The index of the picture has animation effect.

This study is mainly to use Viewpager to achieve user-led interface

Here, we need to use Google mentioned a support package--android-support-v4.jar, this package contains some very useful classes, which is the Viewpager class to achieve the switch between the page, you can go to the official website to download the package Use Google-provided support library (Android)

For more information on Android-support-v4.jar, you can visit the official Google website: http://developer.android.com/sdk/compatibility-library.html

Here is my implementation.

Guideactivity.java

/** * @author manymore13/public class Guideactivity extends activity {//Reach last private static final int to_the
  _end = 0;
  Leave the last one private static final int leave_from_end = 1; 
      If you want to apply directly to your project, just add the delete picture ID here to private int[] ids = {r.drawable.guide_1, r.drawable.guide_3, R.drawable.guide_5,
  R.drawable.guide_6,r.drawable.guide_7};
  Private list<view> guides = new arraylist<view> ();
  Private Viewpager Pager;     Private ImageView start;
  Click Experience Private ImageView Curdot; Private LinearLayout Dotcontain;       Storage point of the container private int offset;     Displacement amount private int curpos = 0; Record the current location/** called when the active is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Requestwindowfeature (Window.feature_no_title);
    Setcontentview (R.layout.main);
  Init ();
    Private ImageView buildimageview (int id) {ImageView IV = new ImageView (this); IV.setimageresource (ID); Viewgroup.layoutparams params = new Viewgroup.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.
    Layoutparams.fill_parent);
    Iv.setlayoutparams (params);
    Iv.setscaletype (SCALETYPE.FIT_XY);
  return IV;
    }//Feature introduction interface initialization private void init () {This.getview ();
    Initdot ();
    ImageView IV = NULL;
    Guides.clear ();
      for (int i = 0; i < ids.length i++) {IV = Buildimageview (Ids[i]);
    Guides.add (iv);
    } System.out.println ("Guild_size=" +guides.size ());
          This method is called Curdot.getviewtreeobserver () when Curdot's tree hierarchy is to be plotted. Addonpredrawlistener (new Onpredrawlistener () {
            public Boolean Onpredraw () {//Gets the width of the ImageView, which is the width of the point picture offset = Curdot.getwidth ();
          return true;
    }
        });
    Final Guidepageradapter adapter = new Guidepageradapter (guides);
    Viewpager sets the data adapter, which is similar to the adapter Pager.setadapter (adapter) used when using ListView; PageR.clearanimation (); Add event listeners to Viewpager Onpagechangelistener Pager.setonpagechangelistener (New Viewpager.simpleonpagechangelistener ()
        {@Override public void onpageselected (int position) {int pos = position% Ids.length;
        Movecursorto (POS);
        if (pos = = ids.length-1) {//to the last one handler.sendemptymessagedelayed (To_the_end, 500);
        else if (CurPos = = ids.length-1) {handler.sendemptymessagedelayed (leave_from_end, 100);
        } CurPos = pos;
      super.onpageselected (position);
  }
    }); /** * Instantiate some view/private void GetView () {Dotcontain = (linearlayout) This.findviewbyid in Layout (r.id.do
    T_contain);
    Pager = (Viewpager) Findviewbyid (R.id.contentpager);
    Curdot = (ImageView) Findviewbyid (R.ID.CUR_DOT);
    Start = (ImageView) Findviewbyid (R.id.open);
    Start.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {//click Experience  }
    }); /** * Initialization Point ImageView * @return return True to indicate that the initialization point succeeded, otherwise the instantiation failed/private Boolean Initdot () {if (Ids.length &G T
      0) {ImageView dotview;
        for (int i=0; i<ids.length; i++) {Dotview = new ImageView (this);
        Dotview.setimageresource (R.DRAWABLE.DOT1_W); Dotview.setlayoutparams (New Linearlayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, VIEWGR Oup.
        layoutparams.wrap_content,1.0f));
      Dotcontain.addview (Dotview);
    return true;
    }else{return false; }/** * Move pointer to adjacent position animation * @param position * Index value */private void Movecursorto (int position) {An
    Imationset animationset = new Animationset (true);
    Translateanimation Tanim = new Translateanimation (offset*curpos, offset*position, 0, 0);
    Animationset.addanimation (Tanim);
    Animationset.setduration (300);
    Animationset.setfillafter (TRUE); Curdot.startanimation (Animationset);
  Handler Handler = new Handler () {@Override public void Handlemessage (msg) {if (Msg.what = =
      To_the_end) start.setvisibility (view.visible);
    else if (msg.what = = leave_from_end) start.setvisibility (View.gone);
  }
  };
    Viewpager Adapter class Guidepageradapter extends pageradapter{private list<view> views;
    Public Guidepageradapter (list<view> views) {this.views=views; @Override public void Destroyitem (View arg0, int arg1, Object arg2) {(Viewpager) arg0). Removeview.
    Get (arg1% views.size ())); @Override public void Finishupdate (View arg0) {} @Override public int getcount () {//Note here must
    Return a slightly larger value, or slide to the top of the slide can not move back views.size () *20;
      @Override public Object Instantiateitem (View arg0, int arg1) {log.e ("tag", "Instantiateitem =" +arg1);
      ((Viewpager) arg0). AddView (Views.get (arg1% views.size ()), 0); Return Views.get (ARG1% views.size ());
    @Override public boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = (arg1); @Override public void Restorestate (parcelable arg0, ClassLoader arg1) {} @Override public parcelabl
    e SaveState () {return null;

 @Override public void Startupdate (View arg0) {}}}

Below is layout main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "
    > <android.support.v4.view.viewpager android:id= "@+id/contentpager" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:layout_gravity= "center" android:flipinterval= "Android:persist" entdrawingcache= "animation"/> <framelayout android:layout_width= "Wrap_content" android:layout_height= P_content "android:layout_alignparentbottom=" true "android:layout_centerhorizontal=" true "android:gravity=" cen ter "android:layout_marginbottom=" 22.0dip "> <linearlayout android:id=" @+id/dot_contain "Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:gravity= "center" android:orient ation= "Horizontal" > </linearlayout> <imageview android:id= "@+id/cur_dot" android:layout_width= "Wrap_content" Android:la yout_height= "Wrap_content" android:src= "@drawable/dot2_w"/> </FrameLayout> <imageview android:i D= "@+id/open" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignPar
    Enttop= "true" android:layout_alignparentright= "true" android:clickable= "true" android:src= "@drawable/ic_open"

 android:visibility= "Gone"/> </RelativeLayout>

Operation Effect:

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary

I hope this article will help you with the Android program.

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.