Android弹性滑动的HorizontalScrollView

android, HorizontalScrollView, 弹性滑动,ElasticHScrollView,自定义控件

作者:安林——eilin(http://www.cnblogs.com/yzyeilin)


 

      很久没写了,最近由于工作需要,用到大量的自定义控件,模仿了几个好的UI,奇异影视的水平方向弹性滑动做的挺不错,用的是水平方向的自定义ListView.

这里我要用到的是水平方向的ScrollView。

大体上借鉴与这篇文章:http://gundumw100.iteye.com/blog/1075286

原理请看这里:http://dev.10086.cn/cmdn/bbs/thread-40685-1-1.html


1.主要是代码部分:ElasticHScrollView.java

 

  1
   import android.content.Context;
  4 import android.graphics.Rect;
  5 import android.util.AttributeSet;
  6 import android.view.MotionEvent;
  7 import android.view.View;
  8 import android.view.animation.TranslateAnimation;
  9 import android.widget.HorizontalScrollView;
 10 /**
 11  * 水平弹性滑动的ScrollView
 12  * @author eilin
 13  *
 14  */
 15 public class ElasticHScrollView extends HorizontalScrollView {
 16 
 17     private View inner;
 18     private float x;
 19     private Rect normal = new Rect();
 20     
 21     public ElasticHScrollView(Context context) {
 22         super(context);
 23         // TODO Auto-generated constructor stub
 24     }
 25 
 26     public ElasticHScrollView(Context context, AttributeSet attrs) {
 27         super(context, attrs);
 28         // TODO Auto-generated constructor stub
 29     }
 30     
 31     @Override
 32     protected void onFinishInflate() {
 33         if (getChildCount() > 0) {
 34             inner = getChildAt(0);
 35         }
 36     }
 37     
 38     @Override
 39     public boolean onTouchEvent(MotionEvent ev) {
 40         if (inner == null) {
 41             return super.onTouchEvent(ev);
 42         } else {
 43             commOnTouchEvent(ev);
 44         }
 45         return super.onTouchEvent(ev);
 46     }
 47     
 48     public void commOnTouchEvent(MotionEvent ev) {
 49         int action = ev.getAction();
 50         switch (action) {
 51         case MotionEvent.ACTION_DOWN:
 52             x = ev.getX();
 53             break;
 54         case MotionEvent.ACTION_UP:
 55             if (isNeedAnimation()) {
 56                 animation();
 57             }
 58             break;
 59         case MotionEvent.ACTION_MOVE:
 60             final float preX = x;
 61             float nowX = ev.getX();
 62             int deltaX = (int) (preX - nowX);
 63             // 滚动
 64             scrollBy(deltaX, 0);
 65             x = nowX;
 66             // 当滚动到最上或最下时就不会再滚动,这是移动布局
 67             if (isNeedMove()) {
 68                 if (normal.isEmpty()) {
 69                     // 保存正常的布局位置
 70                     normal.set(inner.getLeft(), inner.getTop(), inner
 71                             .getRight(), inner.getBottom());
 72 
 73                 }
 74                 // 移动布局
 75                 inner.layout(inner.getLeft() - deltaX, inner.getTop(), inner
 76                         .getRight() - deltaX, inner.getBottom());
 77             }
 78             break;
 79         default:
 80             break;
 81         }
 82     }
 83 
 84     // 开启动画移动
 85 
 86     public void animation() {
 87         // 开启移动动画        
 88         TranslateAnimation ta=new TranslateAnimation(inner.getLeft(), normal.left, 0, 0);
 89         ta.setDuration(50);
 90         inner.startAnimation(ta);
 91         //设置回到正常的布局位置
 92         inner.layout(normal.left, normal.top, normal.right, normal.bottom);
 93         normal.setEmpty();
 94     }
 95 
 96     //是否需要开启动画
 97     public boolean isNeedAnimation() {
 98         return !normal.isEmpty();
 99     }
100 
101     // 是否需要移动布局
102     public boolean isNeedMove() {
103         int offset = inner.getMeasuredWidth() - getWidth();
104         int scrollX = getScrollX();
105         if (scrollX == 0 || scrollX == offset) {
106             return true;
107         }
108         return false;
109     }
110 
111 
112 }

2.布局测试:main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <com.yzy.customview.ElasticHScrollView
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="fill_parent"
 5     android:layout_height="wrap_content"
 6     android:scrollbars="none"
 7     android:background="#D1EEEE"
 8     >
 9     <LinearLayout 
10         android:orientation="horizontal" 
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:background="#F5F5F5"
14         >
15         <ImageView  
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:src="@drawable/t02"
19     />
20          <ImageView  
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"  
23         android:src="@drawable/t02"
24     />
25           <ImageView  
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:src="@drawable/t02"
29     />
30            <ImageView  
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:src="@drawable/t02"
34     />
35             <ImageView  
36         android:layout_width="wrap_content"
37         android:layout_height="wrap_content"
38         android:src="@drawable/t02"
39     />
40              <ImageView  
41         android:layout_width="wrap_content"
42         android:layout_height="wrap_content"
43         android:src="@drawable/t02" 
44     />
45               <ImageView  
46         android:layout_width="wrap_content"
47         android:layout_height="wrap_content"
48         android:src="@drawable/t02" 
49     />
50                <ImageView  
51         android:layout_width="wrap_content"
52         android:layout_height="wrap_content"
53         android:src="@drawable/t02" 
54     />
55                 <ImageView  
56         android:layout_width="wrap_content"
57         android:layout_height="wrap_content"
58     android:src="@drawable/t02" 
59     />
60                  <ImageView  
61         android:layout_width="wrap_content"
62         android:layout_height="wrap_content"
63         android:src="@drawable/t02"
64     />
65         
66     </LinearLayout>
67     
68 </com.yzy.customview.ElasticHScrollView>

t02.jpg

posted @ 2012-07-17 13:48  安林__yzyeilin  阅读(5819)  评论(1编辑  收藏  举报