Custom Controls --- inherit the ViewGroup class method (step 1 step by step effect ---- drag the image left and right + automatic rebound effect)

Source: Internet
Author: User

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]
  1. Xmlns: tools = http://schemas.android.com/tools
  2. Android: layout_width = match_parent
  3. Android: layout_height = match_parent>
  4.  
  5.  
  6. Android: id = @ + id/msv
  7. Android: layout_width = match_parent
  8. Android: layout_height = match_parent/>
  9.  
  10. 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] 
    1. Package com. example. mscrollview;
    2.  
    3. Import android. app. Activity;
    4. Import android. OS. Bundle;
    5. Import android. widget. ImageView;
    6.  
    7. Public class MainActivity extends Activity {
    8.  
    9. // 1. Image Resources
    10. Private int [] ids = {R. drawable. a1, R. drawable. a2, R. drawable. a3,
    11. R. drawable. a3, R. drawable. a5, R. drawable. a6 };
    12. // 2. Define Custom Controls
    13. Private mScrollView msv;
    14.  
    15. @ Override
    16. Protected void onCreate (Bundle savedInstanceState ){
    17. Super. onCreate (savedInstanceState );
    18. SetContentView (R. layout. activity_main );
    19. // Create a custom control object
    20. Msv = (mScrollView) findViewById (R. id. msv );
    21. // Traverse 6 images cyclically, add background images to image objects in sequence, and then add them to the msv custom control.
    22. For (int I = 0; I <ids. length; I ++ ){
    23. ImageView iv = new ImageView (this );
    24. Iv. setBackgroundResource (ids [I]);
    25. // 3. Add a subpage
    26. Msv. addView (iv );
    27. }
    28. }
    29. }

Related Article

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.