Effect 1
Effect 2
Androidmanifest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn.npass.nj" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
Scroll_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <com.cn.npass.nj.ScrollViewGroup android:id="@+id/scrollViewGroup" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <com.cn.npass.nj.PageControlView android:id="@+id/pageControl" android:layout_width="fill_parent" android:layout_height="80px" android:layout_alignParentBottom="true" android:background="#8f00000f" android:gravity="center" /></RelativeLayout>
Scroll_layout_0.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Enter name:"/> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = ""/> <button Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "Submit"/> </linearlayout>
Mainactivity. Java
Package com.cn. npass. NJ; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. imageview;/***/public class mainactivity extends activity {@ suppresswarnings ("UNUSED") Private Static final string tag = "scroller"; private scrollviewgroup viewgroup; private pagecontrolview pagecontrol; private int [] argimages = {R. drawable. a1, R. drawable. a2, R. drawable. a3, R. drawable. a4}; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. scroll_main); viewgroup = (scrollviewgroup) findviewbyid (R. id. scrollviewgroup); imageview = new imageview (this); imageview. setimagedrawable (getresources (). getdrawable (R. drawable. a1); viewgroup. addview (imageview); viewgroup. addview (view. inflate (this, R. layout. scroll_layout_0, null); imageview = new imageview (this); imageview. setimagedrawable (getresources (). getdrawable (R. drawable. a1); viewgroup. addview (imageview); imageview = new imageview (this); imageview. setimagedrawable (getresources (). getdrawable (R. drawable. ic_launcher); viewgroup. addview (imageview); For (INT I = 0; I <argimages. length; I ++) {imageview = new imageview (this); imageview. setimagedrawable (getresources (). getdrawable (argimages [I]); viewgroup. addview (imageview);} // sets the image to be filled with imageview // imageview. setscaletype (scaletype. center); viewgroup. setcurrentscreenindex (0); pagecontrol = (pagecontrolview) findviewbyid (R. id. pagecontrol); pagecontrol. bindscrollviewgroup (viewgroup );}}
Pagecontrolview. Java
package com.cn.npass.nj;import android.content.Context;import android.util.AttributeSet;import android.widget.ImageView;import android.widget.LinearLayout;import com.cn.npass.nj.ScrollViewGroup.OnScreenChangeListener;/** */public class PageControlView extends LinearLayout {private Context context;private int count;public void bindScrollViewGroup(ScrollViewGroup scrollViewGroup) {this.count = scrollViewGroup.getChildCount();generatePageControl(scrollViewGroup.getCurrentScreenIndex());scrollViewGroup.setOnScreenChangeListener(new OnScreenChangeListener() {@Overridepublic void onScreenChange(int currentIndex) {generatePageControl(currentIndex);}});}public PageControlView(Context context) {super(context);this.init(context);}public PageControlView(Context context, AttributeSet attrs) {super(context, attrs);this.init(context);}private void init(Context context) {this.context = context;}private void generatePageControl(int currentIndex) {this.removeAllViews();for (int i = 0; i < this.count; i++) {ImageView imageView = new ImageView(context);if (currentIndex == i) {imageView.setImageResource(R.drawable.page_indicator_focused);} else {imageView.setImageResource(R.drawable.page_indicator);}this.addView(imageView);}}}
Scrollviewgroup. Java
Package com.cn. npass. NJ; import android. content. context; import android. util. attributeset; import android. util. log; import android. view. gesturedetector; import android. view. gesturedetector. ongesturelistener; import android. view. motionevent; import android. view. view; import android. view. viewconfiguration; import android. view. viewgroup; import android. widget. scroller;/***/public class scrollviewgroup extends Viewgroup {Private Static final string tag = "scroller"; private scroller; private int currentscreenindex; // public int getcurrentscreenindex () {return currentscreenindex ;} public void setcurrentscreenindex (INT currentscreenindex) {This. currentscreenindex = currentscreenindex;} private gesturedetector; // set a flag to prevent the underlying ontouch event from repeatedly handling up events private Boolean fling; Public scrollvie Wgroup (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle); initview (context);} public scrollviewgroup (context, attributeset attrs) {super (context, attrs); initview (context);} public scrollviewgroup (context) {super (context); initview (context);} private void initview (final context) {This. scroller = new scroller (context); this. gesturedetector = new GE Sturedetector (New Role () {@ overridepublic Boolean onsingletapup (motionevent e) {return false ;}@ overridepublic void onshowpress (motionevent e) {}@ overridepublic Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {If (distancex> 0 & currentscreenindex <getchildcount ()-1) // prevent moving the last page | (distancex <0 & getscrollx ()> 0) {// prevent moving scrollby (INT) before the first page) Distancex, 0);} return true;} @ overridepublic void onlongpress (motionevent e) {}@ overridepublic Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {log. D (TAG, "Min velocity >>>" + viewconfiguration. get (context ). getscaledminimumflingvelocity () + "current velocity>" + velocityx); // you can determine whether the minimum and Easy speed is reached and if (math) of the absolute value is obtained. ABS (velocityx)> viewconfiguration. get (context ). getscaledminimum Flingvelocity () {If (velocityx> 0 & currentscreenindex> 0) {// drag the finger from left to right to log. D (TAG, ">>>> fling to left"); fling = true; scrolltoscreen (currentscreenindex-1);} else if (velocityx <0 & currentscreenindex <getchildcount () -1) {log. D (TAG, ">>>> fling to right"); fling = true; scrolltoscreen (currentscreenindex + 1) ;}} return true ;}@ overridepublic Boolean ondown (motionevent E) {return false ;}}) ;}@ overri Deprotected void onlayout (Boolean changed, int left, int top, int right, int bottom) {log. D (TAG, "> left:" + Left + "Top:" + TOP + "Right:" + right + "bottom:" + bottom ); /*** set the layout and arrange the sub-views horizontally */For (INT I = 0; I <getchildcount (); I ++) {view child = getchildat (I); child. setvisibility (view. visible); child. measure (right-left, bottom-top); child. layout (0 + I * getwidth (), 0, getwidth () + I * Get Width (), getheight ();} // initialize the display page int Delta = currentscreenindex * getwidth ()-getscrollx (); scroller. startscroll (getscrollx (), 0, Delta, 0, 0); invalidate () ;}@ overridepublic void computescroll () {If (scroller. computescroloffset () {scrollto (scroller. getcurrx (), 0); postinvalidate () ;}@ overridepublic Boolean ontouchevent (motionevent event) {gesturedetector. ontouchevent (event); Switch (event. getacti On () {Case motionevent. action_down: break; Case motionevent. action_move: break; Case motionevent. action_up: If (! Fling) {snaptodestination ();} fling = false; break; default: break;} return true ;} /*** switch to the specified screen ** @ Param whichscreen */private void scrolltoscreen (INT whichscreen) {If (getfocusedchild ()! = NULL & whichscreen! = Currentscreenindex & getfocusedchild () = getchildat (currentscreenindex) {getfocusedchild (). clearfocus ();} final int Delta = whichscreen * getwidth ()-getscrollx (); scroller. startscroll (getscrollx (), 0, Delta, 0, math. ABS (DELTA) * 2); invalidate (); currentscreenindex = whichscreen; If (onscreenchangelistener! = NULL) {onscreenchangelistener. onscreenchange (currentscreenindex) ;}}/*** determine the number of screens to be switched to based on the current X coordinate position */private void snaptodestination () {scrolltoscreen (getscrollx () + (getwidth () /2)/getwidth ();} public interface onscreenchangelistener {void onscreenchange (INT currentindex);} private onscreenchangelistener; Public void listener (onscreenchangelistener listener) {This. onscreenchangelistener = onscreenchangelistener ;}}
Resource: http://download.csdn.net/detail/niejing654092427/4500937