欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现实时滑动ViewPager的2种方式

程序员文章站 2024-03-02 18:35:34
先看看效果图: activity_main.xml 

先看看效果图:

Android实现实时滑动ViewPager的2种方式

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" 
 android: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 
 android: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_width="fill_parent" 
  android:layout_height="10dp" 
  android:orientation="horizontal" > 
 </com.example.welcome.customedotgroup> 
 
 <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(savedinstancestate); 
 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, float 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_dragging://滑动 
   isdragging = true; 
   break; 
  case viewpager.scroll_state_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(imageviewlist.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 context, attributeset attrs, int defstyle) { 
 super(context, attrs, defstyle); 
 this.context = context; 
 initres(); 
 } 
 
 public customedotgroup(context context, attributeset attrs) { 
 this(context, attrs, 0); 
 } 
 
 public customedotgroup(context context) { 
 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 context, attributeset attrs, int defstyle) { 
 super(context, attrs, defstyle); 
 // todo auto-generated constructor stub 
 } 
 
 public pagercursor(context context, attributeset attrs) { 
 super(context, attrs); 
 this.context = context; 
 initres(); 
 // todo auto-generated constructor stub 
 } 
 
 public pagercursor(context 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 * itemwidth + itemwidth * screenoffset); 
 this.itemwidth = itemwidth; 
 desright = offset; 
 log.d("tag", "screenoffset:" + screenoffset + ",position" + position 
  + ",desrigh-->t" + desright + ",itemwidth" + itemwidth); 
 
 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; 
 
/** 
 * 测绘工具�? 
 */ 
public final class measureutil { 
 /** 
 * 获取屏幕尺寸 
 * 
 * @param activity 
 *  activity 
 * @return 屏幕尺寸像素值,下标�?的�?为宽,下标为1的�?为高 
 */ 
 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.getresources().getdisplaymetrics().density; 
 return (int)(dpvalue * scale +0.5f); 
 } 
} 

源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。