解析ScrollView--仿QQ空間標題欄漸變

来源:http://www.cnblogs.com/ganchuanpu/archive/2017/04/30/6790680.html
-Advertisement-
Play Games

先看一下效果圖: 我們需要獲取圖片的高度,並且設置滾動監聽,隨著滾動的距離來設置標題欄的顏色透明度和字體顏色的透明度 github:https://github.com/ganchuanpu/GradationTitleBar ...


先看一下效果圖:

Gif

<?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);
        }
    }
}

我們需要獲取圖片的高度,並且設置滾動監聽,隨著滾動的距離來設置標題欄的顏色透明度和字體顏色的透明度

/**
  * 獲取頂部圖片高度後,設置滾動監聽
*/
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);
            }
        });
 }

  

/**
     * 滑動監聽
     * @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));
        }
    }

  

github:https://github.com/ganchuanpu/GradationTitleBar

  


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 工廠模式:用函數來封裝,以特定介面來創建對象的細節。 console.log(person1 instanceof person); // false console.log(person1 instanceof Object);//true console.log(person instanceo ...
  • 提到響應式,就不得不提兩個響應式框架——bootstrap和foundation。今天給大家介紹的是foundation框架。 何為“嘗鮮”?就是帶大伙初步瞭解一下foundation的靈活和強大; 何為“踩坑”?就是我把我使用的時候踩過的坑給標個記號,這樣大伙用的時候就可以“繞道而行“啦! ...
  • 博客園有很多漂亮的皮膚,但總是有一些地方我不大喜歡,所以經過慎重考慮,我決定親自動手換個裝。本文將介紹博客園換裝的一些基礎(不涉及標準皮膚的做法),如果你想讓你的博客更炫,可以參考本文入個門,然後自己慢慢摸索。相信你一定比我做的好。 ...
  • 以我的經驗來說,要讓TemplatedControl支持Command的需求不會很多,大部分情況用附加屬性解決這個需求會更便利些,譬如UWPCommunityToolkit的 "HyperlinkExtensions" 。 如果正在從頭設計自定義控制項並真的需要提供命令支持,可以參考這篇文章。支持Co ...
  • 一、簡介 1. NSSession 是 iOS 7 之後發佈的,代替原來的 NSURLConnection 2. NSURLSession 中的請求都看做一個請求任務(task),task 的繼承關係如下圖 NSURLSessionTask 是一個抽象類,提供了一些基本的方法 NSURLSessio ...
  • 眾所周知,view是通過刷新來重繪視圖的,Android系統通過發出VSYNC信號來進行屏幕重繪,刷新的時間間隔為16ms,如果在16ms內view完成你所需要的所有操作,那麼用戶在視覺上就不會產生卡頓的感覺;而如果執行的操作邏輯太多,特別是需要頻繁刷新的界面,就會不斷阻塞主線程,從而導致畫面卡頓。 ...
  • 在預設情況下,RadioButton的 文字位置和文字的距離是不變的,為了可以改變它,我們可以用以下的方法。 1.改變文字的位置 1.改變文字的距離 這樣就OK了 ...
  • AndroidManifest.xml 需要註意的是,在聲明許可權時需要一個android:protectionLevel的屬性,它代表“風險級別”。必須是以下值之一: normal、dangerous、signature、signatureOrSystem。normal表示許可權是低風險的,不會對系統 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...