UI實(shí)例-->仿支付寶首頁(yè)頭部伸縮效果


1. 效果圖

下面是我們將要實(shí)現(xiàn)的效果圖:


效果圖

2.具體實(shí)現(xiàn)

我們可以利用design和v7包中的控件來(lái)實(shí)現(xiàn)(涉及到的控件有CoordinatorLayout辈毯、AppBarLayout、Toolbar、NestedScrollView)浸颓。

  • 第一步:創(chuàng)建一個(gè)Scrolling Activity
    下面是該activity的界面效果圖
    Scrolling Activity
  • 第二步:編寫最終效果圖需要用到的xml
    在第一步的基礎(chǔ)上構(gòu)造出我們最終想要的效果圖。

(1) include_toolbar_open.xml
該布局是頭部展開時(shí)toolbar對(duì)應(yīng)的效果圖:

頭部展開時(shí)toolbar的效果圖

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary"
    >

    <RelativeLayout
        android:layout_margin="@dimen/dimen_10dp"
        android:layout_toLeftOf="@+id/iv_contact"
        android:background="@drawable/shape_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/dimen_10dp"
        android:paddingRight="@dimen/dimen_10dp">

        <ImageView
            android:id="@+id/iv_search"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:src="@mipmap/ic_search"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/iv_search"
            android:hint="搜索商品"
            android:background="@null"
            android:paddingLeft="@dimen/dimen_10dp"
            android:textColorHint="@android:color/white"
            android:textSize="14sp"/>
    </RelativeLayout>
    <ImageView
        android:id="@+id/iv_plus"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/dimen_10dp"
        android:src="@mipmap/ic_add"/>
    <ImageView
        android:id="@+id/iv_contact"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/dimen_10dp"
        android:layout_toLeftOf="@id/iv_plus"
        android:src="@mipmap/ic_contact"/>
    <View
        android:id="@+id/bg_toolbar_open"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

(2) include_toolbar_close.xml
該布局是頭部收縮時(shí)toolbar對(duì)應(yīng)的效果圖:

頭部收縮時(shí)toolbar的效果圖

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary">

    <ImageView
        android:id="@+id/iv_scan"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:src="@mipmap/ic_scan"/>
    <ImageView
        android:id="@+id/iv_pay"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/iv_scan"
        android:src="@mipmap/ic_payment"/>
    <ImageView
        android:id="@+id/iv_charge"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/iv_pay"
        android:src="@mipmap/ic_transfer"/>
    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/iv_charge"
        android:src="@mipmap/ic_card"/>
    <ImageView
        android:id="@+id/iv_plus"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:layout_toRightOf="@+id/iv_scan"
        android:src="@mipmap/ic_add"/>
    <View
        android:id="@+id/bg_toolbar_close"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

(3) include_open.xml
該布局是頭部打開時(shí)折疊區(qū)對(duì)應(yīng)的效果圖:

頭部打開時(shí)折疊區(qū)對(duì)應(yīng)的效果圖

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

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

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_scan"/>
            <TextView
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="掃一掃"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_payment"/>
            <TextView
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="付錢"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_transfer"/>
            <TextView
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="收錢"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_card"/>
            <TextView
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="卡包"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </LinearLayout>
    <View
        android:id="@+id/bg_content"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>
</FrameLayout>

(3) content_zfbhome_telescopic.xml
內(nèi)容區(qū)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activity.ZFBHomeTelescopicActivity"
    tools:showIn="@layout/activity_zfbhome_telescopic">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:text="@string/large_text" />

</android.support.v4.widget.NestedScrollView>

(3) activity_zfbhome_telescopic.xml
最終的在activity布局里的效果圖:

最終效果圖

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".activity.ZFBHomeTelescopicActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            >

            <include
                layout="@layout/include_open"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin" >
                <include android:id="@+id/include_toolbar_open"
                    layout="@layout/include_toolbar_open"/>
                <include android:id="@+id/include_toolbar_close"
                    layout="@layout/include_toolbar_close"/>

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_zfbhome_telescopic" />


</android.support.design.widget.CoordinatorLayout>

注:布局中涉及到的一些屬性具體的作用就不做闡述了,可以自行查閱。

  • 第三步:主Activity(ZFBHomeTelescopicActivity.java)
    我們需要對(duì)AppBarLayout設(shè)置偏移監(jiān)聽孝鹊,需要實(shí)現(xiàn)一些背景色縮放效果和收縮展開時(shí)toolbar對(duì)應(yīng)布局的顯示。
public class ZFBHomeTelescopicActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {


    @BindView(R.id.bg_content)
    View bgContent;                 //大布局背景遮罩層
    @BindView(R.id.iv_search)
    ImageView ivSearch;             //
    @BindView(R.id.iv_plus)
    ImageView ivPlus;
    @BindView(R.id.iv_contact)
    ImageView ivContact;
    @BindView(R.id.iv_scan)
    ImageView ivScan;
    @BindView(R.id.iv_pay)
    ImageView ivPay;
    @BindView(R.id.iv_charge)
    ImageView ivCharge;
    @BindView(R.id.bg_toolbar_close)
    View bgToolbarClose;                    //收縮狀態(tài)下toolbar的遮罩層
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_layout)
    CollapsingToolbarLayout toolbarLayout;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.bg_toolbar_open)         //展開狀態(tài)下toolbar的遮罩層
    View bgToolbarOpen;
    private View toolbarOpen;               //展開狀態(tài)下toolbar顯示的內(nèi)容
    private View toolbarClose;              //收縮狀態(tài)下toolbar顯示的內(nèi)容

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zfbhome_telescopic);
        ButterKnife.bind(this);
        toolbarOpen = findViewById(R.id.include_toolbar_open);
        toolbarClose = findViewById(R.id.include_toolbar_close);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        appBar.addOnOffsetChangedListener(this);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        //垂直方向偏移
        int offset = Math.abs(verticalOffset);
        //最大偏移距離
        int scrollRange = appBarLayout.getTotalScrollRange();
        //當(dāng)滑動(dòng)沒超過一半時(shí)展蒂,展開狀態(tài)下toolbar顯示內(nèi)容又活,根據(jù)收縮位置,改變透明值
        if (offset <= scrollRange / 2){
            toolbarOpen.setVisibility(View.VISIBLE);
            toolbarClose.setVisibility(View.GONE);
            //根據(jù)偏移百分比锰悼,計(jì)算透明值
            float scale2 = (float) offset / (scrollRange / 2);
            int alpha2 = (int) (255 * scale2);
            bgToolbarOpen.setBackgroundColor(Color.argb(alpha2,25,131,209));

        }else {//當(dāng)滑動(dòng)超過一半柳骄,收縮狀態(tài)下toolbar顯示內(nèi)容,根據(jù)收縮位置箕般,改變透明值
            toolbarClose.setVisibility(View.VISIBLE);
            toolbarOpen.setVisibility(View.GONE);
            float scale3 = (float) (scrollRange - offset) / (scrollRange / 2);
            int alpha3 = (int) (255 * scale3);
            bgToolbarClose.setBackgroundColor(Color.argb(alpha3,25,131,209));
        }
        //根據(jù)偏移值百分比計(jì)算掃一掃布局的透明度值
        float scale = (float) offset / scrollRange;
        int alpha = (int) (255 * scale);
        bgContent.setBackgroundColor(Color.argb(alpha,25,131,209));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        appBar.removeOnOffsetChangedListener(this);
    }
}

最后獻(xiàn)上完整demo:仿支付寶首頁(yè)頭部伸縮效果

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末耐薯,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子丝里,更是在濱河造成了極大的恐慌可柿,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件丙者,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡营密,警方通過查閱死者的電腦和手機(jī)械媒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人纷捞,你說(shuō)我怎么就攤上這事痢虹。” “怎么了主儡?”我有些...
    開封第一講書人閱讀 156,852評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵奖唯,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我糜值,道長(zhǎng)丰捷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,408評(píng)論 1 283
  • 正文 為了忘掉前任寂汇,我火速辦了婚禮病往,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘骄瓣。我一直安慰自己停巷,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評(píng)論 5 384
  • 文/花漫 我一把揭開白布榕栏。 她就那樣靜靜地躺著畔勤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪扒磁。 梳的紋絲不亂的頭發(fā)上庆揪,一...
    開封第一講書人閱讀 49,772評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音渗磅,去河邊找鬼嚷硫。 笑死,一個(gè)胖子當(dāng)著我的面吹牛始鱼,可吹牛的內(nèi)容都是我干的仔掸。 我是一名探鬼主播,決...
    沈念sama閱讀 38,921評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼医清,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼起暮!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起会烙,我...
    開封第一講書人閱讀 37,688評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤负懦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后柏腻,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纸厉,經(jīng)...
    沈念sama閱讀 44,130評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評(píng)論 2 325
  • 正文 我和宋清朗相戀三年五嫂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了颗品。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肯尺。...
    茶點(diǎn)故事閱讀 38,617評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖躯枢,靈堂內(nèi)的尸體忽然破棺而出则吟,到底是詐尸還是另有隱情,我是刑警寧澤锄蹂,帶...
    沈念sama閱讀 34,276評(píng)論 4 329
  • 正文 年R本政府宣布氓仲,位于F島的核電站,受9級(jí)特大地震影響得糜,放射性物質(zhì)發(fā)生泄漏敬扛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評(píng)論 3 312
  • 文/蒙蒙 一掀亩、第九天 我趴在偏房一處隱蔽的房頂上張望舔哪。 院中可真熱鬧,春花似錦槽棍、人聲如沸捉蚤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)缆巧。三九已至,卻和暖如春豌拙,著一層夾襖步出監(jiān)牢的瞬間陕悬,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工按傅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留捉超,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,315評(píng)論 2 360
  • 正文 我出身青樓唯绍,卻偏偏與公主長(zhǎng)得像拼岳,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子况芒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評(píng)論 2 348

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,749評(píng)論 25 707
  • CoordinatorLayout與滾動(dòng)的處理 CoordinatorLayout實(shí)現(xiàn)了多種Material De...
    cxm11閱讀 6,570評(píng)論 1 15
  • 雨落長(zhǎng)亭心彷徨 東風(fēng)忽起百花調(diào) 一別數(shù)年難...
    是道成歸啊閱讀 265評(píng)論 0 2
  • 寒梅辭去舊樹梢惜纸, 溪邊楊柳舞風(fēng)騷。 梁間鶯燕銜春意绝骚, 隴上嬌花分外妖耐版。 呼玉友,薦溪毛压汪。 殷勤鄰里苦相邀粪牲, 風(fēng)光旖...
    名曰喜閱讀 470評(píng)論 1 5
  • 我是一顆會(huì)流淚的樹 在這國(guó)度,從未被人發(fā)現(xiàn) 也不再期待誰(shuí)會(huì)為我停駐 我是該歡喜還是痛哭 你走近又走出 寒陽(yáng)臥水沙洲...
    巴山夜雨寒閱讀 395評(píng)論 3 0