【发布时间】:2018-03-30 06:31:20
【问题描述】:
有没有办法使用 Java 以编程方式将 android:layoutAnimation 更改为不同的动画资源?
【问题讨论】:
标签: android android-animation android-linearlayout
有没有办法使用 Java 以编程方式将 android:layoutAnimation 更改为不同的动画资源?
【问题讨论】:
标签: android android-animation android-linearlayout
有没有办法使用 Java 以编程方式将
android:layoutAnimation更改为不同的动画资源?
是的:你可以使用LayoutAnimationController
布局动画控制器用于为layout's 或view 组的子级设置动画。每个孩子都使用相同的动画,但对于每个孩子,动画开始的时间不同。 ViewGroup 使用 layout animation controller 来计算每个孩子的动画开始必须偏移的延迟
这里是工作示例代码
public class MainActivity extends AppCompatActivity {
LinearLayout rootView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootView=findViewById(R.id.rootView);
LayoutAnimationController anim = AnimationUtils.loadLayoutAnimation(this, R.anim.scale_down);
rootView.setLayoutAnimation(anim);
}
}
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:id="@+id/buttonPanel"
android:layout_height="wrap_content" />
</LinearLayout>
R.anim.scale_down
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/scale_up" />
@anim/scale_up
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
【讨论】: