LinearLayout以编程方式更改android:layoutAnimation

时间:2018-03-30 06:31:20

标签: android android-animation android-linearlayout

有没有办法使用Java以编程方式将android:layoutAnimation更改为其他动画资源?

1 个答案:

答案 0 :(得分:5)

  

有没有办法使用Java以编程方式将android:layoutAnimation更改为其他动画资源?

:您可以使用LayoutAnimationController

  • 布局动画控制器用于为layout'sview组的儿童设置动画。每个孩子都使用相同的动画,但对于每个孩子,动画都会在不同的时间开始。 layout animation controller使用ViewGroup来计算每个孩子的动画开始必须偏移的延迟

这是工作示例代码

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" />

<强> @动画/ 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>
相关问题