解析ScrollView--仿QQ空間標(biāo)題欄漸變
更新時間:2017年05月02日 08:53:57 作者:ganchuanpu
本篇文章主要介紹了仿QQ空間標(biāo)題欄漸變的實現(xiàn)方法的相關(guā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="我是標(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è)置滾動監(jiān)聽,隨著滾動的距離來設(shè)置標(biāo)題欄的顏色透明度和字體顏色的透明度
/**
* 獲取頂部圖片高度后,設(shè)置滾動監(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) { //設(shè)置標(biāo)題的背景顏色
textView.setBackgroundColor(Color.argb((int) 0, 144,151,166));
} else if (y > 0 && y <= height) { //滑動距離小于banner圖的高度時,設(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 { //滑動到banner下面設(shè)置普通顏色
textView.setBackgroundColor(Color.argb((int) 255, 144,151,166));
}
}
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
您可能感興趣的文章:
- Android ScrollView滑動實現(xiàn)仿QQ空間標(biāo)題欄漸變
- Android仿淘寶商品拖動查看詳情及標(biāo)題欄漸變功能
- 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滑動使標(biāo)題欄漸變背景色的實例代碼
相關(guān)文章
android中打開相機(jī)、打開相冊進(jìn)行圖片的獲取示例
本篇文章主要介紹了android中打開相機(jī)、打開相冊進(jìn)行圖片的獲取示例,非常具有實用價值,需要的朋友可以參考下。2017-01-01
詳解如何在Android中實現(xiàn)懸浮Activity
本篇文章主要介紹詳解如何在Android中實現(xiàn)懸浮Activity,通過修改Activity的實現(xiàn)來適配平板設(shè)備,已達(dá)到代碼的最大利用率。有興趣的可以了解一下。2017-01-01
Android 8.0如何完美適配全局dialog懸浮窗彈出
這篇文章主要給大家介紹了關(guān)于Android 8.0如何完美適配全局dialog懸浮窗彈出的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起看看吧2018-07-07
Android中WebView圖片實現(xiàn)自適應(yīng)的方法
這篇文章主要介紹了Android中WebView圖片實現(xiàn)自適應(yīng)的方法,涉及Android操作圖片顯示的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Android列表實現(xiàn)(1)_數(shù)組列表實例介紹
最近開始學(xué)習(xí)android的ui,先上幾個相關(guān)的例子,后續(xù)還會有更新,感興趣的朋友可以研究下2012-12-12

