当前位置: 移动技术网 > 移动技术>移动开发>Android > android搜索框上下滑动变色效果

android搜索框上下滑动变色效果

2019年07月24日  | 移动技术网移动技术  | 我要评论

搜索框上下滑动变透明度是现在app中很常见的效果,先看看效果:


首先来看下布局骨架:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context="www.sf.com.searchframe.mainactivity"> 
 
 <listview 
 android:id="@+id/listview" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" /> 
 
 <!--搜索框--> 
 <linearlayout 
 android:id="@+id/ll_search" 
 android:layout_width="match_parent" 
 android:layout_height="50dp" 
 android:background="#00ab95" 
 android:orientation="horizontal"> 
 ...... 
 </linearlayout> 
 
</relativelayout> 

整体就是一个相对布局,搜索框直接覆盖在listview上面,效果图最上方的图片是listview的头布局;
这个效果主要用到listview的滑动监听;
在listview滑动的时候不停的获取,imageview距离屏幕顶部的距离;
然后获取到imageview本身的高度;
通过这两个值判断imageview是否滑出屏幕,根据不同情况设置搜索框的透明度;

mlistview.setonscrolllistener(new abslistview.onscrolllistener() { 
  
  //监听滑动状态的改变 
  public void onscrollstatechanged(abslistview view, int scrollstate) { 
  } 
 
  //用于监听listview屏幕滚动 
  public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { 
 
  int[] ints = new int[2]; 
  mimage.getlocationonscreen(ints); 
  /** 
   * mimage距离屏幕顶部的距离(图片顶部在屏幕最上面,向上滑动为负数,所以取反) 
   * 如果不隐藏状态栏,需要加上状态栏的高度;隐藏状态栏就不用加了; 
   */ 
  int scrolly = -ints[1]+statusheight; 
 
  //mimage这个view的高度 
  int imageheight = mimage.getheight(); 
 
  if (mimage != null && imageheight > 0) { 
   //如果“图片”没有向上滑动,设置为全透明 
   if (scrolly < 0) { 
   llsearch.getbackground().setalpha(0); 
   } else { 
   //“图片”已经滑动,而且还没有全部滑出屏幕,根据滑出高度的比例设置透明度的比例 
   if (scrolly < imageheight) { 
    int progress = (int) (new float(scrolly) / new float(imageheight) * 255);//255 
    llsearch.getbackground().setalpha(progress); 
   } else { 
    //“图片”全部滑出屏幕的时候,设为完全不透明 
    llsearch.getbackground().setalpha(255); 
   } 
   } 
  } 
 
  } 
 }); 

源码下载:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网