当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿京东搜索框渐变效果

Android仿京东搜索框渐变效果

2019年07月23日  | 移动技术网IT编程  | 我要评论

课件背景图片素材,牛牛.导航网ag001,赛尔号伊索里怎么打

在许多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));
  }
  }
 });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网