package com.as.apprehendschool.customviews.scrollview;
import android.content.Context;
import android.graphics.Rect;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class ObserveAlphaScrollView extends NestedScrollView {
AlphaChangeListener alphaChangeListener;
private float xLast, yLast, xDistance, yDistance;
// ScrollView的子View, 也是ScrollView的唯一一个子View
private View contentView;
// 用于记录正常的布局位置
private Rect originalRect = new Rect();
//最顶部的文字 颜色变化
private int observeHeight;
//新增的底部按钮图片变化 高度监听
private int observeBoottomHeight;
public ObserveAlphaScrollView(Context context) {
super(context);
}
public ObserveAlphaScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObserveAlphaScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
contentView = getChildAt(0);
}
super.onFinishInflate();
}
@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;
// ScrollView中的唯一子控件的位置信息, 这个位置信息在整个控件的生命周期中保持不变
originalRect.set(contentView.getLeft(), contentView.getTop(),
contentView.getRight(), contentView.getBottom());
}
/**
* 在这里解决滑动上下滑动,左右滑动冲突
*
* @param ev
* @return
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
xLast = ev.getX();
yLast = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - xLast);
yDistance += Math.abs(curY - yLast);
xLast = curX;
yLast = curY;
if (xDistance > yDistance) {
return false; //表示向下传递事件
}
}
return super.onInterceptTouchEvent(ev);
}
public interface AlphaChangeListener {
void alphaChanging(float alpha, int height);
}
public void setObserveHeight(int observeHeight) {
this.observeHeight = observeHeight;
}
public void setAlphaChangeListener(AlphaChangeListener alphaChangeListener) {
this.alphaChangeListener = alphaChangeListener;
}
public interface BottomChangeListener {
void alphaChanging(float alpha, int height);
}
public void setObserveBoottomHeight(int observeBoottomHeight) {
this.observeBoottomHeight = observeBoottomHeight;
}
public BottomChangeListener bottomChangeListener;
public void setBottomChangeListener(BottomChangeListener bottomChangeListener) {
this.bottomChangeListener = bottomChangeListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (alphaChangeListener != null) {
if (t >= observeHeight) {
alphaChangeListener.alphaChanging(1, t);
} else {
//透明度应该是0~1f
float i = (float) t / (float) observeHeight;
alphaChangeListener.alphaChanging(i, t);
}
}
if (bottomChangeListener != null ) {
if (t >= observeHeight) {
bottomChangeListener.alphaChanging(1, t);
} else {
//透明度应该是0~1f
float i = (float) t / (float) observeHeight;
bottomChangeListener.alphaChanging(i, t);
}
}
}
}