mNestedScrollView = (NestedScrollView) findViewById(R.id.nested_scroll_view);
re = (RelativeLayout) findViewById(R.id.re);
final int height = re.getLayoutParams().height;
mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
// 将透明度声明成局部变量用于判断
int alpha = 0;
int count = 0;
float scale = 0;
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY <= height) {
scale = (float) scrollY / height;
alpha = (int) (255 * scale);
// 随着滑动距离改变透明度
// Log.e("al=","="+alpha);
re.setBackgroundColor(Color.argb(alpha, 255, 0, 0));
} else {
if (alpha < 255) {
Log.e("执行次数", "=" + (++count));
// 防止频繁重复设置相同的值影响性能
alpha = 255;
re.setBackgroundColor(Color.argb(alpha, 255, 0, 0));
}
}
}
});
---------------------
原文:https://blog.youkuaiyun.com/codesuperman1314/article/details/55002536
本文介绍如何使用NestedScrollView在Android应用中实现随滚动距离变化的背景透明度效果。通过监听滚动事件,计算透明度值并动态调整背景颜色,达到平滑过渡的视觉体验。
9004

被折叠的 条评论
为什么被折叠?



