ScrollView实现图片滑动功能-创新互联

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();
        }
    }
}

创新互联公司长期为成百上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为浦城企业提供专业的网站设计制作、成都做网站,浦城网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:ScrollView实现图片滑动功能-创新互联
标题链接:http://hbruida.cn/article/cedcpi.html