Android customized Scrollview for bounce effect

From , 3 Years ago, written in Java, viewed 190 times.
URL https://pastebin.vip/view/2ef3dce7
  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. }  
  90. //java/6170

Reply to "Android customized Scrollview for bounce effect"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website