Android布局动画简单使用
这部分内容其实放在视图动画中讲解比较合适,主要是因为这里面比较关键的一个API LayoutAnimatonController的构建需要的是一个Animation,所以你懂的,更多关于Animation的知识请转至Android动画框架之视图动画基本使用。
所谓布局动画,其实主要还是针对布局中的子View显示来说的,也就是当一个ViewGroup中添加一个子View的时候可以通过布局动画让其更加绚丽的添加。下面就来看看具体的实现。
方式1:
通过android:animateLayoutChanges=“true”实现,其效果是默认的一个逐渐显示的效果
方式2:
通过LayoutAnimationController实现对子View出现顺序以及子View自定义动画显示
因为这部分内容较简单,我就在一个例子中把两种方式都展示出来,例子的思想是:在布局中添加三个按钮一个TextView,给该布局添加点击监听事件,程序运行时为了模拟添加子View,这里先隐藏这四个子View,通过点击布局使用第一种方式显示中心按钮,给按钮添加点击事件,触发第二种方式显示三个按钮和TextView。效果图如下
LayoutAnimActivity.java代码:
package com.hfut.operationanimator; import android.support.constraint.ConstraintLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.AnimationSet; import android.view.animation.LayoutAnimationController; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.widget.Button; import android.widget.TextView; /** * @author why * @date 2018-9-9 22:43:26 */ public class LayoutAnimActivity extends AppCompatActivity { ConstraintLayout layout; Button centerButton; Button button1; Button button2; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout_anim); textView = findViewById(R.id.test_view); centerButton = findViewById(R.id.center_button); button1 = findViewById(R.id.button1); button2 = findViewById(R.id.button2); layout = findViewById(R.id.view_group); textView.setVisibility(View.INVISIBLE); centerButton.setVisibility(View.INVISIBLE); button1.setVisibility(View.INVISIBLE); button2.setVisibility(View.INVISIBLE); layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { centerButton.setVisibility(View.VISIBLE); } }); } public void layoutAnim(View view){ textView.setVisibility(View.VISIBLE); button1.setVisibility(View.VISIBLE); button2.setVisibility(View.VISIBLE); ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1); RotateAnimation rotateAnimation=new RotateAnimation(0,360); rotateAnimation.setDuration(4000); scaleAnimation.setDuration(4000); AnimationSet set=new AnimationSet(true); set.addAnimation(rotateAnimation); set.addAnimation(scaleAnimation); LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); //子View出现的顺序可以为 //LayoutAnimationController.ORDER_NORMAL; //LayoutAnimationController.ORDER_REVERSE //LayoutAnimationController.ORDER_RANDOM controller.setOrder(LayoutAnimationController.ORDER_NORMAL); layout.setLayoutAnimation(controller); } }
activity_layout_anim.xml代码:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true" android:id="@+id/view_group" tools:context="com.hfut.operationanimator.LayoutAnimActivity"> <Button android:textSize="20sp" android:text="中心按钮" android:onClick="layoutAnim" android:id="@+id/center_button" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="按钮1" android:id="@+id/button1" android:layout_marginLeft="80dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/button2" app:layout_constraintBottom_toTopOf="@+id/center_button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="按钮2" android:id="@+id/button2" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="@id/button1" app:layout_constraintBottom_toTopOf="@+id/center_button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/test_view" android:layout_marginTop="20dp" app:layout_constraintTop_toBottomOf="@id/center_button" android:gravity="center_horizontal" android:textColor="@color/colorAccent" android:text="布局动画演示" android:textSize="35dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.constraint.ConstraintLayout>
到这里,关于布局动画简单使用就介绍完了。如果还想了解关于属性动画的使用请转至属性动画之Animator API基本使用。