解析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)文章
Kotlin中Lambda表達(dá)式與高階函數(shù)使用分析講解
lambda 本質(zhì)上是可以傳遞給函數(shù)的一小段代碼,Kotlin 與 Java 中的 Lambda 有一定的區(qū)別,除了對(duì) lambda 的全面支持外,還有內(nèi)聯(lián)函數(shù)等簡(jiǎn)潔高效的特性。下面我們來(lái)仔細(xì)看一下2022-12-12
Android入門(mén)之TableLayout應(yīng)用解析(一)
這篇文章主要介紹了Android入門(mén)之TableLayout應(yīng)用,需要的朋友可以參考下2014-08-08
Android開(kāi)發(fā)中實(shí)現(xiàn)IOS風(fēng)格底部選擇器(支持時(shí)間 日期 自定義)
這篇文章主要介紹了Android開(kāi)發(fā)中實(shí)現(xiàn)IOS風(fēng)格底部選擇器(支持時(shí)間 日期 自定義)的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android簡(jiǎn)單實(shí)現(xiàn)啟動(dòng)畫(huà)面的方法
這篇文章主要介紹了Android簡(jiǎn)單實(shí)現(xiàn)啟動(dòng)畫(huà)面的方法,結(jié)合實(shí)例形式分析了啟動(dòng)畫(huà)面核心代碼及相關(guān)函數(shù),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android編程之監(jiān)聽(tīng)器的實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程之監(jiān)聽(tīng)器的實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了Android監(jiān)聽(tīng)器的創(chuàng)建、注冊(cè)及相關(guān)使用技巧,需要的朋友可以參考下2015-11-11
Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android 手機(jī)獲取手機(jī)號(hào)實(shí)現(xiàn)方法
本文主要介紹Android 獲取手機(jī)號(hào)的實(shí)現(xiàn)方法,這里提供了實(shí)現(xiàn)方法,和具體操作流程,并符實(shí)現(xiàn)代碼,有需要的小伙伴可以參考下2016-09-09
android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05

