anim 补间动画、共享动画

补间动画

Fragment之间的切换动画

视图动画

res -> New -> Android Resource Directory(anim)
anim -> New -> Android Resource File

Root element:
补间动画:
translate 平移
scale 缩放
alpha 渐变
rotate 旋转
set 可以放很多动画

translate 平移

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%"
android:toXDelta="0%"
android:duration = "500" />

scale 缩放
pivotX:如果加个p 50%p 就是相对于父容器(默认是相对于自己的)

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"

android:fromXScale="0.1"
android:toXScale="1.0"
android:fromYScale="0.1"
android:toYScale="1.0"
android:duration = "500"
/>

alpha 透明度
系统提供了淡入淡出动画
android.R.anim.fade_in
android.R.anim.fade_out

1
2
3
4
5
6

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="500"/>

rotate 旋转
0 0

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0.0"
android:toDegrees="360.0"
/>

set 可多个动画同时进行

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">



</set>

界面之间切换动画

Activity:
overridePendingTransition(R.anim.enter from rightR.anim.exit to left)

Fragment:
setCustomAnimations(enter1, exit1, popEnter1, popExit1)

1
2
3
4
5
6
7
//切换Fragment时的动画
supportFragmentManager.commit {
setCustomAnimations(enter1, exit1, popEnter1, popExit1)//添加动画 顺序很重要
replace(R.id.fragmentContainerView,HomeFragment)
setReorderingAllowed(true)//允许在事务过程中重新排序Fragment,而不会影响其在视图层次结构中的位置
addToBackStack("name") // Name can be null
}

共享动画

使用共享元素转换
(切换顺滑)
(先确定哪两个控件之间需要共享动画)
1.给两个控件添加transitionName

1
android:transitionName="little"

2.切换动画时设置A Fragment中哪个控件和 B Fragment中的哪个控件共享动画
addSharedElement(view1,”big”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_first, container, false)
val view1 = view.findViewById<ImageView>(R.id.view1)

view.setOnClickListener {
parentFragmentManager.commit {
setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out)
replace(R.id.fragmentContainerView,SecondFragment())
setReorderingAllowed(true)
addSharedElement(view1,"big")
addToBackStack(null)
}
}
return view
}

3.到目的地 Fragment 的 onCreate 中配置动画效果

1
2
3
4
5
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = TransitionInflater.from(requireContext()).inflateTransition(android.R.transition.move)
//(在fragment中使用activity获取当前这个fragment依附的activity对象)
}

自行配置转换动画效果
res -> New -> Android Resource Directory(transition)
transition -> New -> Android Resource File
Root element:
transitionSet
transitionManager

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023-2025 Annie
  • Visitors: | Views:

嘿嘿 请我吃小蛋糕吧~

支付宝
微信