Scroller不是视图控件,只是用于控制滚动
Scroller几个重要方法
void | startScroll(int startX, int startY, int dx, int dy)
Start scrolling by providing a starting point and the distance to travel.
|
void | startScroll(int startX, int startY, int dx, int dy, int duration) |
abortAnimation()
Stops the animation.
|
|
boolean | computeScrollOffset()
Call this when you want to know the new location.
|
void | fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)
Start scrolling based on a fling gesture.
|
final void | forceFinished(boolean finished)
Force the finished field to a particular value.
|
computeScrollerOffset用于计算滚动的的位置
fling用于控制快速滑动松开后,根据velocityX,Y继续滚动一段距离
下面一个简单的自定义view实现水平滚动,为了简单,我们直接使用GestureDetector类去处理触摸事件,通过回调处理滚动事件
@TargetApi(9) public class CustomeScrollView extends FrameLayout { private OverScroller scroller; private GestureDetector detector; public CustomeScrollView(Context context) { this(context,null); // TODO Auto-generated constructor stub } public CustomeScrollView(Context context,AttributeSet set){ this(context, set,0); } public CustomeScrollView(Context context,AttributeSet set,int def){ super(context,set,def); scroller=new OverScroller(context,new AccelerateInterpolator()); detector=new GestureDetector(context, new SimpleGestureListener()); } //调用startScroll可以进行滚动 public void startScroll(){ scroller.startScroll(0, 0,-400 , 0, 1000); invalidate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); int count=getChildCount(); int width=0; for (int i = 0; i < count; i++) { View child=getChildAt(i); if(i==0){ child.measure(getChildMeasureSpec(MeasureSpec.UNSPECIFIED, 0, 200), heightMeasureSpec); }else{ child.measure(widthMeasureSpec, heightMeasureSpec); } width+=child.getWidth(); } setMeasuredDimension(width, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { // TODO Auto-generated method stub super.onLayout(changed, left, top, right, bottom); int count=getChildCount(); for (int i = 0; i < count; i++) { View child=getChildAt(i); child.layout(left, top, left+child.getWidth(), top+child.getHeight()); left+=child.getWidth(); } scrollTo(200, 0); } @Override public void computeScroll() { // TODO Auto-generated method stub //super.computeScroll(); //自定义view必须实现computeScroll函数,调用Scroller.computeScorllOffset去计算当前滚动的位置,滚动到终点,computeScrollOffset返回false if(scroller.computeScrollOffset()){ scrollTo(-scroller.getCurrX(), 0); postInvalidate(); } } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub /// super.onTouchEvent(event); // super.onTouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: scroller.forceFinished(true); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: break; default: break; } detector.onTouchEvent(event); return true; } class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener{ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub scroller.fling(scroller.getCurrX(), scroller.getCurrY(), (int)velocityX, (int)velocityY, -200, 0, 0,0); invalidate(); return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub scrollBy((int)distanceX,0); invalidate(); return true; } } }
Activity代码,两个imageView public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final CustomeScrollView view=new CustomeScrollView(this); view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); // view.setBackgroundColor(Color.RED); ImageView image =new ImageView(this); image.setLayoutParams(new LayoutParams(300, LayoutParams.MATCH_PARENT)); image.setBackgroundColor(Color.RED); view.addView(image); ImageView image2 =new ImageView(this); image2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); image2.setBackgroundColor(Color.BLUE); view.addView(image2); setContentView(view); } }