Custom Controls --- inherit the ViewGroup class method (step 1 step by step effect ---- drag the image left and right + automatic rebound effect)
----------------------- The following effect is only the second step of the overall effect-(currently, drag the image left and right + the rebound effect) --- continue to update the blog -------------------------
Configuration File
Activity_main.xml (not explained, there is only one custom control)
[Html]
- Xmlns: tools = http://schemas.android.com/tools
- Android: layout_width = match_parent
- Android: layout_height = match_parent>
-
-
- Android: id = @ + id/msv
- Android: layout_width = match_parent
- Android: layout_height = match_parent/>
-
MScrollView. java
Package com. example. mscrollview; import android. r. xml; import android. content. context; import android. util. attributeSet; import android. view. gestureDetector; import android. view. motionEvent; import android. view. view; import android. view. viewGroup; public class mScrollView extends ViewGroup {// define gesture reader private GestureDetector gestureDetector; // 1. constructor public mScrollView (Context context, AttributeSet attrs) {super (context, attrs ); initView (context);}/** 2. Obtain the location and size of the Child page */@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {for (int I = 0; I <getChildCount (); I ++) {// obtain the child View childView = getChildAt (I) based on the id ); // layout the childView. layout (I * getWidth (), 0, getWidth () + getWidth () * I, getHeight ());}} /** initialize view */private void initView (Context context) {// Gesture Recognition instantiation gestureDetector = new GestureDetector (context, new GestureDetector. simpleOnGestureListener () {// call this method by dragging your finger. e1 indicates pressing, e2 indicates leaving the event @ Overridepublic boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {// The distance between the x axis and the y axis to scrollBy (int) distanceX, 0); // return true after the event is processed; // scrollTo (x, y) to be moved to the coordinates});}/*** receives the event onScroll * // records the start coordinate private float startX; // records the index position of the current page private int currentIndex; @ Overridepublic boolean onTouchEvent (MotionEvent event) {super. onTouchEvent (event); // executes the parent class method, and the event is not blocked. // receives the gestureDetector event. onTouchEvent (event); switch (event. getAction () {// This case event involves three types: pressing, moving, and leaving case MotionEvent. ACTION_DOWN: // record the starting coordinate startX = event as long as you press it. getX (); break; case MotionEvent. ACTION_MOVE: break; case MotionEvent. ACTION_UP: // when the finger leaves, record the new coordinate float endX = event. getX (); // calculates the offset if (endX-startX> getWidth ()/2) {// currentIndex --;} else if (startX-endX> getWidth () /2) {// display the next page currentIndex ++;} moveTo (currentIndex); break; default: break;} return true;}/** based on the current page index, move to the corresponding location */private void moveTo (int ScrolledIndex) {// block illegal values. if the smaller the page microcosm is than 0, locate the first if (ScrolledIndex <0) {ScrolledIndex = 0;} // if the page number to be moved is greater than the index of the last page, locate the last if (ScrolledIndex> getChildCount ()-1) {ScrolledIndex = getChildCount () -1;} // assign the new page index to the current page index value currentIndex = ScrolledIndex; // locate a coordinate scrollTo (currentIndex * getWidth (), 0 );}}
MainActivity. java, is a combination, and something needs to be added, OK? OK !)
[Java]
- Package com. example. mscrollview;
-
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. widget. ImageView;
-
- Public class MainActivity extends Activity {
-
- // 1. Image Resources
- Private int [] ids = {R. drawable. a1, R. drawable. a2, R. drawable. a3,
- R. drawable. a3, R. drawable. a5, R. drawable. a6 };
- // 2. Define Custom Controls
- Private mScrollView msv;
-
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- // Create a custom control object
- Msv = (mScrollView) findViewById (R. id. msv );
- // Traverse 6 images cyclically, add background images to image objects in sequence, and then add them to the msv custom control.
- For (int I = 0; I <ids. length; I ++ ){
- ImageView iv = new ImageView (this );
- Iv. setBackgroundResource (ids [I]);
- // 3. Add a subpage
- Msv. addView (iv );
- }
- }
- }