Gallery Fling翻页以及自动翻页

来源:互联网 发布:程序员逻辑思维训练 编辑:程序博客网 时间:2024/06/11 15:25

说明:每次FLing最多翻一页,平滑翻页。支持自动翻页

public CustomGallery(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        handler = new Handler();        postDelayedScrollNext();    }    private void postDelayedScrollNext() {        handler.postDelayed(new Runnable() {            public void run() {                postDelayedScrollNext();                Log.d("CustomGallery", "dpad RIGHT");                onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);            }        }, 1000);    }    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {        return e2.getX() > e1.getX();    }    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {        int kEvent;        if (isScrollingLeft(e1, e2)) { //可以改成:if(velocityX > 0),并且这样效果更接近用户体验            Log.d("CustomGallery", "fling LEFT");            kEvent = KeyEvent.KEYCODE_DPAD_LEFT;        } else {            Log.d("CustomGallery", "fling LEFT");            kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;        }        onKeyDown(kEvent, null);        return true;    }


0 0