Android学习之界面篇(二)Android AnimationSet简单使用

AnimationSet类是Android系统中的动画集合类,用于控制View对象进行多个动作的组合,该类继承于Animation类。AnimationSet类中的很多方法都与Animation类一致,该类中最常用的方法便是addAnimation方法,该方法用于为动画集合对象添加动画对象,可以为对象添加多个动画效果。

贴上android官方定义:

Class Overview

Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.

The way that AnimationSet inherits behavior from Animation is important to understand. Some of the Animation attributes applied to AnimationSet affect the AnimationSet itself, some are pushed down to the children, and some are ignored, as follows:

  • duration, repeatMode, fillBefore, fillAfter: These properties, when set on an AnimationSet object, will be pushed down to all child animations.
  • repeatCount, fillEnabled: These properties are ignored for AnimationSet.
  • startOffset, shareInterpolator: These properties apply to the AnimationSet itself.

Starting with ICE_CREAM_SANDWICH, the behavior of these properties is the same in XML resources and at runtime (prior to that release, the values set in XML were ignored for AnimationSet). That is, calling setDuration(500) on an AnimationSet has the same effect as declaringandroid:duration="500" in an XML resource for an AnimationSet object.

下面来实例演示以下AnimationSet的具体用法:

要求:点击界面上的按钮,按钮出现透明和转移的动画效果。

1.首先在界面上添加一个按钮,并配置其id和属性。

Android学习之界面篇(二)Android AnimationSet简单使用_第1张图片

2.在res/anim/目录下添加一个animation资源文件,具体配置如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="1000"/>
    <translate
        android:fromXDelta="200"
        android:toXDelta="0"
        android:fromYDelta="200"
        android:toYDelta="0"
        android:duration="1000"
        />

</set>

3.在程序中实现按钮的点击事件之后利用AnimationUtils的loadAnimation方法来加载动画效果。

Animation a=AnimationUtils.loadAnimation(MainActivity.this, R.anim.animate);

4.使用startAnimation方法来为视图启动动画效果。

v.startAnimation(a);

animation对象同时还可以实现对animation事件的监听。

a.setAnimationListener(new Animation.AnimationListener() {
    @Override
    /**
     *在动画开始时执行
     */
    public void onAnimationStart(Animation animation) {

    }

    @Override
    /**
     *在动画结束是执行
     */
    public void onAnimationEnd(Animation animation) {
        Toast.makeText(MainActivity.this,"Animation End",Toast.LENGTH_SHORT).show();

    }

    @Override
    /**
     *在动画重绘时执行
     */
    public void onAnimationRepeat(Animation animation) {
    }
});

你可能感兴趣的:(android,animation,animationSet)