国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android ScrollView滑動實現(xiàn)仿QQ空間標題欄漸變

2019-12-12 05:48:00
字體:
供稿:網(wǎng)友

今天來研究的是ScrollView-滾動視圖,滾動視圖又分橫向滾動視圖(HorizontalScrollView)和縱向滾動視圖(ScrollView),今天主要研究縱向的。相信大家在開發(fā)中經(jīng)常用到,ScrollView的功能已經(jīng)很強大了,但是仍然滿足不了我們腦洞大開的UI設計師們,所以我們要自定義…本篇文章主要講監(jiān)聽ScrollView的滑動實現(xiàn)仿QQ空間標題欄漸變,先看一下效果圖:

 

好了我們切入主題。

有可能你不知道的那些ScrollView屬性
 •android:scrollbars
設置滾動條顯示。none(隱藏),horizontal(水平),vertical(垂直)
 •android:scrollbarStyle
設置滾動條的風格和位置。設置值:insideOverlay、insideInset、outsideOverlay、outsideInset
 •android:scrollbarThumbHorizontal
設置水平滾動條的drawable。
 •android:soundEffectsEnabled
設置點擊或觸摸時是否有聲音效果
 •android:fadingEdge
設置拉滾動條時,邊框漸變的放向。none(邊框顏色不變),horizontal(水平方向顏色變淡),vertical(垂直方向顏色變淡)。參照fadingEdgeLength的效果圖 android:fadingEdgeLength 設置邊框漸變的長度
 •android:scrollX
以像素為單位設置水平方向滾動的的偏移值,在GridView中可看的這個效果
 •android:scrollY
以像素為單位設置垂直方向滾動的的偏移值
 •android:scrollbarAlwaysDrawHorizontalTrack
設置是否始終顯示垂直滾動條
 •android:scrollbarDefaultDelayBeforeFade
設置N毫秒后開始淡化,以毫秒為單位。 

以上這些屬性有興趣的可以去研究一下,這里就不詳細講了。很多屬性并不常用,下面說說我們經(jīng)常用的,怎樣監(jiān)聽ScrollView的滑動并實現(xiàn)標題欄的漸變?

ScrollView滑動監(jiān)聽:

Google并沒有給我們提供ScrollView的滑動距離、是否滑動到布局底部、頂部的方法,但是提供了一個onScrollChanged方法:

@Override  protected void onScrollChanged(int x, int y, int oldx, int oldy) {    super.onScrollChanged(x, y, oldx, oldy);    //todo:    }  }

通過查看源碼注釋,

/**
     * This is called in response to an internal scroll in this view (i.e., the
     * view scrolled its own contents). This is typically as a result of
     *
{@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
     * called.
     *
     * @param l Current horizontal scroll origin.
     * @param t Current vertical scroll origin.
     * @param oldl Previous horizontal scroll origin.
     * @param oldt Previous vertical scroll origin.
     */

我們可以知道這個方法的參數(shù)分別為:
l:當前橫向滑動距離
t:當前縱向滑動距離
oldl:之前橫向滑動距離
oldt:之前縱向滑動距離

但是這個方法我們不可以調(diào)用,我們可以重寫接口或者重寫ScrollView暴露該方法:

package com.hankkin.gradationscroll;import android.content.Context;import android.util.AttributeSet;import android.widget.ScrollView;/** * 帶滾動監(jiān)聽的scrollview * */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)聽暴露出來我們就該去設置標題欄隨著ScrollView的滑動來改變標題欄的透明度實現(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>

 

最外層是我們自定義的ScrollView,包裹著一張背景圖片和一個ListView(ListView重寫為不可以滑動),然后布局的上面有一個TextView當做標題欄,你也可以用布局。

然后我們需要獲取圖片的高度,并且設置滾動監(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));    }  }

OK,這就實現(xiàn)了你在最上方看到的效果了。
其實并不難,只是我們沒有親自動手去實現(xiàn),相信多動手自己親自去實現(xiàn)一下,UI想要的我們都可以實現(xiàn)。
源碼地址:https://github.com/Hankkin/GradationTitleBar
項目里面我還添加了一個帶banner的,原理是一樣的。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿城市| 宜兰县| 齐齐哈尔市| 林州市| 河池市| 西宁市| 永福县| 孝昌县| 白山市| 榆林市| 抚远县| 贵阳市| 乌拉特中旗| 永平县| 土默特左旗| 千阳县| 徐汇区| 武乡县| 逊克县| 玉田县| 青岛市| 宁国市| 德惠市| 巴彦淖尔市| 读书| 疏勒县| 玛曲县| 丰都县| 威信县| 青冈县| 佛坪县| 达拉特旗| 隆德县| 七台河市| 星子县| 夏邑县| 电白县| 无为县| 土默特右旗| 平定县| 庆城县|