解析ScrollView--仿QQ空間標題欄漸變
更新時間:2017年05月02日 08:53:57 作者:ganchuanpu
本篇文章主要介紹了仿QQ空間標題欄漸變的實現(xiàn)方法的相關知識,具有很好的參考價值。下面跟著小編一起來看下吧
先看一下效果圖:

<?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="我是標題"
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);
}
}
}
我們需要獲取圖片的高度,并且設置滾動監(jiān)聽,隨著滾動的距離來設置標題欄的顏色透明度和字體顏色的透明度
/**
* 獲取頂部圖片高度后,設置滾動監(jiān)聽
*/
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);
}
});
}
/**
* 滑動監(jiān)聽
* @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) { //設置標題的背景顏色
textView.setBackgroundColor(Color.argb((int) 0, 144,151,166));
} else if (y > 0 && y <= height) { //滑動距離小于banner圖的高度時,設置背景和字體顏色顏色透明度漸變
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 { //滑動到banner下面設置普通顏色
textView.setBackgroundColor(Color.argb((int) 255, 144,151,166));
}
}
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
您可能感興趣的文章:
- Android ScrollView滑動實現(xiàn)仿QQ空間標題欄漸變
- Android仿淘寶商品拖動查看詳情及標題欄漸變功能
- Android中實現(xiàn)監(jiān)聽ScrollView滑動事件
- android TextView不用ScrollViewe也可以滾動的方法
- 在android中ScrollView嵌套ScrollView解決方案
- android 實現(xiàn)ScrollView自動滾動的實例代碼
- android 自定義ScrollView實現(xiàn)背景圖片伸縮的實現(xiàn)代碼及思路
- android開發(fā)教程之文本框加滾動條scrollview
- Android之scrollview滑動使標題欄漸變背景色的實例代碼
相關文章
Kotlin中Lambda表達式與高階函數(shù)使用分析講解
lambda 本質上是可以傳遞給函數(shù)的一小段代碼,Kotlin 與 Java 中的 Lambda 有一定的區(qū)別,除了對 lambda 的全面支持外,還有內聯(lián)函數(shù)等簡潔高效的特性。下面我們來仔細看一下2022-12-12
Android開發(fā)中實現(xiàn)IOS風格底部選擇器(支持時間 日期 自定義)
這篇文章主要介紹了Android開發(fā)中實現(xiàn)IOS風格底部選擇器(支持時間 日期 自定義)的相關資料,需要的朋友可以參考下2016-11-11
Android編程之監(jiān)聽器的實現(xiàn)方法
這篇文章主要介紹了Android編程之監(jiān)聽器的實現(xiàn)方法,以實例形式較為詳細的分析了Android監(jiān)聽器的創(chuàng)建、注冊及相關使用技巧,需要的朋友可以參考下2015-11-11
android studio實現(xiàn)簡單的計算器功能
這篇文章主要為大家詳細介紹了android studio實現(xiàn)簡單的計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05

