属性动画(Property Animation)资源

Animator 代表一个属性动画,但它只是一个抽象类,通常会使用它的子类:AnimatorSet、ValueAnimator、ObjectAnimator、TimeAnimator。
定义属性动画的 XML 资源文件能以如下三个元素中的任意一个作为根元素。
  • <set.../>:它是一个父元素,用于包含其他<objectAnimator.../>、<animator.../>或 <set.../>子元素,该元素定义的资源代表 AnimatorSet 对象。
  • <objectAnimator.../>:用于定义 ObjectAnimator 动画。 
  • <animator.../>:用于定义 ValueAnimator 动画。
实例:不断渐变的背景色
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    >
</LinearLayout>
color_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="backgroundColor"
    android:duration="3000"
    android:valueFrom="#FF8080"
    android:valueTo="#8080FF"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:valueType="intType">
</objectAnimator>
AnimatorTest.java
package org.crazyit.res;

import android.animation.AnimatorInflater;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class AnimatorTest extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout container = (LinearLayout)
            findViewById(R.id.container);
        // 添加MyAnimationView组件
        container.addView(new MyAnimationView(this));
    }

    public class MyAnimationView extends View
    {
        public MyAnimationView(Context context)
        {
            super(context);
            // 加载动画资源
            ObjectAnimator colorAnim = (ObjectAnimator) AnimatorInflater
                .loadAnimator(AnimatorTest.this, R.animator.color_anim);
            colorAnim.setEvaluator(new ArgbEvaluator());
            // 对该View本身应用属性动画
            colorAnim.setTarget(this);
            // 开始指定动画
            colorAnim.start();
        }
    }
}
 
原文地址:https://www.cnblogs.com/AndyGe/p/3436845.html