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

本文介绍了Android AnimationSet类,它是动画集合类,用于组合多个动画效果。详细讲解了AnimationSet如何通过addAnimation方法添加动画对象,以及如何通过设置属性控制动画行为。还提供了实例,演示如何在按钮点击事件中应用AnimationSet,实现透明和位移动画,并提到可以通过AnimationUtils加载动画资源,以及使用startAnimation启动动画。
摘要由CSDN通过智能技术生成

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和属性。


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) {
    }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值