本文基于B站UP主longway教程编写:第57集 底部导航栏动态效果
一个理想中的应用导航栏,应该达到什么需求标准?
1.能够切换Fragment
2.点击切换图片
3.附带动画效果
4.滑动切换
5.右上角带消息红点
理想归理想,还得看需求实现,对于底部按钮动画用MotionLayout
+ImagFilterView
简单便捷又能取得不错的效果。
MotionLayout
作为ConstraintLayout
的子类,AS4.0可以直接将ConstraintLayout
转为MotionLayout
,并且作为导航栏的一个子项。而每一个MotionLayout
里面有不同内容的ImagFilterView
和TextView
。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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="?attr/selectableItemBackgroundBorderless"
app:layoutDescription="@xml/motionlayout_scene">
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="@+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="center"
android:scaleX="0.5"
android:scaleY="0.5"
app:altSrc="@drawable/ic_resell_fill_24"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_resell_outline_24" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment1"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.motion.widget.MotionLayout>
正如绝大多数复杂控件一样,MotionLayout
也需要配置一个XML配置文件,并且在app:layoutDescription
属性中引用,另外,以下属性可以为MotionLayout
增加点触水波特效:
android:background="?attr/selectableItemBackgroundBorderless"
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="300">
<KeyFrameSet>
<KeyCycle
motion:wavePeriod="1"
android:translationX="10dp"
motion:waveOffset="0dp" />
<KeyCycle
motion:wavePeriod="1"
android:translationX="10dp"
motion:waveOffset="0dp" />
<KeyCycle
motion:motionTarget="@+id/imageView"
motion:wavePeriod="1"
android:translationX="5dp"
motion:framePosition="1"
motion:waveOffset="0dp" /