Android透明到白色滑动渐变,Android中Toolbar随着ScrollView滑动透明度渐变效果实现...
Android中Toolbar隨著ScrollView滑動透明度漸變效果實現
一.思路:監聽ScrollView的滑動事件 不斷的修改Toolbar的透明度
二.注意
1.ScrollView 6.0以前沒有scrollView.setOnScrollChangeListener(l)方法? 所以要自定義ScrollView 在onScrollChanged()中監聽
2.ScrollView 6.0(23)以前沒有scrollView.setOnScrollChangeListener()方法? 所以要自定義ScrollView 實現.為了Toolbar不遮蓋ScrollView我們給ScrollView設置paddingTop
但是ScrollView 設置paddintTop以后 Toolbar透明度變為0以后還占據空間 會出現空白,解決方法:
為ScrollView設置兩個屬性:
1〉.
android:clipToPadding="false"
表示控件的繪制范圍是否不在padding里面? false就是表示空間的繪制可以繪制到padding中
2〉
android:clipChildren="false"
表示子控件是否不能超出padding區域(比如: false :ScrollView上滑的時候 child 可以滑出padding區域 ;true:ScrollView上滑的時候 child 不能可以滑出padding區域 )
布局文件如下:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/scrollview"
android:clipToPadding="false"
android:clipChildren="true"
android:paddingTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_orange_light"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_orange_light"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_orange_light"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light"
/>
android:layout_width="fill_parent"
android:layout_height="400dp"
android:background="@android:color/holo_orange_light"
/>
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:layout_height="?attr/actionBarSize" >
三.步驟
1. 創建回調接口:
public interface TranslucentListener {
/**
* 透明度的回調
* @param alpha
*/
public void onTranslucent(float alpha);
}
2.自定義ScrollView 在onScrollChange方法中回調TranslucentListener接口的方法 并且回傳alpha的值:
@Override
protected void onScrollChanged(int l,int t,int oldl,int oldt) {
super.onScrollChanged(l,t,oldl,oldt);
if (translucentListener!=null) {
//translucentListener.onTranslucent(alpha);
}
}
3.alpha的值得計算:
// alpha = 滑出去的高度/(screenHeight/3);
float heightPixels = getContext().getResources().getDisplayMetrics().heightPixels;
float scrollY = getScrollY();//該值 大于0
float alpha = 1-scrollY/(heightPixels/3);// 0~1 透明度是1~0
//這里選擇的screenHeight的1/3 是alpha改變的速率 (根據你的需要你可以自己定義)
最后MainActivity中
@Override
public void onTranslucent(float alpha) {
toolbar.setAlpha(alpha);
}
以上所述是小編給大家介紹的Android中Toolbar隨著ScrollView滑動透明度漸變效果實現,小編會及時回復大家的。在此也非常感謝大家對編程小技巧網站的支持!
總結
如果覺得編程之家網站內容還不錯,歡迎將編程之家網站推薦給程序員好友。
本圖文內容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
小編個人微信號 jb51ccc
喜歡與人分享編程技術與工作經驗,歡迎加入編程之家官方交流群!
總結
以上是生活随笔為你收集整理的Android透明到白色滑动渐变,Android中Toolbar随着ScrollView滑动透明度渐变效果实现...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android自动化优化工具,Auto
- 下一篇: android sharesdk分享功能