Android自訂ScrollView實現反彈效果

來源:互聯網
上載者:User

android的ScrollView控制項預設是沒有反彈效果的,當滑動到邊緣的時候便不能繼續滑動。這裡通過自訂ScrollView來實現反彈效果。看下面的,紅色圖片在最左邊,android預設ScrollView控制項紅色圖片在最左邊的時候是不能向右滾動的。

這裡是水平滾動,我們可以通過自訂類繼承自HorizontalScrollView類來實現。


  1. public class MyScrollView extends HorizontalScrollView {  
  2.     private View inner;  
  3.     private float x;  
  4.     private Rect normal = new Rect();  
  5.       
  6.     @Override  
  7.     protected void onFinishInflate() {  
  8.         if (getChildCount() > 0) {  
  9.             inner = getChildAt(0);  
  10.         }  
  11.         System.out.println("getChildCount():" + getChildCount());  
  12.     }  
  13.       
  14.     public MyScrollView(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.     }  
  17.       
  18.     @Override  
  19.     public boolean onTouchEvent(MotionEvent ev) {  
  20.         if (inner == null) {  
  21.             return super.onTouchEvent(ev);  
  22.         } else {  
  23.             commOnTouchEvent(ev);  
  24.         }  
  25.   
  26.         return super.onTouchEvent(ev);  
  27.     }  
  28.   
  29.     public void commOnTouchEvent(MotionEvent ev) {  
  30.         int action = ev.getAction();  
  31.         switch (action) {  
  32.         case MotionEvent.ACTION_DOWN:  
  33.             x = ev.getX();  
  34.             break;  
  35.         case MotionEvent.ACTION_UP:  
  36.   
  37.             if (isNeedAnimation()) {  
  38.                 animation();  
  39.             }  
  40.   
  41.             break;  
  42.         case MotionEvent.ACTION_MOVE:  
  43.             final float preX = x;  
  44.             float nowX = ev.getX();  
  45.             int deltaX = (int) (preX - nowX);  
  46.             // 滾動   
  47.             scrollBy(0, deltaX);  
  48.   
  49.             x = nowX;  
  50.             // 當滾動到最左或者最右時就不會再滾動,這時移動布局   
  51.             if (isNeedMove()) {  
  52.                 if (normal.isEmpty()) {  
  53.                     // 儲存正常的布局位置   
  54.                     normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());  
  55.                 }  
  56.                 // 移動布局   
  57.                 inner.layout(inner.getLeft() - deltaX/2, inner.getTop() , inner.getRight()- deltaX/2, inner.getBottom() );  
  58.             }  
  59.             break;  
  60.   
  61.         default:  
  62.             break;  
  63.         }  
  64.     }  
  65.   
  66.     // 開啟動畫移動   
  67.     public void animation() {  
  68.         // 開啟移動動畫   
  69.         TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), normal.top);  
  70.         ta.setDuration(200);  
  71.         inner.startAnimation(ta);  
  72.         // 設定回到正常的布局位置   
  73.         inner.layout(normal.left, normal.top, normal.right, normal.bottom);  
  74.         normal.setEmpty();  
  75.     }  
  76.     // 是否需要開啟動畫   
  77.     public boolean isNeedAnimation() {  
  78.         return !normal.isEmpty();  
  79.     }  
  80.     // 是否需要移動布局   
  81.     public boolean isNeedMove() {  
  82.         int offset = inner.getMeasuredWidth() - getWidth();  
  83.         int scrollX = getScrollX();  
  84.         if (scrollX == 0 || scrollX == offset) {  
  85.             return true;  
  86.         }  
  87.         return false;  
  88.     }  
  89. }  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.