最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android ScrollView滑動實現仿QQ空間標題欄漸變

 更新時間:2020年04月16日 15:38:43   作者:lyhhj  
這篇文章主要為大家詳細介紹了Android ScrollView滑動實現仿QQ空間標題欄漸變,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

今天來研究的是ScrollView-滾動視圖,滾動視圖又分橫向滾動視圖(HorizontalScrollView)和縱向滾動視圖(ScrollView),今天主要研究縱向的。相信大家在開發(fā)中經常用到,ScrollView的功能已經很強大了,但是仍然滿足不了我們腦洞大開的UI設計師們,所以我們要自定義…本篇文章主要講監(jiān)聽ScrollView的滑動實現仿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毫秒后開始淡化,以毫秒為單位。 

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

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.
     */

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

但是這個方法我們不可以調用,我們可以重寫接口或者重寫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的滑動來改變標題欄的透明度實現漸變:
我們先看一下布局:

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

更多關于滑動功能的文章,請點擊專題: 《Android滑動功能》

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Material Design系列之Behavior實現Android知乎首頁

    Material Design系列之Behavior實現Android知乎首頁

    這篇文章主要為大家詳細介紹了Material Design系列之Behavior實現Android知乎首頁的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • android繪制多個黑豎線條

    android繪制多個黑豎線條

    這篇文章主要為大家詳細介紹了android繪制多個黑豎線條,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 關于ADB的Android Debug Bridge(安卓調試橋)那些事

    關于ADB的Android Debug Bridge(安卓調試橋)那些事

    這篇文章主要介紹了關于ADB的Android Debug Bridge(安卓調試橋)那些事,需要的朋友可以參考下
    2019-10-10
  • Android 調用百度地圖API示例

    Android 調用百度地圖API示例

    在Android開發(fā)中有一個非常重要的應用就是實時定位,通過手機在手機地圖上進行實時定位,定位當前手機的位置,這篇文章主要介紹了Android 調用百度地圖API示例,有興趣的可以了解一下。
    2017-01-01
  • 解析Android框架之OkHttp3源碼

    解析Android框架之OkHttp3源碼

    OkHttp3是一個處理網絡請求的開源項目,是安卓端最火熱的輕量級框架。本文將詳細解析它的源碼。
    2021-06-06
  • android網絡圖片查看器簡單實現代碼

    android網絡圖片查看器簡單實現代碼

    這篇文章主要為大家詳細介紹了android網絡圖片查看器的簡單實現代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android仿IOS系統(tǒng)懸浮窗效果

    Android仿IOS系統(tǒng)懸浮窗效果

    這篇文章主要為大家詳細介紹了Android仿IOS系統(tǒng)懸浮窗效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android清除工程中無用資源文件的兩種方法

    Android清除工程中無用資源文件的兩種方法

    這篇文章主要介紹了Android清除工程中無用資源文件的兩種方法,調用Android lint命令查找出無用資源,二是使用代碼自動刪除無用的文件,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Kotlin Suspend掛起函數的使用詳解

    Kotlin Suspend掛起函數的使用詳解

    這里介紹 Kotlin Suspend 掛起函數的使用。掛起(suspend)函數是所有協(xié)程的核心。 掛起函數可以執(zhí)行長時間運行的操作并等待它完成而不會阻塞主線程。Kotlin 的 suspend 關鍵字可以幫助我們消除回調,用同步的寫法寫異步
    2023-02-02
  • Android仿淘寶訂單頁面效果

    Android仿淘寶訂單頁面效果

    這篇文章主要介紹了Android仿淘寶訂單頁面效果,電商項目的訂單管理模塊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論

华容县| 潍坊市| 台北县| 德惠市| 天峨县| 儋州市| 丁青县| 华宁县| 原阳县| 荣成市| 阜康市| 博爱县| 丰城市| 延边| 修文县| 远安县| 乌拉特中旗| 阳原县| 汕头市| 毕节市| 砀山县| 蓬溪县| 临泽县| 读书| 舒兰市| 临沭县| 旌德县| 五华县| 宝鸡市| 察哈| 蒙自县| 平安县| 甘肃省| 玛多县| 镇原县| 崇信县| 平舆县| 乌拉特中旗| 台中市| 大关县| 天台县|