/**
* @date:2020/9/9
* @author:dongxiaogang
* @description:
*/
public class MyScrollview extends ScrollView {
private View innerView;
private float y;
private Rect normal = new Rect();
private boolean animationFinish = true;
public MyScrollview(Context context) {
super(context, null);
}
public MyScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
int childCount = getChildCount();
if (childCount > 0) {
innerView = getChildAt(0);
}
super.onFinishInflate();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (innerView == null) {
return super.onTouchEvent(ev);
} else {
commonTouchEvent(ev);
}
return super.onTouchEvent(ev);
}
/**
* 自定义Touch事件
*
* @author:dongxiaogang
* create at 2020/9/10 13:40
*/
private void commonTouchEvent(MotionEvent ev) {
if (animationFinish) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
y = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
float preY = y == 0 ? ev.getY() : y;
float nowY = ev.getY();
int detailY = (int) (preY - nowY);
y = nowY;
if (isNeedMove()) {
if (normal.isEmpty()) {
normal.set(innerView.getLeft(), innerView.getTop(), innerView.getRight(), innerView.getBottom());
}
innerView.layout(innerView.getLeft(), innerView.getTop() - detailY / 2, innerView.getRight(), innerView.getBottom() - detailY / 2);
}
break;
case MotionEvent.ACTION_UP:
y = 0;
//布局回到原来的位置
if (isNeedAnimation()) {
animation();
}
break;
}
}
}
private void animation() {
TranslateAnimation ta = new TranslateAnimation(0, 0, 0, normal.top - innerView.getTop());
ta.setDuration(200);
ta.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
animationFinish = false;
}
@Override
public void onAnimationEnd(Animation animation) {
innerView.clearAnimation();
innerView.layout(normal.left, normal.top, normal.right, normal.bottom);
normal.setEmpty();
animationFinish = true;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
innerView.startAnimation(ta);
}
/**
* 是否需要回滚
*
* @author:dongxiaogang 187188534@qq.com
* create at 2020/9/10 14:10
*/
private boolean isNeedAnimation() {
return !normal.isEmpty();
}
/**
* 布局是否需要移动
*
* @author:dongxiaogang 187188534@qq.com
* create at 2020/9/10 14:06
*/
private boolean isNeedMove() {
int offset = innerView.getMeasuredHeight() - getHeight();
int scrollY = getScrollY();
if (scrollY == 0 || scrollY == offset) {
return true;
}
return false;
}
}