ScrollView实现图片滑动功能

发布时间:2020-08-10 15:14:33 作者:uxlwsfmf
来源:网络 阅读:390
public class MyScrollView_1Activity extends Activity {
    int[] ids = new int[] { R.drawable.a1, R.drawable.a3,
            R.drawable.a4, R.drawable.a5};
    private MyScrollView scrollView;
    private Context ctx;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        ctx = this;
        scrollView = new MyScrollView(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, -1);
        for (int i = 0; i < ids.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setBackgroundResource(ids[i]);
            scrollView.addView(iv, params);
        }
        setContentView(scrollView);
    }
}
public class MyScrollView extends ViewGroup {
    private GestureDetector detector;
    private MyScrollView_1Activity ctx;
    private Scroller scroller;
    public MyScrollView(Context context) {
        super(context);
        this.ctx = (MyScrollView_1Activity) context;
        initView();
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            view.layout(1 + i * getWidth(), t, r + i * getWidth(), b);
        }
    }
    private void initView() {
        scroller = new Scroller(ctx);
        detector = new GestureDetector(new OnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }
            @Override
            public void onShowPress(MotionEvent e) {
                // TODO Auto-generated method stub
            }
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2,
                    float distanceX, float distanceY) {
                scrollBy((int) distanceX, 0);
                return false;
            }
            @Override
            public void onLongPress(MotionEvent e) {
                // TODO Auto-generated method stub
            }
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2,
                    float velocityX, float velocityY) {
                // TODO Auto-generated method stub
                return false;
            }
            @Override
            public boolean onDown(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }
        });
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            moveToDest();
            break;
        }
        return true;
    }
    public void moveToDest() {
        int dest = (getScrollX() + getWidth() / 2) / getWidth();
        moveToDest(dest);
    }
    private void moveToDest(int dest) {
        int distance = dest * getWidth() - getScrollX();
        scroller.startScroll(getScrollX(), 0, distance, 0);
        invalidate();
    }
    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), 0);
            postInvalidate();
        }
    }
}


推荐阅读:
  1. 竖向ScrollView+横向ScrollView+ListView,实现listView可以左右上下滑动
  2. Unity工具类ScrollView如何实现拖拽滑动翻页

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

scrollview实现图片滑动功能 ol

上一篇:python操作excel表格的方法

下一篇:监控mysql数据库主从状态

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》