Similar to launcher desktop sliding effect

Source: Internet
Author: User

This example involves the following knowledge points:
1) Use of the attrs. xml file
2) gesturedetector. ongesturelistener listens for gestures
3) Use of onlayout (), onmeasure (), and ontouchevent ()
Next, let's talk about my Implementation ideas:
1) each desktop is a large component and horizontally arranged in a linear layout file. Each desktop is suitable for the same screen size. Therefore, we need to expand linearlayout and rewrite the onmeasure () and onlayout () methods.
2) To slide with gestures, you only need to implement the ondown () and onscroll () methods in the gesturedetector. ongesturelistener interface.
3) To receive touch-screen events, you must implement ontouchevent ()
Let's take a look at the Code:

  1. Public class scrolllayout extends linearlayout implements gesturedetector. ongesturelistener {
  2. Private int offset; // relative distance
  3. Private gesturedetector; // gesture event
  4. Private int childwidth; // The width of the sub-View
  5. Private int childcount; // Number of subviews
  6. Private int defaultwindow; // default window
  7. Private Boolean setsharewindowflag = false; // ensure that the default window settings are executed only once.
  8. Public scrolllayout (context ){
  9. Super (context );
  10. Init ();
  11. }
  12. Public scrolllayout (context, attributeset attrs ){
  13. Super (context, attrs );
  14. Init ();
  15. // Obtain the defined defaultwindow Value
  16. Typedarray = context. obtainstyledattributes (attrs, R. styleable. myscrollwindow );
  17. Defaultwindow = typedarray. getinteger (R. styleable. myscrollwindow_defaultwindow, 0 );
  18. }
  19. Private void Init (){
  20. Gesturedetector = new gesturedetector (this. getcontext (), this );
  21. }
  22. // A gesture event can be triggered only when the returned value is true.
  23. Public Boolean ondown (motionevent e ){
  24. Return true;
  25. }
  26. Public void onshowpress (motionevent e ){}
  27. Public Boolean onsingletapup (motionevent e ){
  28. Return false;
  29. }
  30. // Slide along the gesture
  31. Public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey ){
  32. // Obtain the sliding distance
  33. Offset = (INT) (offset-distancex );
  34. // Prevents sliding out of the Boundary
  35. If (Offset> 0 ){
  36. Offset = 0;
  37. } Else if (offset <-1 * childwidth * (childCount-1 )){
  38. Offset =-1 * childwidth * (childCount-1 );
  39. }
  40. // Redraw the Layout
  41. Requestlayout ();
  42. Return true;
  43. }
  44. Public void onlongpress (motionevent e ){
  45. }
  46. Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
  47. Return false;
  48. }
  49. // Set the layout file width and height to the width and height of each desktop
  50. @ Override
  51. Protected void onmeasure (INT widthmeasurespec, int heightmeasurespec ){
  52. Super. onmeasure (widthmeasurespec, heightmeasurespec );
  53. // Set the same width and height for each desktop and Screen
  54. Int childcount = getchildcount ();
  55. For (INT I = 0; I <childcount; I ++ ){
  56. Getchildat (I). Measure (widthmeasurespec, heightmeasurespec );
  57. }
  58. }
  59. // Set the Layout
  60. @ Override
  61. Protected void onlayout (Boolean changed, int L, int T, int R, int B ){
  62. Childcount = getchildcount (); // gets the number of child views
  63. Childwidth = childcount> 0? Getchildat (0). getmeasuredwidth (): 0; // get the byte point width
  64. If (! Setsharewindowflag & defaultwindow> = 0 & defaultwindow <= childCount-1 ){
  65. // Set the left end distance of the default window
  66. Offset =-1 * defaultwindow * childwidth;
  67. Setmediawindowflag = true;
  68. }
  69. // Set the initial distance from (0, 0) to the X axis.
  70. Int left = 0 + offset;
  71. For (INT I = 0; I <childcount; I ++ ){
  72. // Set the position of each subview in the layout
  73. View child = getchildat (I );
  74. If (child. getvisibility ()! = View. Gone ){
  75. Child. layout (left, 0, childwidth + left, child. getmeasuredheight ());
  76. Left = left + childwidth;
  77. }
  78. }
  79. }
  80. // Touch screen events
  81. @ Override
  82. Public Boolean ontouchevent (motionevent event ){
  83. Boolean result = gesturedetector. ontouchevent (event );
  84. If (event. getaction () = motionevent. action_up ){
  85. // When the finger is raised, the entire sub-view is displayed based on the sliding distance.
  86. Showonedesktop ();
  87. }
  88. Return result;
  89. }
  90. // Determine the desktop that is displayed when your finger is raised
  91. Private void showonedesktop (){
  92. Int Index = math. Abs (offset)/childwidth;
  93. If (math. Abs (offset)-Index * childwidth> childwidth/2 ){
  94. Index ++;
  95. }
  96. Offset = offset> 0? Index * childwidth:-1 * Index * childwidth;
  97. Requestlayout ();
  98. }
  99. }

Copy code

The attrs. xml file used in this Code:

  1. <Resources>
  2. <Declare-styleable name = "myscrollwindow">
  3. <ATTR name = "defaultwindow" format = "integer"/>
  4. </Declare-styleable>
  5. </Resources>

Copy code

It can be used the same as the layout file provided by the system.

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Com. WXG. scroll_1_view.scrolllayout
  3. Xmlns: Android = "http://schemas.android.com/apk/res/android"
  4. Xmlns: Demo = "http://schemas.android.com/apk/res/com.wxg.scroll_window"
  5. Android: Id = "@ + ID/testlayout"
  6. Android: layout_width = "fill_parent"
  7. Android: layout_height = "fill_parent"
  8. Demo: defaultwindow = "1"
  9. >
  10. <Framelayout
  11. Android: layout_height = "fill_parent"
  12. Android: layout_width = "fill_parent"
  13. Android: Background = "# cccccc">
  14. <Button
  15. Android: layout_height = "wrap_content"
  16. Android: layout_width = "wrap_content"
  17. Android: text = "1"/>
  18. </Framelayout>
  19. <Framelayout
  20. Android: layout_height = "fill_parent"
  21. Android: layout_width = "fill_parent"
  22. Android: Background = "# ffffff">
  23. <Button
  24. Android: layout_height = "wrap_content"
  25. Android: layout_width = "wrap_content"
  26. Android: text = "2"/>
  27. </Framelayout>
  28. <Framelayout
  29. Android: layout_height = "fill_parent"
  30. Android: layout_width = "fill_parent"
  31. Android: Background = "# bcbcbc">
  32. <Button
  33. Android: layout_height = "wrap_content"
  34. Android: layout_width = "wrap_content"
  35. Android: text = "3"/>
  36. </Framelayout>
  37. </COM. WXG. scroll_1_view.scrolllayout>

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.