LinearLayout 以编程方式更改 android:layoutAnimation

LinearLayout change android:layoutAnimation programmatically

是否有任何方法可以使用 Java 以编程方式将 android:layoutAnimation 更改为不同的动画资源?

Is there any way to change android:layoutAnimation to a different animation resource programmatically using Java?

: 你可以使用 LayoutAnimationController

  • 布局动画控制器用于为 layout'sview 组 children 设置动画。每个 child 使用相同的动画,但对于它们中的每一个,动画开始的时间不同。 ViewGroup 使用 layout animation controller 来计算每个 child 的动画开始必须偏移的延迟

这是工作示例代码

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>