解析ScrollView--仿QQ空間標(biāo)題欄漸變
先看一下效果圖:
<?xml version="1.0" encoding="utf-8"?> <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="com.hankkin.gradationtitlebar.QQSpeakActivity"> <com.hankkin.gradationscroll.GradationScrollView android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/iv_banner" android:scaleType="fitXY" android:src="@drawable/banner3" android:layout_width="match_parent" android:layout_height="200dp" /> <com.hankkin.gradationscroll.NoScrollListview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" > </com.hankkin.gradationscroll.NoScrollListview> </LinearLayout> </com.hankkin.gradationscroll.GradationScrollView> <TextView android:paddingBottom="10dp" android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="55dp" android:gravity="center|bottom" android:text="我是標(biāo)題" android:textSize="18sp" android:textColor="@color/transparent" android:background="#00000000" /> </RelativeLayout>
public class GradationScrollView extends ScrollView { public interface ScrollViewListener { void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy); } private ScrollViewListener scrollViewListener = null; public GradationScrollView(Context context) { super(context); } public GradationScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public GradationScrollView(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); } } }
我們需要獲取圖片的高度,并且設(shè)置滾動(dòng)監(jiān)聽(tīng),隨著滾動(dòng)的距離來(lái)設(shè)置標(biāo)題欄的顏色透明度和字體顏色的透明度
/** * 獲取頂部圖片高度后,設(shè)置滾動(dòng)監(jiān)聽(tīng) */ private void initListeners() { ViewTreeObserver vto = ivBanner.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { textView.getViewTreeObserver().removeGlobalOnLayoutListener( this); height = ivBanner.getHeight(); scrollView.setScrollViewListener(QQSpeakActivity.this); } }); } /** * 滑動(dòng)監(jiān)聽(tīng) * @param scrollView * @param x * @param y * @param oldx * @param oldy */ @Override public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) { // TODO Auto-generated method stub if (y <= 0) { //設(shè)置標(biāo)題的背景顏色 textView.setBackgroundColor(Color.argb((int) 0, 144,151,166)); } else if (y > 0 && y <= height) { //滑動(dòng)距離小于banner圖的高度時(shí),設(shè)置背景和字體顏色顏色透明度漸變 float scale = (float) y / height; float alpha = (255 * scale); textView.setTextColor(Color.argb((int) alpha, 255,255,255)); textView.setBackgroundColor(Color.argb((int) alpha, 144,151,166)); } else { //滑動(dòng)到banner下面設(shè)置普通顏色 textView.setBackgroundColor(Color.argb((int) 255, 144,151,166)); } }
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android ScrollView滑動(dòng)實(shí)現(xiàn)仿QQ空間標(biāo)題欄漸變
- Android仿淘寶商品拖動(dòng)查看詳情及標(biāo)題欄漸變功能
- Android中實(shí)現(xiàn)監(jiān)聽(tīng)ScrollView滑動(dòng)事件
- android TextView不用ScrollViewe也可以滾動(dòng)的方法
- 在android中ScrollView嵌套ScrollView解決方案
- android 實(shí)現(xiàn)ScrollView自動(dòng)滾動(dòng)的實(shí)例代碼
- android 自定義ScrollView實(shí)現(xiàn)背景圖片伸縮的實(shí)現(xiàn)代碼及思路
- android開(kāi)發(fā)教程之文本框加滾動(dòng)條scrollview
- Android之scrollview滑動(dòng)使標(biāo)題欄漸變背景色的實(shí)例代碼
相關(guān)文章
android中打開(kāi)相機(jī)、打開(kāi)相冊(cè)進(jìn)行圖片的獲取示例
本篇文章主要介紹了android中打開(kāi)相機(jī)、打開(kāi)相冊(cè)進(jìn)行圖片的獲取示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01詳解如何在Android中實(shí)現(xiàn)懸浮Activity
本篇文章主要介紹詳解如何在Android中實(shí)現(xiàn)懸浮Activity,通過(guò)修改Activity的實(shí)現(xiàn)來(lái)適配平板設(shè)備,已達(dá)到代碼的最大利用率。有興趣的可以了解一下。2017-01-01Android 8.0如何完美適配全局dialog懸浮窗彈出
這篇文章主要給大家介紹了關(guān)于Android 8.0如何完美適配全局dialog懸浮窗彈出的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧2018-07-07Android開(kāi)發(fā)之DOM解析xml文件的方法
這篇文章主要介紹了Android開(kāi)發(fā)之DOM解析xml文件的方法,涉及Android中DOM工廠對(duì)象的創(chuàng)建,文檔的獲取及節(jié)點(diǎn)的遍歷技巧,需要的朋友可以參考下2015-12-12Android studio 自動(dòng)換行和取消自動(dòng)換行操作
這篇文章主要介紹了Android studio 自動(dòng)換行和取消自動(dòng)換行操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Android 個(gè)人理財(cái)工具五:顯示賬單明細(xì) 上
本文主要介紹 Android 個(gè)人理財(cái)工具顯示賬單明細(xì),這里提供了示例代碼,和實(shí)現(xiàn)效果圖,幫助大家學(xué)習(xí)理解ListView的用法,有興趣的小伙伴可以參考下2016-08-08Android中WebView圖片實(shí)現(xiàn)自適應(yīng)的方法
這篇文章主要介紹了Android中WebView圖片實(shí)現(xiàn)自適應(yīng)的方法,涉及Android操作圖片顯示的相關(guān)技巧,需要的朋友可以參考下2015-05-05Android列表實(shí)現(xiàn)(1)_數(shù)組列表實(shí)例介紹
最近開(kāi)始學(xué)習(xí)android的ui,先上幾個(gè)相關(guān)的例子,后續(xù)還會(huì)有更新,感興趣的朋友可以研究下2012-12-12