1、自定义scrollview,监听上下滑动距离:
public class ScrollListenerView extends ScrollView {
private ScrollListener scrollViewListener = null;
public ScrollListenerView(Context context) {
super(context);
}
public ScrollListenerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ScrollListenerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollListener(ScrollListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldX, oldY);
}
}
}
接口
public interface ScrollListener {
void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY);
}
2、activity实现接口
public class FriendDetailFragment extends SubBasicFragment implements ScrollListener {
private ScrollListenerView slv;
private int dp2_5;
private int dp37_5;
private int dp54;
slv.setScrollListener(this);
private void initMeasure() {
nSdvWidth = UiUtils.dpToPx(77.5f, mContext.getResources());
nSdvSmallWidth = UiUtils.dpToPx(40f, mContext.getResources());
dp2_5 = UiUtils.dpToPx(2.5f, mContext.getResources());
dp37_5 = UiUtils.dpToPx(37.5f, mContext.getResources());
dp54 = UiUtils.dpToPx(54f, mContext.getResources());
}
@Override
public void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY) {
animationViews(y);
}
//------------------------- 动画 start ------------------------
private void animationViews(int y) {
int nStart = dp2_5;
int nScaledY = nSdvWidth - y;
if (y > nStart && y < dp37_5) {
vMiddle.setVisibility(View.INVISIBLE);
vRight.setVisibility(View.VISIBLE);
UiUtils.setViewLayoutParams(sdvSurface, nScaledY, nScaledY);
}
if (y <= nStart) {
vMiddle.setVisibility(View.VISIBLE);
vRight.setVisibility(View.GONE);
UiUtils.setViewLayoutParams(sdvSurface, nSdvWidth, nSdvWidth);
setSideViewAlpha(1f);
}
if (y > dp2_5 && y < dp37_5) {
float percent = ((float) (y - dp2_5)) / ((float) (dp37_5 - dp2_5));
tvRightUserName.setTextSize(14f + 3.5f * percent);
setSideViewAlpha(1 - percent - 0.3f);
}
if (y > dp54) {
ivQrCode.setVisibility(View.GONE);
ivUserInfo.setVisibility(View.GONE);
vTop.setVisibility(View.VISIBLE);
vTopBackground.setVisibility(View.VISIBLE);
setViewBackground(R.drawable.selector_bt_back);
} else {
vTop.setVisibility(View.GONE);
vTopBackground.setVisibility(View.GONE);
setViewBackground(0);
}
}
2.2工具方法:
public static int dpToPx(float dp, Resources resources) {
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
return (int) px;
}
/**
* 重设 view 的宽高
*/
public static void setViewLayoutParams(View view, int nWidth, int nHeight) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp.height != nHeight || lp.width != nWidth) {
lp.width = nWidth;
lp.height = nHeight;
view.setLayoutParams(lp);
}
}
private void setSideViewAlpha(float alpha) {
ivQrCode.setAlpha(alpha);
ivUserInfo.setAlpha(alpha);
Log.i("lgq","ttt----"+alpha+"......."+ivQrCode.getVisibility());
if (ivQrCode.getVisibility() != View.VISIBLE)
ivQrCode.setVisibility(View.VISIBLE);
if (ivUserInfo.getVisibility() != View.VISIBLE)
ivUserInfo.setVisibility(View.VISIBLE);
}
xml文件
<com.zhenhua.online.view.scrollbylistener.ScrollListenerView
android:id="@+id/slv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
开始滑动