重写Gallery控件,控制滑动速度

来源:互联网 发布:php解密工具 编辑:程序博客网 时间:2024/05/17 06:55

用自带的Gallery时滑动速度太快,有时滑动一次就越过了好几张图片,所以重写解决此问题。

public class MyGallery extends Gallery {public MyGallery(Context context) {this(context, null);// TODO Auto-generated constructor stub}public MyGallery(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@SuppressWarnings("deprecation")@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {int kEvent;if (isScrollingLeft(e1, e2)) {// Check if scrolling leftkEvent = KeyEvent.KEYCODE_DPAD_LEFT;} else {// Otherwise scrolling rightkEvent = KeyEvent.KEYCODE_DPAD_RIGHT;}return onKeyDown(kEvent, null);}private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {return e2.getX() > e1.getX();}}
也用onKeyDown方法可实现手动滑动效果,例如使用Button的点击时间调用

gallery.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0))
则画册左移,反之右移。

gallery.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0))