这篇文章主要为大家详细介绍了Android仿京东搜索框渐变效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在许多APP中,有的搜索框是一直固定的,有的呢,附加了很多的效果,就比如京东

好吧,谁让京东那么厉害呢,不说了,开始高仿!
原理:就是自定义scrollview实现对滑动高度的监听而已,如此实现对搜索框的渐变
先贴上我的自定义scrollview
//自定义ScrollView
public class CustomView extends ScrollView {
public interface ScrollViewListener {
void onScrollChanged(CustomView customView, int x, int y, int oldx, int oldy);
}
private ScrollViewListener scrollViewListener = null;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public CustomView(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);
}
}
}
好了,接下来就直接在逻辑代码中调用就行了!
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//搜索框在布局最上面
line.bringToFront();
mScrollview.setScrollViewListener(new CustomView.ScrollViewListener() {
@Override
public void onScrollChanged(CustomView customView, int x, int y, int oldx, int oldy) {
if (y <= 0) {
line.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得,或者美工提供
} else if (y > 0 && y <= imageHeight) {
//获取ScrollView向下滑动图片消失的比例
float scale = (float) y / imageHeight;
//更加这个比例,让标题颜色由浅入深
float alpha = (255 * scale);
// 只是layout背景透明
line.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
}
}
});
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
织梦狗教程
本文标题为:Android仿京东搜索框渐变效果
基础教程推荐
猜你喜欢
- Android中的webview监听每次URL变化实例 2023-01-23
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- Android多返回栈技术 2023-04-15
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- android studio按钮监听的5种方法实例详解 2023-01-12
- Flutter手势密码的实现示例(附demo) 2023-04-11
