Android 實(shí)現(xiàn)ToolBar滑動(dòng)漸變

title.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:id="@+id/top_bg"

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"

? ? android:background="@color/white"

? ? android:orientation="vertical">

? ? <View

? ? ? ? android:id="@+id/viewTop"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="25dp" />

? ? <RelativeLayout

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="45dp">

? ? ? ? <ImageView

? ? ? ? ? ? android:id="@+id/toolbar_iv_back"

? ? ? ? ? ? android:layout_width="45dp"

? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? android:layout_gravity="center"

? ? ? ? ? ? android:padding="14dp"

? ? ? ? ? ? android:src="@drawable/img_back" />

? ? ? ? <TextView

? ? ? ? ? ? android:id="@+id/toolbar_tv_title"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_centerInParent="true"

? ? ? ? ? ? android:layout_marginLeft="45dp"

? ? ? ? ? ? android:layout_marginRight="60dp"

? ? ? ? ? ? android:ellipsize="end"

? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? android:lines="1"

? ? ? ? ? ? android:text="我是標(biāo)題"

? ? ? ? ? ? android:textSize="17sp"

? ? ? ? ? ? />

? ? ? ? <LinearLayout

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? android:layout_alignParentRight="true"

? ? ? ? ? ? android:layout_centerVertical="true">

? ? ? ? ? ? <TextView

? ? ? ? ? ? ? ? android:id="@+id/toolbar_tv_menu"

? ? ? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? ? ? android:lines="1"

? ? ? ? ? ? ? ? android:paddingLeft="10dp"

? ? ? ? ? ? ? ? android:paddingRight="10dp"

? ? ? ? ? ? ? ? android:textSize="14sp"

? ? ? ? ? ? ? ? />

? ? ? ? ? ? <ImageView

? ? ? ? ? ? ? ? android:id="@+id/toolbar_iv_menu"

? ? ? ? ? ? ? ? android:layout_width="45dp"

? ? ? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? ? ? android:padding="14dp"

? ? ? ? ? ? ? ? android:visibility="gone" />

? ? ? ? </LinearLayout>

? ? </RelativeLayout>

? ? <View

? ? ? ? android:id="@+id/view_line"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="0.5dp"

? ? ? ? android:background="@color/background"

? ? ? ? />

</LinearLayout>



activity_main.xml


<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">

? ? <com.cy.myapplication.MyScrollView

? ? ? ? android:id="@+id/scroller"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="match_parent">

? ? ? ? <LinearLayout

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? android:orientation="vertical">

? ? ? ? ? ? <LinearLayout

? ? ? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? ? ? android:layout_height="2000dp"

? ? ? ? ? ? ? ? android:background="@color/colorAccent" />

? ? ? ? </LinearLayout>

? ? </com.cy.myapplication.MyScrollView>

? ? <include

? ? ? ? layout="@layout/title" />

</RelativeLayout>



MyScrollView? 自定義滾動(dòng)View

package com.cy.myapplication;

import android.content.Context;

import android.os.Build;

import android.os.Handler;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.widget.ScrollView;

import androidx.annotation.RequiresApi;

/**

* Created by ShinnyYang on 2020/8/18.

*/

public class MyScrollView extends ScrollView {

? ? private OnScrollListener onScrollListener;

? ? /**

? ? * 主要是用在用戶(hù)手指離開(kāi)MyScrollView雳窟,MyScrollView還在繼續(xù)滑動(dòng)伐蒂,我們用來(lái)保存Y的距離,然后做比較

? ? */

? ? private int lastScrollY;

? ? public MyScrollView(Context context) {

? ? ? ? super(context);

? ? }

? ? public MyScrollView(Context context, AttributeSet attrs) {

? ? ? ? super(context, attrs);

? ? }

? ? public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {

? ? ? ? super(context, attrs, defStyleAttr);

? ? }

? ? @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

? ? public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

? ? ? ? super(context, attrs, defStyleAttr, defStyleRes);

? ? }

? ? /**

? ? * 設(shè)置滾動(dòng)接口

? ? * @param onScrollListener

? ? */

? ? public void setScrolListener(OnScrollListener onScrollListener){

? ? ? ? this.onScrollListener = onScrollListener;

? ? }

? ? /**

? ? * 用于用戶(hù)手指離開(kāi)MyScrollView的時(shí)候獲取MyScrollView滾動(dòng)的Y距離裸违,然后回調(diào)給onScroll方法中

? ? */

? ? private Handler handler = new Handler() {

? ? ? ? public void handleMessage(android.os.Message msg) {

? ? ? ? ? ? int scrollY = MyScrollView.this.getScrollY();

? ? ? ? ? ? if(onScrollListener != null){

? ? ? ? ? ? ? ? onScrollListener.onScroll(scrollY);

? ? ? ? ? ? }

? ? ? ? ? ? //此時(shí)的距離和記錄下的距離不相等音同,在隔5毫秒給handler發(fā)送消息

? ? ? ? ? ? if(lastScrollY != scrollY){

? ? ? ? ? ? ? ? lastScrollY = scrollY;

? ? ? ? ? ? ? ? handler.sendMessageDelayed(handler.obtainMessage(), 5);

? ? ? ? ? ? }

? ? ? ? }

? ? };

? ? /**

? ? * 重寫(xiě)onTouchEvent欠动, 當(dāng)用戶(hù)的手在MyScrollView上面的時(shí)候贸营,

? ? * 直接將MyScrollView滑動(dòng)的Y方向距離回調(diào)給onScroll方法中骇吭,當(dāng)用戶(hù)抬起手的時(shí)候橙弱,

? ? * MyScrollView可能還在滑動(dòng),所以當(dāng)用戶(hù)抬起手我們隔20毫秒給handler發(fā)送消息燥狰,在handler處理

? ? * MyScrollView滑動(dòng)的距離

? ? */

? ? @Override

? ? public boolean onTouchEvent(MotionEvent ev) {

? ? ? ? if(onScrollListener != null){

? ? ? ? ? ? onScrollListener.onScroll(lastScrollY = this.getScrollY());

? ? ? ? }

? ? ? ? if(ev.getAction() == MotionEvent.ACTION_UP){

? ? ? ? ? ? handler.sendMessageDelayed(handler.obtainMessage(), 20);

? ? ? ? }

? ? ? ? return super.onTouchEvent(ev);

? ? }

? ? public interface OnScrollListener{

? ? ? ? /**

? ? ? ? * 回調(diào)方法棘脐, 返回MyScrollView滑動(dòng)的Y方向距離

? ? ? ? * @param scrollY

? ? ? ? *? ? ? ? ? ? ? 、

? ? ? ? */

? ? ? ? public void onScroll(int scrollY);

? ? }

}


Main里面實(shí)現(xiàn)


private LinearLayout top_bg;

private MyScrollView scroller;


private void initView() {

? ? top_bg = (LinearLayout) findViewById(R.id.top_bg);

? ? scroller = (MyScrollView) findViewById(R.id.scroller);

? ? top_bg.getBackground().setAlpha(0);

? ? scroller.setScrolListener(this);

}


@Override

public void onScroll(int scrollY) {

? ? if (scrollY < 100) {

? ? ? ? top_bg.getBackground().setAlpha(0);

? ? } else if (scrollY >= 100 && scrollY < 860) {

? ? ? ? top_bg.getBackground().setAlpha((scrollY - 100) / 3);

? ? } else {

? ? ? ? top_bg.getBackground().setAlpha(255);

? ? }

}




<color name="white">#ffffff</color>

<color name="black">#000000</color>

<color name="background">#f6f6f6</color>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末龙致,一起剝皮案震驚了整個(gè)濱河市蛀缝,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌目代,老刑警劉巖屈梁,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異榛了,居然都是意外死亡在讶,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)霜大,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)构哺,“玉大人,你說(shuō)我怎么就攤上這事僧诚≌谏簦” “怎么了蝗碎?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)旗扑。 經(jīng)常有香客問(wèn)我蹦骑,道長(zhǎng),這世上最難降的妖魔是什么臀防? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任眠菇,我火速辦了婚禮,結(jié)果婚禮上袱衷,老公的妹妹穿的比我還像新娘捎废。我一直安慰自己,他們只是感情好致燥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布登疗。 她就那樣靜靜地躺著,像睡著了一般嫌蚤。 火紅的嫁衣襯著肌膚如雪辐益。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天脱吱,我揣著相機(jī)與錄音智政,去河邊找鬼。 笑死箱蝠,一個(gè)胖子當(dāng)著我的面吹牛续捂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播宦搬,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼牙瓢,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了床三?” 一聲冷哼從身側(cè)響起一罩,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎撇簿,沒(méi)想到半個(gè)月后聂渊,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡四瘫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年汉嗽,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片找蜜。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡饼暑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情弓叛,我是刑警寧澤彰居,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站撰筷,受9級(jí)特大地震影響陈惰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜毕籽,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一抬闯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧关筒,春花似錦溶握、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至廉赔,卻和暖如春肉微,著一層夾襖步出監(jiān)牢的瞬間匾鸥,已是汗流浹背蜡塌。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留勿负,地道東北人馏艾。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像奴愉,于是被迫代替她去往敵國(guó)和親琅摩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354