天天看点

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

用自带的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")
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		int kEvent;
		if (isScrollingLeft(e1, e2)) {
			// Check if scrolling left
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		} else {
			// Otherwise scrolling right
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		return onKeyDown(kEvent, null);
	}

	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return e2.getX() > e1.getX();
	}

}
           

也用onKeyDown方法可实现手动滑动效果,例如使用Button的点击时间调用

则画册左移,反之右移。