原理:主要是在ScrollView滑动的时候,计算Y轴偏移量的值y,然后float alpha = (float) y / height(图片或视频的高度值);
mTitle.setAlpha(alpha);
1,自定义可滑动的ObservableScrollView
public class ObservableScrollView extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener 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 ScrollViewListener {
void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy);
}
}
2,计算偏移量实现效果
mScrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy) {
Log.e("===z","偏移量 = " + y);
//计算偏移量
int height = mImageView.getHeight();
if (y <= height) {
float alpha = (float) y / height;
mTitle.setAlpha(alpha);
} else {
//将标题栏的颜色设置为完全不透明状态
mTitle.setAlpha(1.0f);
}
}
});
网友评论