Sta*_*wer 8 animation android android-animation android-layout android-view
我需要用视图做两件事:
简而言之,我需要视图覆盖100%的父视图.
翻译动画不起作用,因为它移动视图但不会增加大小.
缩放动画有效,但它会拉伸视图的内容,我不希望这样.我想增加可见区域,而不是拉伸内容以适应新的尺寸.
这样做的正确方法是什么?
azi*_*ian 11
使用Transitions API可以轻松实现这一点.
使用Transitions API,您不需要编写动画,只需告诉您想要的最终值,Transitions API将负责构建动画.
将此xml作为内容视图(屏幕中心的视图):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@color/colorAccent" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在活动中:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.item)
val root = findViewById(R.id.root) as ViewGroup
val view = findViewById(R.id.view)
view.setOnClickListener {
// After this line Transitions API would start counting the delta
// and will take care of creating animations for each view in `root`
TransitionManager.beginDelayedTransition(root)
// By default AutoTransition would be applied,
// but you can provide your transition with the second parameter
// val transition = AutoTransition()
// transition.duration = 2000
// TransitionManager.beginDelayedTransition(root, transition)
// We are changing size of the view to match parent
val params = view.layoutParams
params.height = ViewGroup.LayoutParams.MATCH_PARENT
params.width = ViewGroup.LayoutParams.MATCH_PARENT
view.requestLayout()
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:

Platform的Transitions API(android.transition.TransitionManager)可从API 19获得,但支持库将功能向后移植到API 14(android.support.transition.TransitionManager).
| 归档时间: |
|
| 查看次数: |
1290 次 |
| 最近记录: |