android自定义弹性ScrollVIew ,十分简单!

太简单,不多废话,直接上代码:

package demo.com.customviewdemo.customview;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

/**
 * Created by Alv_chi
 */
public class RebounceScrollViewCopy extends ScrollView {

    private static final String TAG = "test";
    private View contentView;
    private float downY;
    private float moveY;
    private Rect rect;
    private boolean isMoved;

    public RebounceScrollViewCopy(Context context) {
        this(context, null);
    }

    public RebounceScrollViewCopy(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RebounceScrollViewCopy(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public RebounceScrollViewCopy(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

    }

    @Override
    protected void onFinishInflate() {

        /**
         * onFinishInflate 当View中所有的子控件均被映射成xml后触发
         * F inalize inflating a view from XML.  This is called as the last phase
         * of inflation, after all child views have been added
         */
        super.onFinishInflate();
        if (getChildCount() > 0) {
            contentView = getChildAt(0);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (contentView == null) return;
//        用一矩形,记录contentView的原始位置:
        rect = new Rect(contentView.getLeft(), contentView.getTop(), contentView.getRight(), contentView.getBottom());
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (contentView != null) {
                    moveY = ev.getY();
                    float deltaY = moveY - downY;
                    Log.e(TAG, "dispatchTouchEvent: deltaY="+deltaY );
                    int offset = (int) (deltaY * 0.7);
                    int scrollY = getScrollY();
                    if (scrollY == 0 && deltaY > 0) {//计算是否到ScrollView的顶部,以及判断是否向下拉;
//                       移动ContentView,并标记移动了
                        contentView.layout(rect.left, rect.top + offset, rect.right, rect.bottom + offset);

                        isMoved = true;
                    } else if (scrollY + getHeight() == contentView.getHeight() && deltaY < 0) {//计算是否到ScrollVIew底部,以及判断是否向上拉;
//                         移动ContentView,并标记移动了
                        contentView.layout(rect.left, rect.top + offset, rect.right, rect.bottom + offset);

                        isMoved = true;
                    }
                }


                break;
            case MotionEvent.ACTION_UP:
                if (isMoved) {
//                    定义动画
                    TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, contentView.getTop(), rect.top);
                    translateAnimation.setDuration(800);
                    translateAnimation.setInterpolator(new BounceInterpolator());
                    translateAnimation.start();
                    contentView.setAnimation(translateAnimation);
//                    contentView回复原位置;
                    contentView.layout(rect.left, rect.top, rect.right, rect.bottom);

                    isMoved = false;
                }
                break;
        }

        return super.dispatchTouchEvent(ev);
    }
}

不上图了,源码下载:

点击打开链接下载源码


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值