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();
}
}
}
下一篇: MySQL管理之用户权限