Android標(biāo)題欄漸變色意推,沉寂式狀態(tài)欄

1.概述

今天給大家?guī)淼氖悄7翾Q7.0標(biāo)題欄之自定義Toolbar
<big>github項(xiàng)目地址:[模仿QQ7.0標(biāo)題欄之自定義Toolbar]
(https://github.com/laotan7237/EasyReader/blob/master/app/src/main/java/com/laotan/easyreader/view/ColorFilterToolBar.java)</big>
作者原創(chuàng),轉(zhuǎn)載請注明出處謝謝珊蟀。
效果圖:

F29FF03E64DE793AA704581078ED1491.jpg

2.正文

自定義toolbar

首先我們先來說說自定義Toolbar菊值,至于怎么讓狀態(tài)欄和Toolbar顏色相同等等在來說。
以下就是自定義Toolbar的代碼了系洛。接下來開始分析:

public class ColorFilterToolBar extends Toolbar {

    public static final int STRAT_BLUE= 0xFF4D8DFF;
    public static final int END_BLUE= 0xFF37B7FB;
    private Paint mPaint;
    private float windowWidth;
    private int height;

    public ColorFilterToolBar(Context context) {
        this(context, null);
    }

    public ColorFilterToolBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public ColorFilterToolBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        windowWidth = wm.getDefaultDisplay().getWidth();//獲取屏幕寬度
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 1; i <= windowWidth; i++) {
            // 設(shè)置畫筆顏色為自定義顏色
            mPaint.setColor((Integer) evaluateColor(Math.pow(i/ windowWidth,2),STRAT_BLUE,END_BLUE));
            canvas.drawRect(i-1, 0, i, height,mPaint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        height = h;
    }

    /**
     * 顏色變化過度
     *
     * @param fraction
     * @param startValue
     * @param endValue
     * @return
     */
    public Object evaluateColor(double fraction, Object startValue, Object endValue) {
        int startInt = (Integer) startValue;
        int startA = (startInt >> 24) & 0xff;
        int startR = (startInt >> 16) & 0xff;
        int startG = (startInt >> 8) & 0xff;
        int startB = startInt & 0xff;

        int endInt = (Integer) endValue;
        int endA = (endInt >> 24) & 0xff;
        int endR = (endInt >> 16) & 0xff;
        int endG = (endInt >> 8) & 0xff;
        int endB = endInt & 0xff;

        return (startA + (int) (fraction * (endA - startA))) << 24 |
                (startR + (int) (fraction * (endR - startR))) << 16 |
                (startG + (int) (fraction * (endG - startG))) << 8 |
                (startB + (int) (fraction * (endB - startB)));
    }
}

我們先單獨(dú)說說這個顏色改變的方法原理是什么樣的俊性?
其實(shí)很簡單就是傳入一個開頭(startValue)和結(jié)尾的數(shù)字(endValue),然后再傳入一個0-1的數(shù)(fraction)描扯,這時候fraction可以是各種函數(shù)不過要是0-1之間定页。

   /**
     * 顏色變化過度
     *
     * @param fraction
     * @param startValue
     * @param endValue
     * @return
     */
     假設(shè)我們傳入的start是FFFFFFFF,end是00000000
    public Object evaluateColor(double fraction, Object startValue, Object endValue) {
        int startInt = (Integer) startValue;//這個數(shù)字我們就不管了。還是當(dāng)作他是FFFFFFFF
        int startA = (startInt >> 24) & 0xff;
        startInt >> 24=0xff  0xff&0xff = 0xff;下面以此類推
        int startR = (startInt >> 16) & 0xff;
        int startG = (startInt >> 8) & 0xff;
        int startB = startInt & 0xff;

        int endInt = (Integer) endValue;
        int endA = (endInt >> 24) & 0xff;
         startInt >> 24=0x00  0x00&0xff = 0x00;下面以此類推
        int endR = (endInt >> 16) & 0xff;
        int endG = (endInt >> 8) & 0xff;
        int endB = endInt & 0xff;

        return (startA + (int) (fraction * (endA - startA))) << 24 |
                (startR + (int) (fraction * (endR - startR))) << 16 |
                (startG + (int) (fraction * (endG - startG))) << 8 |
                (startB + (int) (fraction * (endB - startB)));
    }

大家自己多看看理解下就可以了绽诚。

構(gòu)造方法我就不說了典徊,onSizeChanged就是獲取到toolbar的高度。

重點(diǎn)看看ondraw方法就可以了

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 1; i <= windowWidth; i++) {
            // 設(shè)置畫筆顏色為自定義顏色
            mPaint.setColor((Integer) evaluateColor(Math.pow(i/ windowWidth,2),STRAT_BLUE,END_BLUE));
            canvas.drawRect(i-1, 0, i, height,mPaint);
        }
    }

這個方法里面我們用一個for循環(huán)去遍歷屏幕寬度恩够,把屏幕寬度分成諾干個卒落,1080*1920就是1080份以此類推。
然后我們給畫筆設(shè)置顏色 mPaint.setColor((Integer) evaluateColor(Math.pow(i/ windowWidth,2),STRAT_BLUE,END_BLUE));
這個畫筆顏色就是通過上面那個方法得到一個顏色值蜂桶,Math.pow(i/ windowWidth,2)這個就是一個函數(shù)也可以直接用i/ windowWidth儡毕;后面這個是一次函數(shù)。一次函數(shù)的圖像變化就是顏色變化規(guī)律。

核心思想

我們要把toolbar分成諾干個小矩形腰湾,然后他的寬度是1px雷恃,高度是onsizechange獲取的所以就有 canvas.drawRect(i-1, 0, i, height,mPaint)
經(jīng)過for循環(huán)1080次畫了1080個小矩形就形成了背景圖。

使用過程

  • 在xml布局里面的代碼如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.laotan.easyreader.view.ColorFilterToolBar
        android:id="@+id/toolbar_base_activity"
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:fitsSystemWindows="true"
        app:theme="@style/ToolbarStyle">
        <ImageView
            android:id="@+id/iv_base_back"
            android:layout_marginLeft="@dimen/dp_10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/icon_back"/>
        <TextView
            android:id="@+id/tv_toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:maxLines="1"
            android:text="我是標(biāo)題"
            android:textColor="@color/colorWhite"
            android:textSize="20sp" />
        </com.laotan.easyreader.view.ColorFilterToolBar>

    <FrameLayout
        android:id="@+id/fl_toolbar_base"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

注意到toolbar有這么一個屬性android:fitsSystemWindows="true"這個就是為了讓標(biāo)題欄和toolbar顏色相同的费坊。不過還要在stayle中配置這句 <item name="android:windowTranslucentStatus">true</item>如下:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorTheme</item>
        <item name="colorPrimaryDark">@color/colorTheme</item>
        <item name="colorAccent">@color/colorAccent</item>

        //沉浸狀態(tài)欄
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        //可以讓appbarlayout從最頂上開始倒槐,狀態(tài)欄會覆蓋toolbar

        //ActionBarDrawerToggle設(shè)置顏色
        <item name="colorControlNormal">@android:color/white</item>
    </style>

style在mainifest中的使用我就不說了。
之后可能還會寫寫QQ7.0下面會動的小人哈哈.
好了文章到這里就結(jié)束了附井,如果哪里寫的不好或者不懂讨越,請大家多反饋。喜歡的話記得給個star永毅。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末把跨,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子卷雕,更是在濱河造成了極大的恐慌节猿,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件漫雕,死亡現(xiàn)場離奇詭異滨嘱,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)浸间,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門太雨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人魁蒜,你說我怎么就攤上這事囊扳。” “怎么了兜看?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵锥咸,是天一觀的道長。 經(jīng)常有香客問我细移,道長搏予,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任弧轧,我火速辦了婚禮雪侥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘精绎。我一直安慰自己速缨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布代乃。 她就那樣靜靜地躺著旬牲,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上引谜,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天牍陌,我揣著相機(jī)與錄音,去河邊找鬼员咽。 笑死,一個胖子當(dāng)著我的面吹牛贮预,可吹牛的內(nèi)容都是我干的贝室。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼仿吞,長吁一口氣:“原來是場噩夢啊……” “哼滑频!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起唤冈,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤峡迷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后你虹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體绘搞,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年傅物,在試婚紗的時候發(fā)現(xiàn)自己被綠了夯辖。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡董饰,死狀恐怖蒿褂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情卒暂,我是刑警寧澤啄栓,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站也祠,受9級特大地震影響昙楚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜齿坷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一桂肌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧永淌,春花似錦崎场、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春螃宙,著一層夾襖步出監(jiān)牢的瞬間蛮瞄,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工谆扎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挂捅,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓堂湖,卻偏偏與公主長得像闲先,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子无蜂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

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