android 动画详解(一)

其实前面有一篇关于动画的详细介绍了,写这一篇博客就是为了更细致的介绍动画,多贴出来一些代码,更直观探讨一下动画的使用……

一、Animations介绍

Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。

二、Animations的分类

Animations从总体上可以分为三大类:
1 . Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。
2 . Frame-by-frame Animations:这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。
3 . Property Animation : 故名思议就是通过动画的方式改变对象的属性了.
属性动画就是,动画的执行类来设置动画操作的对象的属性、持续时间,开始和结束的属性值,时间差值等,然后系统会根据设置的参数动态的变化对象的属性。

三、Animations的使用方法(代码中使用)

Animations extends Object implements Cloneable

使用TweenedAnimations的步骤:
  1.创建一个AnimationSet对象(Animation子类);
  2.增加需要创建相应的Animation对象;
  3.更加项目的需求,为Animation对象设置相应的数据;
  4.将Animatin对象添加到AnimationSet对象当中;
  5.使用控件对象开始执行AnimationSet

Tweened Animations的分类
  1、Alpha:淡入淡出效果
  2、Scale:缩放效果
  3、Rotate:旋转效果
  4、Translate:移动效果

Animation的四个子类:

  AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation

四、代码具体使用

Tween Animations的通用方法
  1、setDuration(long durationMills)
     设置动画持续时间(单位:毫秒)
  2、setFillAfter(Boolean fillAfter)
     如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
  3、setFillBefore(Boolean fillBefore)
    如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
  4、setStartOffSet(long startOffSet)
    设置动画执行之前的等待时间
  5、setRepeatCount(int repeatCount)
    设置动画重复执行的次数

(一)代码中使用Animation
  • 渐变动画
 /**
     * 渐变动画
     */
    public static void alphaAction(View view) {
        //创建一个AnimationSet对象,参数为Boolean型,
        //true表示使用Animation的interpolator,false则是使用自己的
        AnimationSet animationSet = new AnimationSet(true);
        //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        //设置动画执行的时间
        alphaAnimation.setDuration(500);
        //将alphaAnimation对象添加到AnimationSet当中
        animationSet.addAnimation(alphaAnimation);
        //使用ImageView的startAnimation方法执行动画
        view.startAnimation(animationSet);
    }
  • 旋转动画
/**
     * 旋转动画
     */
    public static void rotateAction(View view) {
        AnimationSet animationSet = new AnimationSet(true);
        //参数1:从哪个旋转角度开始
        //参数2:转到什么角度
        //后4个参数用于设置围绕着旋转的圆的圆心在哪里
        //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
        //参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
        //参数5:确定y轴坐标的类型
        //参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        animationSet.addAnimation(rotateAnimation);
        view.startAnimation(animationSet);
    }
  • 缩放动画
 /**
     * 缩放动画
     */
    public static void scaleAction(View view) {

        //参数1:x轴的初始值
        //参数2:x轴收缩后的值
        //参数3:y轴的初始值
        //参数4:y轴收缩后的值
        //参数5:确定x轴坐标的类型
        //参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
        //参数7:确定y轴坐标的类型
        //参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                0, 0.1f, 0, 0.1f,
                Animation.RELATIVE_TO_SELF, 1,
                Animation.RELATIVE_TO_SELF, 1);
        scaleAnimation.setDuration(1000);
        animationSet.addAnimation(scaleAnimation);
        view.startAnimation(animationSet);
    }
  • 位移动画
 /**
     * 位移动画
     */

    public static void translateAction(View view) {
        AnimationSet animationSet = new AnimationSet(true);
        //参数1~2:x轴的开始位置
        //参数3~4:y轴的开始位置
        //参数5~6:x轴的结束位置
        //参数7~8:x轴的结束位置
        TranslateAnimation translateAnimation =
                new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF, 0f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
        translateAnimation.setDuration(1000);
        animationSet.addAnimation(translateAnimation);
        view.startAnimation(animationSet);
    }

下面贴出整理后的整个动画工具类


import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

/**
 * Created by 大军 on 2016/5/3
 */
public class AnimationTools {


    /**
     * Tween Animations的通用方法
     *   1、setDuration(long durationMills)
     *   设置动画持续时间(单位:毫秒)
     *   2、setFillAfter(Boolean fillAfter)
     *   如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
     * 3、setFillBefore(Boolean fillBefore)
     *   如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
     *   4、setStartOffSet(long startOffSet)
     *   设置动画执行之前的等待时间
     *   5、setRepeatCount(int repeatCount)
     *   设置动画重复执行的次数
     */


//    private static AnimationTools animationTools = null;
//    private AnimationTools() {
//    }
//
//    public static AnimationTools instance() {
//
//        if (animationTools == null) {
//            synchronized (animationTools) {
//                if (animationTools == null) {
//                    animationTools = new AnimationTools();
//                }
//            }
//        }
//        return animationTools;
//    }

    /**
     * 渐变动画
     */
    public static void alphaAction(View view) {
        //创建一个AnimationSet对象,参数为Boolean型,
        //true表示使用Animation的interpolator,false则是使用自己的
        AnimationSet animationSet = new AnimationSet(true);
        //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        //设置动画执行的时间
        alphaAnimation.setDuration(500);
        //将alphaAnimation对象添加到AnimationSet当中
        animationSet.addAnimation(alphaAnimation);
        //使用ImageView的startAnimation方法执行动画
        view.startAnimation(animationSet);
    }

    public static void alphaAction(AnimationSet animationSet) {
        //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        //设置动画执行的时间
        alphaAnimation.setDuration(500);
        //将alphaAnimation对象添加到AnimationSet当中
        animationSet.addAnimation(alphaAnimation);
        //使用ImageView的startAnimation方法执行动画
    }


    /**
     * 旋转动画
     */
    public static void rotateAction(View view) {
        AnimationSet animationSet = new AnimationSet(true);
        //参数1:从哪个旋转角度开始
        //参数2:转到什么角度
        //后4个参数用于设置围绕着旋转的圆的圆心在哪里
        //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
        //参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
        //参数5:确定y轴坐标的类型
        //参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        animationSet.addAnimation(rotateAnimation);
        view.startAnimation(animationSet);
    }

    public static void rotateAction(AnimationSet animationSet) {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        animationSet.addAnimation(rotateAnimation);
    }

    /**
     * 缩放动画
     */
    public static void scaleAction(View view) {

        //参数1:x轴的初始值
        //参数2:x轴收缩后的值
        //参数3:y轴的初始值
        //参数4:y轴收缩后的值
        //参数5:确定x轴坐标的类型
        //参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
        //参数7:确定y轴坐标的类型
        //参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                0, 0.1f, 0, 0.1f,
                Animation.RELATIVE_TO_SELF, 1,
                Animation.RELATIVE_TO_SELF, 1);
        scaleAnimation.setDuration(1000);
        animationSet.addAnimation(scaleAnimation);
        view.startAnimation(animationSet);
    }

    public static void scaleAction(AnimationSet animationSet) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                0, 0.1f, 0, 0.1f,
                Animation.RELATIVE_TO_SELF, 1,
                Animation.RELATIVE_TO_SELF, 1);
        scaleAnimation.setDuration(1000);
        animationSet.addAnimation(scaleAnimation);
    }


    /**
     * 位移动画
     */

    public static void translateAction(View view) {
        AnimationSet animationSet = new AnimationSet(true);
        //参数1~2:x轴的开始位置
        //参数3~4:y轴的开始位置
        //参数5~6:x轴的结束位置
        //参数7~8:x轴的结束位置
        TranslateAnimation translateAnimation =
                new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF, 0f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
        translateAnimation.setDuration(1000);
        animationSet.addAnimation(translateAnimation);
        view.startAnimation(animationSet);
    }


    public static void translateAction(AnimationSet animationSet) {
        TranslateAnimation translateAnimation =
                new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF, 0f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
        translateAnimation.setDuration(1000);
        animationSet.addAnimation(translateAnimation);
    }

    /**
     * 属性动画
     */

    public void propertyValuesHolder(View view) {
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
                0f, 1f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
                0, 1f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
                0, 1f);
        ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(1000).start();
    }
}
(二)在xml中使用Animations

1.在res文件夹下建立一个anim文件夹;
2.创建xml文件,并首先加入set标签,更改标签如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
</set>

3.在该标签当中加入rotate,alpha,scale或者translate标签;

<alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="500"/>

4.在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象。因为Animation是AnimationSet的子类,所以向上转型,用Animation对象接收。

Animation animation = AnimationUtils.loadAnimation(
                  Animation1Activity.this, R.anim.alpha);
           // 启动动画
           image.startAnimation(animation);

贴出具体代码
1、 alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
      <!-- fromAlpha和toAlpha是起始透明度和结束时透明度 -->
    <alpha
    android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="500"/>
</set>

2、 rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!--
        fromDegrees:开始的角度
        toDegrees:结束的角度,+表示是正的
        pivotX:用于设置旋转时的x轴坐标
        例
           1)当值为"50",表示使用绝对位置定位
           2)当值为"50%",表示使用相对于控件本身定位
           3)当值为"50%p",表示使用相对于控件的父控件定位
        pivotY:用于设置旋转时的y轴坐标
          -->
    <rotate
        android:fromDegrees="0"
        android:toDegrees="+360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"/>
</set>

3、 scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
   <!--
           起始x轴坐标
           止x轴坐标
           始y轴坐标
           止y轴坐标
           轴的坐标
           轴的坐标
     -->
     <scale
       android:fromXScale="1.0"
       android:toXScale="0.0"
       android:fromYScale="1.0"
       android:toYScale="0.0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="1000"/>
</set>

4、 translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!--
           始x轴坐标
           止x轴坐标
           始y轴坐标
           止y轴坐标
      -->
      <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="2000"/>
</set>

5、 .java文件

importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
import android.view.animation.Animation;
importandroid.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class Animation1Activity extends Activity {
    private Button rotateButton = null;
    private Button scaleButton = null;
    private Button alphaButton = null;
    private Button translateButton = null;
    private ImageView image = null;
 @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       rotateButton = (Button) findViewById(R.id.rotateButton);
       scaleButton = (Button) findViewById(R.id.scaleButton);
       alphaButton = (Button) findViewById(R.id.alphaButton);
       translateButton = (Button) findViewById(R.id.translateButton);
       image = (ImageView) findViewById(R.id.image);

       rotateButton.setOnClickListener(newRotateButtonListener());
       scaleButton.setOnClickListener(newScaleButtonListener());
       alphaButton.setOnClickListener(newAlphaButtonListener());
       translateButton.setOnClickListener(newTranslateButtonListener());                 
    }

 class AlphaButtonListener implementsOnClickListener {
       public void onClick(View v) {
           // 使用AnimationUtils装载动画配置文件
           Animation animation = AnimationUtils.loadAnimation(
           Animation1Activity.this, R.anim.alpha);
           // 启动动画
           image.startAnimation(animation);
       }
    }
  class RotateButtonListener implementsOnClickListener {
       public void onClick(View v) {
           Animation animation = AnimationUtils.loadAnimation(
           Animation1Activity.this, R.anim.rotate);
           image.startAnimation(animation);
       }
    }
 class ScaleButtonListener implementsOnClickListener {
       public void onClick(View v) {
           Animation animation = AnimationUtils.loadAnimation(
           Animation1Activity.this, R.anim.scale);
           image.startAnimation(animation);
       }
    }
class TranslateButtonListener implementsOnClickListener {
       public void onClick(View v) {
           Animation animation =AnimationUtils.loadAnimation(
           Animation1Activity.this, R.anim.translate);
           image.startAnimation(animation);
       }
    }
}

后面会接着解析常规动画的一些用法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值