原文链接:https://www.jianshu.com/p/36391b013a30
1.自定义一个颜色渐变的Behavior
public class ColorGradBehavior extends CoordinatorLayout.Behavior<View> {
private static final String TAG = "ColorGradBehavior";
private int PAGE_COLOR_ONE;
private int PAGE_COLOR_TWO;
public ColorGradBehavior() {
}
public ColorGradBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
PAGE_COLOR_ONE = ContextCompat.getColor(context, R.color.transparent);
PAGE_COLOR_TWO = ContextCompat.getColor(context, R.color.colorAccent);
}
//所依赖的联动对象
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
float dy = dependency.getHeight()+dependency.getY();
float y = dy / child.getHeight();
y = y>1?1:y;
ArgbEvaluator argbEvaluator = new ArgbEvaluator();//渐变色计算类
int currentLastColor = (int) (argbEvaluator.evaluate(1-y, PAGE_COLOR_ONE, PAGE_COLOR_TWO));
child.setBackgroundColor(currentLastColor);
return true;
}
}
2.布局文件:一个AppBarLayout
包裹一张图片,中间一个滚动控件NestedScrollView
,最下面就是一个LinearLayout
写的标题栏
效果如下: