coordinatorlayout 滑动监听_MotionLayout系列之配合布局CoordinatorLayout, DrawerLayout, ViewPager使用...

本文详细介绍了如何在Coordinatorlayout、DrawerLayout和ViewPager中使用MotionLayout,通过实例展示了如何实现滑动监听和动画效果。同时,文章还探讨了如何结合MotionLayout与Lottie动画,以及在ViewPager头部添加动画。
摘要由CSDN通过智能技术生成
dcbe816c5088c34a506773b5074b19c1.png

作者:谈小龙 链接:https://www.jianshu.com/p/ddf434aa4de8

在前量部分我们引入了几个示例:

  • 基础使用

  • 滑动处理

  • 自定义属性插值

  • 关键帧

在一,二部分已经大量的介绍了 MotionLayout 的功能,在这部分我们将介绍如何在已有的项目中使用 MotionLayout ,整合到已有的布局中(CoordinatorLayout, DrawerLayout, ViewPager)。

在 Coordinatorlayout 中使用 MotionLayout:

( MotionLayout 可以实现类似 CoodinatorLayout 的功能,我们将在以后的文章中提供示例)

可以通过 MotionLayout 指定一部分 View 的动画,将更多有趣的动画加到已经存在的布局中。

例如,下图中的效果:

85651956dcf5594896fc8cc5d9a79294.gif

可以使用 MotionLayout 替换 AppBarLayout 中的 Toolbar , 通过 CoordinatorLayout 控制动画的进度。尽管你可以通过 setProgress() 方法控制 MotionLayout 转换的进度,也可以创建一个子类通过监听 AppBarlayout 的偏移自动修改:

package com.google.androidstudio.motionlayoutexample.utils

import android.content.Context
import android.support.constraint.motion.MotionLayout
import android.support.design.widget.AppBarLayout
import android.util.AttributeSet

class CollapsibleToolbar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : MotionLayout(context, attrs, defStyleAttr), AppBarLayout.OnOffsetChangedListener {

override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
progress = -verticalOffset / appBarLayout?.totalScrollRange?.toFloat()!!
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
(parent as? AppBarLayout)?.addOnOffsetChangedListener(this)
}
}

然后在布局文件中使用:

<?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:id="@+id/content"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="false"android:background="@color/contentBackground">

<android.support.design.widget.AppBarLayoutandroid:id="@+id/app_bar"android:layout_width="match_parent"android:layout_height="@dimen/app_bar_height"android:theme="@style/AppTheme.AppBarOverlay">

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

android.support.design.widget.AppBarLayout>

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

android.support.design.widget.CoordinatorLayout>

最后我们需要实现motion_09_coordinatorlayout_header 的内容,一个 ImagerView 作为背景和一个 TextView:

<?xml version="1.0" encoding="utf-8"?>
<com.google.androidstudio.motionlayoutexample.utils.CollapsibleToolbarxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/motionLayout"app:layoutDescription="@xml/scene_09"android:layout_width="match_parent"android:layout_height="match_parent"android:minHeight="50dp"android:fitsSystemWindows="false"app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed">

<ImageViewandroid:id="@+id/background"android:layout_width="match_parent"android:layout_height="200dp"android:background="@color/colorAccent"android:scaleType="centerCrop"android:src="@drawable/monterey"/>

<TextViewandroid:id="@+id/label"android:layout_width="wrap_content"android:layout_height="wrap_content"android:transformPivotX="0dp"android:transformPivotY="0dp"android:text="Monterey"android:textColor="#FFF"android:textSize="32dp" />

com.google.androidstudio.motionlayoutexample.utils.CollapsibleToolbar>

创建自包含的 MotionScene 文件:

<?xml version="1.0"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值