View 滑动的实现方式

来源:互联网 发布:儿童编程教材 编辑:程序博客网 时间:2024/06/05 07:34

瞬时移动实现
1. scroller
2. 使用动画
3. 改变布局参数

 ViewGroup.MarginLayoutParams  param = (ViewGroup.MarginLayoutParams) btn2.getLayoutParams();        param.leftMargin = 100;        btn2.setLayoutParams(param);

弹性滑动

  1. 使用scroller
//实现button中的内容弹性滑动public class ScrollBtn extends Button {    private Scroller mScroller;    public ScrollBtn(Context context) {        super(context);    }    public ScrollBtn(Context context, AttributeSet attrs) {        super(context, attrs);        mScroller = new Scroller(context);    }    public ScrollBtn(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public void smoothS(int x,int y){        mScroller.startScroll(getScrollX(),getScrollY(),x,y,2000);        invalidate();    }    @Override    public void computeScroll() {        if(mScroller.computeScrollOffset()){            scrollTo(mScroller.getCurrX(),mScroller.getCurrY());            postInvalidate();        }    }}

2 使用动画
3. 延时策略:handler.postDelay(runnable,time);

0 0