最近发现一个很流行的一个效果 随着界面的滑动 顶部标题栏会出现渐变效果 要是现实这样的效果 可以通过ScrollView 滑动达到效果
直接贴代码
public class MyScrollView extends ScrollView{
private ScrollViewListener scrollViewListener = null;
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@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 void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
public interface ScrollViewListener{
void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy);
}
}
第二步 在你的Activty 或者frament 开始的时候 标题栏不透明 超过图片高度的时候 标题栏显示完整
<pre name="code" class="html"> private void initView() {
mScrollview = (MyScrollView) view.findViewById(R.id.scrollView);
mScrollview.setScrollViewListener(this);
//搜索栏的背景透明度
rlSs.getBackground().setAlpha(START_ALPHA);
convenientBanner = (ImageView) view.findViewById(R.id.convenientBanner);
//获取convenientBannerg高度
convenientBanner.post(new Runnable() {
@Override
public void run() {
convenientBannerMeasuredHeight = convenientBanner.getMeasuredHeight();
}
});
private static final int START_ALPHA = 0;
private static final int END_ALPHA = 255;
@Override
public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
if (y > convenientBannerMeasuredHeight) {
y = convenientBannerMeasuredHeight; //当滑动到指定位置之后设置颜色为纯色,之前的话要渐变---实现下面的公式即可
THLog.e("666", "y--convenientBannerMeasuredHeight" +y);
}
if (convenientBannerMeasuredHeight > 0){
rlSs.getBackground().setAlpha(y * (END_ALPHA - START_ALPHA) / convenientBannerMeasuredHeight + START_ALPHA);
}
}
因为代码涉及到项目 代码不完全 基本思路就这么写