仿豆瓣的title變色方案 android

寫文章寫的正在興頭上,把一年前做的一個(gè)title拿出來(lái)給大家看一下。因?yàn)槭且粋€(gè)仿豆瓣的app,里邊有一個(gè)title變色

1528185742690.gif

不太會(huì)用制圖軟件,沒(méi)錯(cuò)你看到的上邊的這個(gè)就是成品,圖的原因不太流暢,實(shí)際上還可以。基本還原 ,不是單純的變色,是兩種漸變,由不透明到透明,再到不透明,顏色在從透明到半白。

一年前寫的了。不太能記得清晰了,但是我盡量解釋到位

項(xiàng)目地址:點(diǎn)這里 具體位置在com.example.douban.douban4_b.ui.activity.MovieDetilActivity 歡迎查看

具體實(shí)現(xiàn)是一個(gè)behavior ToolbarAlphaDependBehavior 這是android后來(lái)提供的一個(gè)組件,專門用來(lái)進(jìn)行titlebar的變形。

繼承CoordinatorLayout.Behavior<LinearLayout> 之后就可以對(duì)Linearlayout進(jìn)行控制

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, MyTitleBar child, View dependency) {
    return dependency instanceof Toolbar;
}

該方法使用來(lái)制定一個(gè)觀察者,指定我們依據(jù)那個(gè)view來(lái)進(jìn)行變化


Screenshot_20180605-162042.jpg

該圖片上半部分整體用了一個(gè)framelayout進(jìn)行裝載,在下邊放了一個(gè)高度為0的toolbar。
我們觀察的對(duì)象就指定了這個(gè)toolbar。這樣往上滑的時(shí)候就可以依據(jù)toolbar的位置進(jìn)行控制。

這是一個(gè)具體的方案變色實(shí)現(xiàn),基本思想就是計(jì)算toolbar從開(kāi)始到最后移動(dòng)的距離占整個(gè)framelayout的百分比。這樣得到百分比之后就依據(jù)百分比來(lái)進(jìn)行控制顏色,控制透明度。

 @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, MyTitleBar child, View dependency) {

        Drawable drawable = dependency.getBackground();
        dra = (ColorDrawable) drawable;
        float percent;
        if (mStartY == 0) {
            mStartY = (int) dependency.getY() - context.getResources().getDimensionPixelOffset(R.dimen.toolbar_padding_top);
        }
        Logger.w(dependency.getY() + "");
        float endpercent = Math.abs((mStartY - child.getY() * 2) / mStartY);
        Logger.w("childheight" + child.getY() + "");
        //計(jì)算toolbar從開(kāi)始到最后移動(dòng)的百分比
        if (dependency.getY() >= 0) {
            percent = 1 - Math.abs((dependency.getY() - (int) child.getY()) / mStartY);
            Logger.w("percent" + percent + "");
            Logger.w("endpercent" + endpercent + "");
            //改變textview的坐標(biāo)值

            if (percent <= 0) {
                child.setBackgroundColor(0x00FFFFFF);
                child.getBackground().setAlpha(0);
            } else if (0 < percent && percent <= 0.6) {
                child.setBackgroundColor(toobal_old);
                int alpha = (int) Math.abs(percent * 255);
                child.getBackground().setAlpha(alpha);
            } else if (percent >= 0.6 && percent < endpercent) {
                float percenttemp = (float) ((percent - 0.6) / 0.4);
                final int evaluate1 = (Integer) evaluator.evaluate(percenttemp, toobal_old, dra.getColor());
                child.setBackgroundColor(evaluate1);
                int alpha = (int) Math.abs(percent * 255);
                child.getBackground().setAlpha(alpha);
            } else if (percent >= endpercent && percent < 1) {
                float percenttemp = (float) ((percent - 0.6) / 0.4);
                final int evaluate1 = (Integer) evaluator.evaluate(percenttemp, toobal_old, dra.getColor());
                child.setBackgroundColor(evaluate1);
                int alpha = (int) Math.abs(percent * 255);
                child.getBackground().setAlpha(alpha);
                EventBus.getDefault().post(new onTitleMissing());
            } else if (percent >= 1) {
                child.getBackground().setAlpha(255);
                child.setBackgroundColor(dra.getColor());
                EventBus.getDefault().post(new onTitleComplete());
            }
        } else {
            child.getBackground().setAlpha(0);
            child.setBackgroundColor(0x00FFFFFF);
        }

        return true;
    }

最后將這個(gè)behavior賦給自己的title即可。
不算簡(jiǎn)單,我中間跳過(guò)了是因?yàn)轫?xiàng)目里有。
謝謝

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容