反弹效果实现

反弹效果主要是自定义scrollview


0去掉滑动到底部和顶部的弧线阴影效果,在xml文件添加如下语句

android:fadingEdge="none"
android:overScrollMode="never"

1.首先定义2个变量

private View sonView ;--住视图
private Rect normal = new Rect();--上方弹簧用的举行

需要使用下面的个方法

 
@Override//滑动结束的时候调用--用于顶部的反弹效果
protected void onFinishInflate() {
    if (getChildCount() > 0) {
        sonView = getChildAt(0);
    }
}

这个是加载位移动画的方法:

这里需要说明以下

1.通过Rect来判断是否可以加载

normal.setEmpty();

2位移动画有一个bug,就是图片是位移了,但是view的位置还是在原来的地方(imooc里说用ObjectAnimator.ofFloat(imageView,"transLationY",0f,100f).setDuring(200).start()属性动画的方法),所以位移动画需要通过layout重新设置终止的最新位置

public void animation() {
    // 开启移动动画
    TranslateAnimation ta = new TranslateAnimation(0, 0, sonView.getTop(),
            normal.top);
    ta.setDuration(200);
    sonView.startAnimation(ta);
    // 设置回到正常的布局位置
    sonView.layout(normal.left, normal.top, normal.right, normal.bottom);
    normal.setEmpty();
}

3.在touchevent里面处理事件

down:记录下初始位置

            y = ev.getY();

up:回弹动画

  if (isNeedAnimation()) {
                    // Log.v("mlguitar", "will up and animation");
                    animation();
                }

move:rect的功能类似于辅助存储sonview视图

                final float preY = y;
                float nowY = ev.getY();
                /**
                 * size=4 表示 拖动的距离为屏幕的高度的1/4
                 */
                int deltaY = (int) (preY - nowY) / size;
                // 滚动
                // scrollBy(0, deltaY);


                y = nowY;
                // 当滚动到最上或者最下时就不会再滚动,这时移动布局
                if (isNeedMove()) {
                    if (normal.isEmpty()) {
                        // 保存正常的布局位置
                        normal.set(sonView.getLeft(), sonView.getTop(),
                                sonView.getRight(), sonView.getBottom());
                        return;
                    }
                    int yy = sonView.getTop() - deltaY;


                    // 移动布局
                    sonView.layout(sonView.getLeft(), yy, sonView.getRight(),
                            sonView.getBottom() - deltaY);
                }

4下面是2个辅助的方法

// 是否需要开启动画
public boolean isNeedAnimation() {
    return !normal.isEmpty();
}

// 是否需要移动布局
public boolean isNeedMove() {
    int offset = sonView.getMeasuredHeight() - getHeight();
    int scrollY = getScrollY();
    if (scrollY == 0 || scrollY == offset) {
        return true;
    }
    return false;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值