Package com.rong.activity;import android.content.context;import android.graphics.color;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.VelocityTracker; import android.view.view;import android.view.viewgroup;import android.widget.linearlayout; import android.widget.scroller;public class customviewpager extends viewgroup { Private context mcontext;private int screenwidth;private int screenhight;private int lastmovex = 0;private velocitytracker velocitytracker;private int MAX_VELOCITY=600;private int curScreen=0;private Scroller scroller;public Customviewpager (Context context) {super (Context); Mcontext = context;initview ();} Public customviewpager (context context, attributeset attrs) {super (context, ATTRS); Mcontext = context;initviEW ();} Public customviewpager (Context context, attributeset attrs, int defstyle) { Super (Context, attrs, defstyle); Mcontext = context;initview ();} @Overridepublic boolean ontouchevent (motionevent event) {if (velocitytracker == null) {velocitytracker = velocitytracker.obtain ();} Velocitytracker.addmovement (event);int x = (int) event.getx ();switch (event.getAction ()) {case motionevent.action_down:lastmovex = x;break;case motionevent.action_move: Int dis = lastmovex - x;scrollby (dis, 0); lastmovex = x;break;case motionevent.action_up:velocitytracker.computecurrentvelocity (+); int velocityx= (int) Velocitytracker.getxvelocity (); if (velocityx>max_velocity&&curscreen>0) {jump2Screen (curScreen-1);} Else if (Velocityx<-max_velocity&&curscreen<getchildcount ()-1) {JUMP2SCREen (curscreen+1);} Else{int screen= (GETSCROLLX () +SCREENWIDTH/2)/screenwidth;jump2screen (screen);} if (Velocitytracker != null) {velocitytracker.recycle (); velocitytracker = null;} break;} Return true;} /** * jumps to the specified screen * @param screen */public void jump2screen (int Screen) {curscreen=screen;if (Curscreen>getchildcount ()-1) {Curscreen=getchildcount ()-1;} Int dis=curscreen*screenwidth-getscrollx (); Scroller.startscroll (Getscrollx (), 0, dis, 0); Invalidate ();} @Overridepublic void computescroll () {if (Scroller.computescrolloffset ()) {ScrollTo ( Scroller.getcurrx (), 0);p ostinvalidate ();}} Public void initview () {scroller=new scroller (Mcontext); Linearlayout layout1 = new linearlayout (GetContext ()); Layout1.setbackgroundcolor (Color.RED ); AddView (LAYOUT1); Linearlayout layout2 = new linearlayout (GetContext ()); Layout2.setbackgrouNdcolor (Color.green); AddView (LAYOUT2); Linearlayout layout3 = new linearlayout (GetContext ()); Layout3.setbackgroundcolor ( Color.Blue); AddView (LAYOUT3);} @Overrideprotected void onmeasure (Int widthmeasurespec, int heightmeasurespec) { Super.onmeasure (Widthmeasurespec, heightmeasurespec); Screenwidth = measurespec.getsize ( WIDTHMEASURESPEC); screenhight = measurespec.getsize (Heightmeasurespec); SetMeasuredDimension ( Screenwidth, screenhight);for (Int i = 0; i < getchildcount (); i++) {view view = getchildat (i); View.measure (screenwidth, screenhight);}} @Overrideprotected void onlayout (boolean changed, int l, int t, int &NBSP;R,&NBSP;INT&NBSP;B) {int leftWidth = 0;for (int i = 0; i < getchildcount (); i++) {view view = getchildat (i); View. Layout (Leftwidth, 0, leftwidth + screenwidth, screenhight);leftWidth = Leftwidth + screenwidth;}}}
Android Custom Viewpager