Android新控件之MotionLayout效果:仿京東頂部搜索框變化效果<十>

搜索框效果.gif

近年來越來越多的App首頁搜索控件添加了動(dòng)畫 什么SVG展示搜索圖標(biāo)動(dòng)畫,以及滑動(dòng)中變化搜索框的動(dòng)畫.本文就是MotionLayout 簡單實(shí)現(xiàn)收拾框的簡單平移,背景縮放,搜索框尺寸變化的效果

目標(biāo)

  • 背景縮放,漸變
  • 搜索框尺寸變成置頂寬度
  • 搜索框懸浮

思路

CoordinatorLayout+MotionLayout +ScrollView,CoordinatorLayout關(guān)聯(lián)滑動(dòng),MotionLayout 實(shí)現(xiàn)關(guān)聯(lián)滑動(dòng)中的動(dòng)畫.滑動(dòng)中修改背景的縮放大小從 1.2 變成 1.0 透明度從 1.0 到0.2 變化 咋此期間動(dòng)態(tài)控制 搜索控件的寬度

實(shí)現(xiàn)

1.MotionScene 配置動(dòng)畫效果 scene_search.xml(xml包下)

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:background="@color/white"
    android:fitsSystemWindows="false">

    <!-- 頂部布局   -->
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_layout"
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <!--      minHeight: 最小高度  -->
        <!--      background: 背景色  -->
        <!--      layoutDescription : 配置文件-->
        <androidx.constraintlayout.motion.widget.MotionLayout

            android:id="@+id/ml"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:fitsSystemWindows="false"
            android:minHeight="60dp"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layoutDescription="@xml/scene_search"
            app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed"
            tools:ignore="MotionLayoutInvalidSceneFileReference">

            <ImageView
                android:id="@+id/backgroundIcon"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:src="@mipmap/roard" />


            <RelativeLayout
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@drawable/shape_10dp">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/iv_search"
                    android:hint="請輸入搜索的內(nèi)容"
                    android:padding="10dp"
                    android:textColorHint="@color/tintImage"
                    android:textSize="14dp" />

                <ImageView
                    android:id="@+id/iv_search"
                    android:layout_width="23dp"
                    android:layout_height="23dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="15dp"
                    android:src="@mipmap/iv_search" />
            </RelativeLayout>

        </androidx.constraintlayout.motion.widget.MotionLayout>

    </com.google.android.material.appbar.AppBarLayout>
    <!-- 移動(dòng)的控件 -->
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

2.頁面布局 activity_search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:background="@color/white"
    android:fitsSystemWindows="false">

    <!-- 頂部布局   -->
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_layout"
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <!--      minHeight: 最小高度  -->
        <!--      background: 背景色  -->
        <!--      layoutDescription : 配置文件-->
        <androidx.constraintlayout.motion.widget.MotionLayout

            android:id="@+id/ml"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:fitsSystemWindows="false"
            android:minHeight="60dp"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layoutDescription="@xml/scene_search"
            app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed"
            tools:ignore="MotionLayoutInvalidSceneFileReference">

            <ImageView
                android:id="@+id/backgroundIcon"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:src="@mipmap/roard" />


            <RelativeLayout
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@drawable/shape_10dp">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/iv_search"
                    android:hint="請輸入搜索的內(nèi)容"
                    android:padding="10dp"
                    android:textColorHint="@color/tintImage"
                    android:textSize="14dp" />

                <ImageView
                    android:id="@+id/iv_search"
                    android:layout_width="23dp"
                    android:layout_height="23dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="15dp"
                    android:src="@mipmap/iv_search" />
            </RelativeLayout>

        </androidx.constraintlayout.motion.widget.MotionLayout>

    </com.google.android.material.appbar.AppBarLayout>
    <!-- 移動(dòng)的控件 -->
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

3.頁面代碼

package com.wu.material.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import com.google.android.material.appbar.AppBarLayout
import com.wu.material.R

/**
 * @author wkq
 *
 * @date 2022年01月24日 13:56
 *
 *@des
 *
 */

class SearchLayoutActivity : AppCompatActivity() {

    @SuppressLint("RestrictedApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_layout)
        //顯示路徑
        var motionLayout = findViewById<MotionLayout>(R.id.ml)
//        motionLayout.setDebugMode(MotionLayout.DEBUG_SHOW_PATH)


        var appBarLayout = findViewById<AppBarLayout>(R.id.app_layout)
        appBarLayout.addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener {
            override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
                //綁定 MotionLayout偏移量
                motionLayout.progress = -verticalOffset / appBarLayout?.totalScrollRange?.toFloat()!!
            }
        })
    }


}

總結(jié)

滑動(dòng)搜索框動(dòng)畫簡單實(shí)現(xiàn),有興趣的可以多搞幾個(gè)控件實(shí)現(xiàn)關(guān)聯(lián)動(dòng)畫,什么頭像移動(dòng),索索圖標(biāo)切換什么的

注意!!!
MotionScene 文件放在新建的../res/xml文件夾下

參考文獻(xiàn)

1.Google的MotionLayout介紹說明

2.MotionLayout的文檔簡介

3.MotionLayout 源碼地址

4. 源碼地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末钉寝,一起剝皮案震驚了整個(gè)濱河市饭于,隨后出現(xiàn)的幾起案子注竿,更是在濱河造成了極大的恐慌遇骑,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件客们,死亡現(xiàn)場離奇詭異乳乌,居然都是意外死亡逻炊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門慢宗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來坪蚁,“玉大人,你說我怎么就攤上這事镜沽∶粑睿” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵缅茉,是天一觀的道長嘴脾。 經(jīng)常有香客問我,道長蔬墩,這世上最難降的妖魔是什么译打? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮拇颅,結(jié)果婚禮上奏司,老公的妹妹穿的比我還像新娘。我一直安慰自己樟插,他們只是感情好韵洋,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著黄锤,像睡著了一般麻献。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上猜扮,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天勉吻,我揣著相機(jī)與錄音,去河邊找鬼旅赢。 笑死齿桃,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的煮盼。 我是一名探鬼主播短纵,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼僵控!你這毒婦竟也來了香到?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎悠就,沒想到半個(gè)月后千绪,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡梗脾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年荸型,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片炸茧。...
    茶點(diǎn)故事閱讀 39,977評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡瑞妇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出梭冠,到底是詐尸還是另有隱情辕狰,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布控漠,位于F島的核電站柳琢,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏润脸。R本人自食惡果不足惜柬脸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望毙驯。 院中可真熱鬧倒堕,春花似錦、人聲如沸爆价。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽铭段。三九已至骤宣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間序愚,已是汗流浹背憔披。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留爸吮,地道東北人芬膝。 一個(gè)月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像形娇,于是被迫代替她去往敵國和親锰霜。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評論 2 355

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