android搜索框上下滑動變色效果
搜索框上下滑動變透明度是現(xiàn)在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的滑動監(jiān)聽;
在listview滑動的時候不停的獲取,imageview距離屏幕頂部的距離;
然后獲取到imageview本身的高度;
通過這兩個值判斷imageview是否滑出屏幕,根據(jù)不同情況設(shè)置搜索框的透明度;
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { //監(jiān)聽滑動狀態(tài)的改變 public void onScrollStateChanged(AbsListView view, int scrollState) { } //用于監(jiān)聽ListView屏幕滾動 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int[] ints = new int[2]; mImage.getLocationOnScreen(ints); /** * mImage距離屏幕頂部的距離(圖片頂部在屏幕最上面,向上滑動為負數(shù),所以取反) * 如果不隱藏狀態(tài)欄,需要加上狀態(tài)欄的高度;隱藏狀態(tài)欄就不用加了; */ int scrollY = -ints[1]+statusHeight; //mImage這個view的高度 int imageHeight = mImage.getHeight(); if (mImage != null && imageHeight > 0) { //如果“圖片”沒有向上滑動,設(shè)置為全透明 if (scrollY < 0) { llSearch.getBackground().setAlpha(0); } else { //“圖片”已經(jīng)滑動,而且還沒有全部滑出屏幕,根據(jù)滑出高度的比例設(shè)置透明度的比例 if (scrollY < imageHeight) { int progress = (int) (new Float(scrollY) / new Float(imageHeight) * 255);//255 llSearch.getBackground().setAlpha(progress); } else { //“圖片”全部滑出屏幕的時候,設(shè)為完全不透明 llSearch.getBackground().setAlpha(255); } } } } });
源碼下載:http://xiazai.jb51.net/201611/yuanma/AndroidSearch(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android側(cè)滑菜單控件DrawerLayout使用詳解
這篇文章主要為大家詳細介紹了Android側(cè)滑菜單控件DrawerLayout的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12phonegap教程使用jspdf庫在應(yīng)用中生成pdf文件(pdf生成方法)
在PhoneGap應(yīng)用中生成pdf文件,實現(xiàn)起來很簡單,使用JSPDF這個標準的JavaScript類庫來實現(xiàn)這個功能2014-01-01Android監(jiān)聽鍵盤狀態(tài)獲取鍵盤高度的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android監(jiān)聽鍵盤狀態(tài)獲取鍵盤高度的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12