public class CustomScrollView extends ScrollView {
private OnScrollChanged mOnScrollChanged;
private int upHeight;
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
upHeight = dpTopx(100);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollChanged!=null){
mOnScrollChanged.onScroll(l,t,oldl,oldt);
}
}
/**
*
* @param onScrollChanged
*/
public void setOnScrollChanged(OnScrollChanged onScrollChanged){
this.mOnScrollChanged=onScrollChanged;
}
public interface OnScrollChanged{
void onScroll(int l,int t,int oldl,int oldt);
}
/**
*
* @param dpValue
* @return
*/
private int dpTopx(int dpValue){
final float scale = this.getResources().getDisplayMetrics().density;
return (int)(dpValue * scale + 0.5f);
}
}
使用:
mCustomScrollView.setOnScrollChanged(new CustomScrollView.OnScrollChanged() {
@Override
public void onScroll(int l, int t, int oldl, int oldt) {
//擷取控件高度
int mHeight = (ivSingerDetailBg.getHeight() - llSingerDetailInfoBar.getHeight());
if (t <= 0) {
//未滑動
llSingerDetailTitlebar.setBackgroundColor(Color.argb(0, 26, 26, 26));
tvSingerDetailTitle.setVisibility(View.GONE);
cbSingerDetailTitleAttention.setVisibility(View.GONE);
} else if (t > 0 && t <= mHeight) {
//滑動過程中,并在mHeight内
// float alpha = (255 * (t / mHeight));
float scale = (float) t / mHeight;
float alpha = (255 * scale);
llSingerDetailTitlebar.setBackgroundColor(Color.argb((int) alpha, 26, 26, 26));
tvSingerDetailTitle.setVisibility(View.GONE);
cbSingerDetailTitleAttention.setVisibility(View.GONE);
} else {
//超過标題欄
llSingerDetailTitlebar.setBackgroundColor(Color.argb(255, 26, 26, 26));
tvSingerDetailTitle.setVisibility(View.VISIBLE);
cbSingerDetailTitleAttention.setVisibility(View.VISIBLE);
}
}
});