Android View弹性滑动

1. scrollBy/scrollTo

public void scrollTo(int x, int y) 
public void scrollBy(int x, int y)

动画会比较生硬。不会像属性动画一样柔和,就像直接将View搬到指定的坐标点一样。to是移动到(x,y)坐标。by是基于本身位置方向上移动(x,y)的距离。

2.Scroller

public boolean computeScrollOffset() :Scroller类中的方法 
public void startScroll(int startX, int startY, int dx, int dy, int duration) :Scroller类中的方法 
public void computeScroll() : View中的方法
---
/**
  * Called by a parent to request that a child update its values for mScrollX
  * and mScrollY if necessary. This will typically be done if the child is
  * animating a scroll using a {@link android.widget.Scroller Scroller}
  * object.
  */
public void computeScroll() {}

使用Scroller类可以实现很多滚动效果,类似于ViewPager,ListView等的滚动效果。
通过实例化Scroller后,调用startScroll来传入参数,这时并不能执行滑动,通过调用invalidate()去刷新View。而后Scroller执行后会触发computeScroll()回调。通过在回调中执行:

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

来实现弹性动画

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值