根据文章 Android仿IOS上拉/下拉弹性效果ScrollView 优化而来的弹性ScrollView,可自定义下拉上拉的最大距离,也可设置阻碍系数,使滑动更有感觉
效果图
attr文件配置
<!--弹性ScrollView-->
<declare-styleable name="ElasticityScrollView">
<attr name="max_move" format="integer"/>
<attr name="block_ratio" format="float"/>
</declare-styleable>
控件代码
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ScrollView;
/**
* 仿IOS上拉下拉弹性效果ScrollView
*/
public class ElasticityScrollView extends ScrollView {
// 上下文
private Context context;
// ScrollView子布局
private View contentView;
// 手势按下Y坐标
private float startY;
// 手势按下时,是否可以下拉/上拉标志
private boolean isCanPullDown, isCanPullUp;
// 保存ScrollView子布局的初始位置信息
private int leftPosition, topPosition, rightPosition, bottomPosition;
private int maxMove = 300;//限制滑动距离-->默认值300
private float blockRatio = 3;//滑动阻碍系数-->默认值3
public ElasticityScrollView(Context context) {
this(context, null);
}
public ElasticityScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initView(attrs);//初始化View
}
/**
* 初始化View
*
* @param attrs
*/
private void initView(AttributeSet attrs) {
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ElasticityScrollView);
//指定最大滑动距离--防止负值出现
maxMove = Math.abs(attributes.getInt(R.styleable.ElasticityScrollView_max_move, maxMove));
//指定阻尼系数
blockRatio = attributes.getFloat(R.styleable.ElasticityScrollView_block_ratio, blockRatio);
blockRatio = blockRatio <= 0 ? 1 : blockRatio;//防止负值或0的出现
attributes.recycle();
}
/**
* 获取ScrollView的子布局
*/
@Override
protected void onFinishInflate() {
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) {
leftPosition = contentView.getLeft();
topPosition = contentView.getTop();
rightPosition = contentView.getRight();
bottomPosition = contentView.getBottom();
}
}
/**
* 触摸事件处理
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (contentView == null)
return super.dispatchTouchEvent(ev);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
startY = ev.getY();
isCanPullDown = isScrollViewTop();
isCanPullUp = isScrollViewBottom();
break;
case MotionEvent.ACTION_UP:
float endY = ev.getY();
// 手势放开时,采用动画形式返回原位置
if (endY > startY && isCanPullDown || endY < startY && isCanPullUp) {
ObjectAnimator animator = ObjectAnimator.ofFloat(contentView, "translationY", contentView.getTop(), topPosition);
animator.setDuration(500);
animator.setInterpolator(new AccelerateInterpolator());
animator.start();
// 设置布局到正常位置
contentView.layout(leftPosition, topPosition, rightPosition, bottomPosition);
}
break;
case MotionEvent.ACTION_MOVE:
// 如果不在ScrollView的最顶部或最底部(startY需要是在最顶部或最底部时按下的坐标)
if (!isCanPullUp && !isCanPullDown) {
startY = ev.getY();
isCanPullDown = isScrollViewTop();
isCanPullUp = isScrollViewBottom();
break;
}
// 在最上部或最底部时,拉动移动布局
// 1、下拉 2、上拉 3、布局内容比ScrollView小,则既可以上拉,也可以下拉
if (isCanPullDown && ev.getY() > startY || isCanPullUp && ev.getY() < startY || isCanPullDown && isCanPullUp) {
int deltaY = (int) ((ev.getY() - startY) / blockRatio);//增加阻尼系数
if (deltaY > maxMove) {
deltaY = maxMove;
}
if (deltaY < -maxMove) {
deltaY = -maxMove;
}
contentView.layout(leftPosition, topPosition + deltaY, rightPosition, bottomPosition + deltaY);
}
break;
}
return super.dispatchTouchEvent(ev);
}
/**
* 判断是否在ScrollView顶部,在顶部时可以下拉
*/
private boolean isScrollViewTop() {
if (getScrollY() == 0)
return true;
return false;
}
/**
* 判断是否在ScrollView底部,在顶部时可以上拉
*/
private boolean isScrollViewBottom() {
if (contentView.getMeasuredHeight() <= getScrollY() + getHeight())
return true;
return false;
}
}