2 ways to implement real-time sliding viewpager on Android _android

Source: Internet
Author: User

First look at the effect chart:

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context" = "Com.example.welcome.MainActivity" > <android.support.v4.view.viewpager android:id= "@+id/mviewpager" Androi D:layout_width= "Fill_parent" android:layout_height= "fill_parent"/> <framelayout android:layout_width= "Fill_" Parent "android:layout_height=" Wrap_content "android:layout_alignparenttop=" true "android:layout_centerhorizontal 
  = "true" android:layout_margintop= "30DP" > <com.example.welcome.pagercursor android:id= "@+id/pagercursor" Android:layout_width= "Fill_parent" android:layout_height= "5DP"/> </FrameLayout> <framelayout Andr 
 Oid:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" Android:layout_centerhorizontal= "true" Android:layout_marginbottom= "30DP" > <com.example.welcome.customedotgroup android:id= "@+id/ll_point_group" Android:layout_w Idth= "Fill_parent" android:layout_height= "10DP" android:orientation= "Horizontal" > </com.example.welcome.cus tomedotgroup> <view android:id= "@+id/red_point" android:layout_width= "10DP" android:layout_height= "10DP  "Android:background=" @drawable/point_red "/> </FrameLayout> </RelativeLayout>

Mainactivity

Package com.example.welcome; 
 
Import java.util.ArrayList; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.support.v4.view.PagerAdapter; 
Import Android.support.v4.view.ViewPager; 
Import Android.support.v4.view.ViewPager.OnPageChangeListener; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import android.view.animation.TranslateAnimation; 
 
Import Android.widget.ImageView; 
 
 public class Mainactivity extends activity {private Viewpager Mviewpager; 
 Myadapter Madapter; 
 Private arraylist<imageview> imageviewlist; 
 View Red_point; 
 private int Lastdis; 
 Pagercursor Pagercursor; @SuppressWarnings ("deprecation") @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (saved 
 Instancestate); 
 Setcontentview (R.layout.activity_main); 
 Pagercursor = (pagercursor) Findviewbyid (r.id.pagercursor); 
 Red_point = Findviewbyid (r.id.red_point); 
 Imageviewlist = new arraylist<imageview> (); int[] Imagesint = {R.DRAWABLE.P1,R.DRAWABLE.P2,R.DRAWABLE.P3}; 
  for (int i = 0; i < imagesint.length i++) {ImageView ImageView = new ImageView (mainactivity.this); 
  Imageview.setbackgroundresource (Imagesint[i]); 
  
 Imageviewlist.add (ImageView); 
  
 } Mviewpager = (Viewpager) Findviewbyid (R.id.mviewpager); 
  
 Madapter = new Myadapter (); 
  
 Mviewpager.setadapter (Madapter); 
  
 
  Mviewpager.setonpagechangelistener (New Onpagechangelistener () {private Boolean isdragging; @Override public void onpageselected (int arg0) {} @Override public void onpagescrolled (int position, F 
  Loat screenoffset, int arg2) {translateanimation animation = null; 
   if (isdragging) {int dp2px = phoneutils.dp2px (Mainactivity.this, 20f); 
   
   int offSet = (int) (position*dp2px + dp2px*screenoffset); 
   Animation = new Translateanimation (Lastdis, OffSet, 0f, 0f); 
   Animation.setduration (200); 
   Animation.setfillafter (TRUE); 
Red_point.startanimation (animation);   
   Lastdis = OffSet; 
  } float[] ScreenSize = measureutil.getscreensize (mainactivity.this); 
   
  float itemwidth = SCREENSIZE[0]/3; 
   
  Pagercursor.setoffset (Position,screenoffset,itemwidth); @Override public void onpagescrollstatechanged (int arg0) {switch (arg0) {case VIEWPAGER.SCROLL_STATE_DR 
   agging://sliding isdragging = true; 
  Break 
   Case viewpager.scroll_state_idle://Idle isdragging = false; 
 
  Break 
  Default:break; 
 } 
  } 
 }); 
 Class Myadapter extends pageradapter{@Override public int GetCount () {return imageviewlist.size (); 
 @Override public boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = Arg1; @Override public void Destroyitem (ViewGroup container, int position, object object) {Container.removeview (IMA 
 Geviewlist.get (position)); @Override public Object Instantiateitem (viewgroup container, int position) {Container.addview (imageviewlist. get (position)); 
 return Imageviewlist.get (position);  } 
  
 } 
}

Customedotgroup

Package com.example.welcome; 
Import Android.content.Context; 
Import Android.util.AttributeSet; 
Import Android.view.View; 
Import Android.widget.ImageView; 
 
Import Android.widget.LinearLayout; 
 public class Customedotgroup extends LinearLayout {private context context; 
 Public Customedotgroup (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
 This.context = context; 
 Initres (); 
 Public Customedotgroup (context, AttributeSet attrs) {This (context, attrs, 0); 
 The public Customedotgroup {This (context, NULL); 
  
 private void Initres () {int dotwidthorheight = phoneutils.dp2px (context, 10); 
  for (int i = 0; i < 3; i++) {ImageView Dotimageview = new ImageView (context); 
  
  Dotimageview.setbackgroundresource (R.drawable.point_normal); 
  Layoutparams dotimageviewparams = new Layoutparams (dotwidthorheight, dotwidthorheight); if (I!= 0) {Dotimageviewparams.leftmargin = DotwiDthorheight; 
  } dotimageview.setlayoutparams (Dotimageviewparams); 
 This.addview (Dotimageview); } 
  
 } 
 
}

 pagercursor

Package com.example.welcome; 
Import android.app.Activity; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.graphics.Paint; 
Import Android.util.AttributeSet; 
Import Android.util.Log; 
 
Import Android.view.View; 
 public class Pagercursor extends View {context context; 
 Private Paint Mpaint; 
 private int screenwidth; 
 private int desright; 
 
 private float itemwidth; 
 Public Pagercursor (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);  TODO auto-generated Constructor stub} public pagercursor (context, AttributeSet attrs) {Super (context, 
 ATTRS); 
 This.context = context; 
 Initres (); 
 TODO auto-generated Constructor stub} public pagercursor (context) {super (context); TODO auto-generated constructor stub} private void Initres () {float[] screensize = Measureutil.getscreensize ( 
 (activity) context); 
 ScreenWidth = (int) screensize[0]; LOG.D ("TAG", ",ScreenWidth "+ screenwidth); 
 Mpaint = new Paint (); 
 Mpaint.setcolor (Getresources (). GetColor (R.color.contentpresscolor)); 
 Mpaint.setstyle (Paint.Style.FILL); 
 Mpaint.setantialias (TRUE); public void SetOffset (int position, float screenoffset, float itemwidth) {int offSet = (int) (position * Itemwidt 
 H + itemwidth * screenoffset); 
 This.itemwidth = Itemwidth; 
 Desright = OffSet; LOG.D ("TAG", "Screenoffset:" + Screenoffset + ", position" + position + ", desrigh-->t" + Desright + ", itemwidth" + it 
 
 Emwidth); 
 Invalidate (); 
 } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
 LOG.D ("TAG", "ondraw,desrigh-->t" + Desright + ", itemwidth" + itemwidth); 
 Canvas.drawrect (Desright, 0f, Desright + itemwidth, phoneutils.dp2px (context, 5), mpaint);  } 
}

Measureutil

Package com.example.welcome; 
 
Import android.app.Activity; 
Import Android.util.DisplayMetrics; 
 
/** 
 * Surveying tools? 
 * * Public 
Final class Measureutil { 
 /** 
 * Get screen size 
 * * 
 @param 
 activity * @return screen size pixel value, subscript? is wide, subscript 1? For high 
 * 
 /public static float[] Getscreensize (activity activity) { 
 Displaymetrics metrics = new Displaymetrics (); 
 Activity.getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics); 
 return new float[] {metrics.widthpixels, metrics.heightpixels}} 
 

Phoneutils

Package com.example.welcome; 
 
Import Android.content.Context; 
 
public class Phoneutils {public 
 
 static int dp2px (context context,float dpvalue) { 
 float scale = Context.getreso Urces (). Getdisplaymetrics (). density; 
 return (int) (Dpvalue * scale +0.5f); 
  

SOURCE Download: Http://xiazai.jb51.net/201610/yuanma/androidviewpaper (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.