欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Scroller用法

程序员文章站 2022-06-08 22:41:10
...
public class StudyScroller extends LinearLayout{

    private Scroller mScroller;

    public StudyScroller(Context context) {
        super(context);
        init();
    }

    public StudyScroller(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StudyScroller(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mScroller = new Scroller(getContext());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
         getChildAt(0).setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
                 int scrollX = getScrollX();
                 int delta = 500 - scrollX;
                 System.out.println("delta = " + delta);
                 mScroller.startScroll(scrollX, getScrollY(), -delta, 0, 3000);
                 invalidate();
             }
         });
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }
}

Scroller用法

相关标签: Scroller用法