仿網(wǎng)易云音樂(lè)歌單詳情頁(yè)頂部效果

  1. 沉浸式狀態(tài)欄
    styles-v19
 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
     
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

styles-v21

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

說(shuō)明: 設(shè)置狀態(tài)欄透明唤衫,可以讓ContentView的內(nèi)容繪制到狀態(tài)欄

  1. fitsSystemWindows
    請(qǐng)參考注釋
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.brandy.blurtoolbar.MainActivity">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/top_img"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:scaleType="centerCrop"
                android:src="@mipmap/img_test" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/content" />
        </LinearLayout>
    </ScrollView>

    <FrameLayout
        android:id="@+id/blur_content"
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <ImageView
            android:id="@+id/bg_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"></ImageView>

        <!--注意這里toolbar的高度用wrapcontent啼止, 這樣設(shè)置android:fitsSystemWindows="true"時(shí)须蜗,toolbar的高度會(huì)增加到statusbarHeight + toolbarHeight  -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/MyToolbar"
            app:title=" "
            app:titleTextAppearance="@style/ToolbarTextAppearance.Title"></android.support.v7.widget.Toolbar>
    </FrameLayout>
</FrameLayout>
  1. 實(shí)現(xiàn)起來(lái)就很容易了
  • 將toolbar透明度設(shè)置為0奇瘦,初始設(shè)置bg_view透明度為0
  • 加載高度高斯模糊的圖片到bg_view和top_img
 Glide.with(MainActivity.this).load(R.mipmap.img_test)
                .centerCrop()
                // 設(shè)置高斯模糊
                .bitmapTransform(new BlurTransformation(MainActivity.this, 20, 20))
                .into(top_img);
  • 監(jiān)聽(tīng)ScrollView的滑動(dòng),根據(jù)滑動(dòng)的距離來(lái)計(jì)算并修改bg_view的透明度
  @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if (topImg_height - mTopHeight >= 0) {
            if (scrollY < topImg_height) {
                float ratio = (float) scrollY / (float) (topImg_height - mTopHeight);
                Log.e(TAG, "onScrollChange: ratio = " + ratio);
                if (ratio <= 1 && ratio >= 0) {
                    bg_view.setAlpha(ratio);
                }
            } else {
                bg_view.setAlpha(1.0f);
            }
        }
    }
  1. 補(bǔ)充
//獲取Toolbar的系統(tǒng)默認(rèn)高度
   private int getToolbarHeight() {
        return getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height_material);
    }

  //獲取Statusbar的高度
  private float getStatusbarHeight() {
        int statusBarHeight1 = -1;
        //獲取status_bar_height資源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根據(jù)資源ID獲取響應(yīng)的尺寸值
            return getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }

在布局中鹦牛,blur_content給定了一個(gè)高度(80dp)羹幸,在獲取到這兩個(gè)高度之后,需要重新修改blur_content的高度赁遗,來(lái)達(dá)到更好的效果(其實(shí)最好是獲取Toolbar測(cè)量后的高度)

參考文章

http://www.open-open.com/lib/view/open1455771321605.html
http://www.reibang.com/p/1995b7135073
http://blog.csdn.net/a_running_wolf/article/details/50477965

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末署辉,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子岩四,更是在濱河造成了極大的恐慌哭尝,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剖煌,死亡現(xiàn)場(chǎng)離奇詭異材鹦,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)耕姊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)桶唐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人茉兰,你說(shuō)我怎么就攤上這事尤泽。” “怎么了规脸?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵坯约,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我莫鸭,道長(zhǎng)闹丐,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任被因,我火速辦了婚禮卿拴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘氏身。我一直安慰自己巍棱,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布蛋欣。 她就那樣靜靜地躺著航徙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪陷虎。 梳的紋絲不亂的頭發(fā)上到踏,一...
    開(kāi)封第一講書(shū)人閱讀 52,682評(píng)論 1 312
  • 那天杠袱,我揣著相機(jī)與錄音,去河邊找鬼窝稿。 笑死楣富,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的伴榔。 我是一名探鬼主播纹蝴,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼踪少!你這毒婦竟也來(lái)了塘安?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤援奢,失蹤者是張志新(化名)和其女友劉穎兼犯,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體集漾,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡切黔,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了具篇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纬霞。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖栽连,靈堂內(nèi)的尸體忽然破棺而出险领,到底是詐尸還是另有隱情,我是刑警寧澤秒紧,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布绢陌,位于F島的核電站,受9級(jí)特大地震影響熔恢,放射性物質(zhì)發(fā)生泄漏脐湾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一叙淌、第九天 我趴在偏房一處隱蔽的房頂上張望秤掌。 院中可真熱鬧,春花似錦鹰霍、人聲如沸闻鉴。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)孟岛。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間渠羞,已是汗流浹背斤贰。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留次询,地道東北人荧恍。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像屯吊,于是被迫代替她去往敵國(guó)和親送巡。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容