【轉】Android自訂控制項(二)——有彈性的ScrollView

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   sp   on   

原文地址:http://blog.csdn.net/a105865708/article/details/17784041

實現了當手指滑動到ScrollView的頂部、底部時,

可以繼續的向上、向下展開。當釋放手指的時候,向上、下彈回。

效果:

主要代碼:

 

  1     public class ElasticScrollView extends ScrollView {    2         private View inner;    3         private float y;    4         private Rect normal = new Rect();    5         private boolean animationFinish = true;    6         7         public ElasticScrollView(Context context) {    8             super(context);    9         }   10        11         public ElasticScrollView(Context context, AttributeSet attrs) {   12             super(context, attrs);   13         }   14        15         @Override   16         protected void onFinishInflate() {   17             if (getChildCount() > 0) {   18                 inner = getChildAt(0);   19             }   20         }   21            22         @Override   23         public boolean onInterceptTouchEvent(MotionEvent ev) {   24             return super.onInterceptTouchEvent(ev);   25         }   26        27         @Override   28         public boolean onTouchEvent(MotionEvent ev) {   29             if (inner == null) {   30                 return super.onTouchEvent(ev);   31             } else {   32                 commOnTouchEvent(ev);   33             }   34             return super.onTouchEvent(ev);   35         }   36        37         public void commOnTouchEvent(MotionEvent ev) {   38             if (animationFinish) {   39                 int action = ev.getAction();   40                 switch (action) {   41                 case MotionEvent.ACTION_DOWN:   42     //              System.out.println("ACTION_DOWN");   43                     y = ev.getY();   44                     super.onTouchEvent(ev);   45                     break;   46                 case MotionEvent.ACTION_UP:   47     //              System.out.println("ACTION_UP");   48                     y = 0;   49                     if (isNeedAnimation()) {   50                         animation();   51                     }   52                     super.onTouchEvent(ev);   53                     break;   54                 case MotionEvent.ACTION_MOVE:   55     //              System.out.println("ACTION_MOVE");   56                     final float preY = y == 0 ? ev.getY() : y;   57                     float nowY = ev.getY();   58                     int deltaY = (int) (preY - nowY);   59                     // 滾動   60     //              scrollBy(0, deltaY);   61        62                     y = nowY;   63                     // 當滾動到最上或者最下時就不會再滾動,這時移動布局   64                     if (isNeedMove()) {   65                         if (normal.isEmpty()) {   66                             // 儲存正常的布局位置   67                             normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());   68                         }   69                         // 移動布局   70                         inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, inner.getRight(), inner.getBottom() - deltaY / 2);   71                     } else {   72                         super.onTouchEvent(ev);   73                     }   74                     break;   75                 default:   76                     break;   77                 }   78             }   79         }   80        81         // 開啟動畫移動   82        83         public void animation() {   84             // 開啟移動動畫   85             TranslateAnimation ta = new TranslateAnimation(0, 0, 0, normal.top - inner.getTop());   86             ta.setDuration(200);   87             ta.setAnimationListener(new AnimationListener() {   88                 @Override   89                 public void onAnimationStart(Animation animation) {   90                     animationFinish = false;   91        92                 }   93        94                 @Override   95                 public void onAnimationRepeat(Animation animation) {   96        97                 }   98        99                 @Override  100                 public void onAnimationEnd(Animation animation) {  101                     inner.clearAnimation();  102                     // 設定回到正常的布局位置  103                     inner.layout(normal.left, normal.top, normal.right, normal.bottom);  104                     normal.setEmpty();  105                     animationFinish = true;  106                 }  107             });  108             inner.startAnimation(ta);  109         }  110       111         // 是否需要開啟動畫  112         public boolean isNeedAnimation() {  113             return !normal.isEmpty();  114         }  115       116         // 是否需要移動布局  117         public boolean isNeedMove() {  118             int offset = inner.getMeasuredHeight() - getHeight();  119             int scrollY = getScrollY();  120             if (scrollY == 0 || scrollY == offset) {  121                 return true;  122             }  123             return false;  124         }  125       126     }  

 

【轉】Android自訂控制項(二)——有彈性的ScrollView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.