在Android上实现动画,官方的SDK提供了Animations,并且介绍了两种不同模式,分别是:
1. Tween Animation:通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果,即是一种渐变动画;
2. Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。
同时,Animation由四种类型组成:
XML文件:
· alpha 渐变透明度动画效果
· scale 渐变尺寸伸缩动画效果
· translate 画面转换位置移动动画效果
· rotate 画面转移旋转动画效果
在Java 源码中定义了相应的类,可以使用这些类的方法来获取和操作相应的属性:
· AlphaAnimation 渐变透明度动画效果
· ScaleAnimation 渐变尺寸伸缩动画效果
· TranslateAnimation 画面转换位置移动动画效果
· RotateAnimation 画面转移旋转动画效果
具体Android的原生就不再多说了,相对复杂,有兴趣的可以直接去看google的SDK。这里分享了Rexsee的API,基于对原生的封装,可以直接使用JS实现功能调用。如:
【事件】 void onAnimationStart(String id)
【说明】 当动画开始播放时触发。
在Rexsee社区可以直接查看源码。同时,新上线的项目中心提供了在线开发服务,不需要单独准备服务器。同时也有大量的应用以分享的方式开放供查阅,关于动画的具体应用源码也可以在这里查到:http://www.rexsee.com/project/
Rexsee API:Animations源码
- package rexsee.core.animation;
- import rexsee.core.style.StyleSheet;
- import rexsee.core.utilities.RexseeUtilities;
- import android.view.View;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.AccelerateInterpolator;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.view.animation.AnimationSet;
- import android.view.animation.AnticipateInterpolator;
- import android.view.animation.AnticipateOvershootInterpolator;
- import android.view.animation.BounceInterpolator;
- import android.view.animation.CycleInterpolator;
- import android.view.animation.DecelerateInterpolator;
- import android.view.animation.Interpolator;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.OvershootInterpolator;
- import android.view.animation.RotateAnimation;
- import android.view.animation.ScaleAnimation;
- import android.view.animation.TranslateAnimation;
- public class Animations extends Animation {
- public Animations() {
- super();
- }
- public static Animation getAnimation(StyleSheet style, AnimationListener listener, View view, View viewParent) {
- if (view == null) return null;
- if (viewParent == null) {
- try {
- viewParent = (View) view.getParent();
- } catch (Exception e) {
- viewParent = view;
- }
- }
- AnimationSet animation = new AnimationSet(true);
- String[] types = style.animation_type.split("\\+");
- int length = 0;
- animation.setDuration(Integer.parseInt(style.animation_rotate_duration));
- animation.setRepeatCount(Integer.parseInt(style.animation_rotate_repeat_count));
- animation.setRepeatMode(style.animation_rotate_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);
- animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_start_time, 0));
- animation.setInterpolator(getInterPolator(style.animation_rotate_interpolator));
- animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());
- } catch (Exception e) {
- }
- return animation;
- }
- public static Animation getAlphaAnimation(StyleSheet style, View view, View viewParent) {
- Animation animation = null;
- try {
- float from = Float.parseFloat(style.animation_alpha_from.replaceAll("%", "")) / 100;
- float to = Float.parseFloat(style.animation_alpha_to.replaceAll("%", "")) / 100;
- animation = new AlphaAnimation(from, to);
- animation.setDuration(Integer.parseInt(style.animation_alpha_duration));
- animation.setRepeatCount(Integer.parseInt(style.animation_alpha_repeat_count));
- animation.setRepeatMode(style.animation_alpha_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);
- animation.setStartOffset(RexseeUtilities.getLong(style.animation_alpha_start_time, 0));
- animation.setInterpolator(getInterPolator(style.animation_alpha_interpolator));
- animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());
- } catch (Exception e) {
- }
- return animation;
- }
- public static Animation getScaleAnimation(StyleSheet style, View view, View viewParent) {
- Animation animation = null;
- try {
- float scaleCenterX, scaleCenterY;
- try {
- scaleCenterX = Float.parseFloat(style.animation_scale_center_x.replaceAll("%", ""));
- scaleCenterX = scaleCenterX / 100;
- if (scaleCenterX < 0) scaleCenterX = 0;
- if (scaleCenterX > 1) scaleCenterX = 1;
- } catch (Exception e) {
- scaleCenterX = (float) 0.5;
- }
- try {
- scaleCenterY = Float.parseFloat(style.animation_scale_center_y.replaceAll("%", ""));
- scaleCenterY = scaleCenterY / 100;
- if (scaleCenterY < 0) scaleCenterY = 0;
- if (scaleCenterY > 1) scaleCenterY = 1;
- } catch (Exception e) {
- scaleCenterY = (float) 0.5;
- }
- float fromX = Float.parseFloat(style.animation_scale_x_from);
- float toX = Float.parseFloat(style.animation_scale_x_to);
- float fromY = Float.parseFloat(style.animation_scale_y_from);
- float toY = Float.parseFloat(style.animation_scale_y_to);
- animation = new ScaleAnimation(fromX, toX, fromY, toY, Animation.RELATIVE_TO_PARENT, scaleCenterX, Animation.RELATIVE_TO_PARENT, scaleCenterY);
- animation.setDuration(Integer.parseInt(style.animation_scale_duration));
- animation.setRepeatCount(Integer.parseInt(style.animation_scale_repeat_count));
- animation.setRepeatMode(style.animation_scale_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);
- animation.setStartOffset(RexseeUtilities.getLong(style.animation_scale_start_time, 0));
- animation.setInterpolator(getInterPolator(style.animation_scale_interpolator));
- animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());
- } catch (Exception e) {
- }
- return animation;
- }
- public static Animation getTranslateAnimation(StyleSheet style, View view, View viewParent) {
- Animation animation = null;
- try {
- float fromX = Float.parseFloat(style.animation_translate_x_from);
- float toX = Float.parseFloat(style.animation_translate_x_to);
- float fromY = Float.parseFloat(style.animation_translate_y_from);
- float toY = Float.parseFloat(style.animation_translate_y_to);
- fromX = fromX / 100;
- toX = toX / 100;
- fromY = fromY / 100;
- toY = toY / 100;
- animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, fromX, Animation.RELATIVE_TO_PARENT, toX, Animation.RELATIVE_TO_PARENT, fromY, Animation.RELATIVE_TO_PARENT, toY);
- animation.setDuration(Integer.parseInt(style.animation_translate_duration));
- animation.setRepeatCount(Integer.parseInt(style.animation_translate_repeat_count));
- animation.setRepeatMode(style.animation_translate_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);
- animation.setStartOffset(RexseeUtilities.getLong(style.animation_translate_start_time, 0));
- animation.setInterpolator(getInterPolator(style.animation_translate_interpolator));
- animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());
- } catch (Exception e) {
- }
- return animation;
- }
- public static Animation getRotate3dyAnimation(StyleSheet style, View view, View viewParent) {
- Animation animation = null;
- try {
- float rotate3DCenterX, rotate3DCenterY;
- try {
- rotate3DCenterX = Float.parseFloat(style.animation_rotate_3dy_center_x.replaceAll("%", ""));
- rotate3DCenterX = rotate3DCenterX / 100;
- if (rotate3DCenterX < 0) rotate3DCenterX = 0;
- if (rotate3DCenterX > 1) rotate3DCenterX = 1;
- } catch (Exception e) {
- rotate3DCenterX = (float) 0.5;
- }
- rotate3DCenterX = view.getWidth() * rotate3DCenterX;
- try {
- rotate3DCenterY = Float.parseFloat(style.animation_rotate_3dy_center_y.replaceAll("%", ""));
- rotate3DCenterY = rotate3DCenterY / 100;
- if (rotate3DCenterY < 0) rotate3DCenterY = 0;
- if (rotate3DCenterY > 1) rotate3DCenterY = 1;
- } catch (Exception e) {
- rotate3DCenterY = (float) 0.5;
- }
- rotate3DCenterY = view.getHeight() * rotate3DCenterY;
- float from = RexseeUtilities.getFloat(style.animation_rotate_3dy_from, 0f);
- float to = RexseeUtilities.getFloat(style.animation_rotate_3dy_to, 90f);
- float rotate3DDepthZ = RexseeUtilities.getFloat(style.animation_rotate_3dy_depth_z, 310.0f);
- boolean reverse = (style.animation_rotate_3dy_reverse.equalsIgnoreCase("true")) ? true : false;
- animation = new Rotate3dyAnimation(from, to, rotate3DCenterX, rotate3DCenterY, rotate3DDepthZ, reverse);
- animation.setDuration(RexseeUtilities.getInt(style.animation_rotate_3dy_duration, 1000));
- animation.setRepeatCount(RexseeUtilities.getInt(style.animation_rotate_3dy_repeat_count, 0));
- animation.setRepeatMode(style.animation_rotate_3dy_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);
- animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_3dy_start_time, 0));
- animation.setInterpolator(getInterPolator(style.animation_rotate_3dy_interpolator));
- animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());
- } catch (Exception e) {
- }
- return animation;
- }
- public static Animation getRotate3dxAnimation(StyleSheet style, View view, View viewParent) {
- Animation animation = null;
- try {
- float rotate3DCenterX, rotate3DCenterY;
- try {
- rotate3DCenterX = Float.parseFloat(style.animation_rotate_3dx_center_x.replaceAll("%", ""));
- rotate3DCenterX = rotate3DCenterX / 100;
- if (rotate3DCenterX < 0) rotate3DCenterX = 0;
- if (rotate3DCenterX > 1) rotate3DCenterX = 1;
- } catch (Exception e) {
- rotate3DCenterX = (float) 0.5;
- }
- rotate3DCenterX = view.getWidth() * rotate3DCenterX;
- try {
- rotate3DCenterY = Float.parseFloat(style.animation_rotate_3dx_center_y.replaceAll("%", ""));
- rotate3DCenterY = rotate3DCenterY / 100;
- if (rotate3DCenterY < 0) rotate3DCenterY = 0;
- if (rotate3DCenterY > 1) rotate3DCenterY = 1;
- } catch (Exception e) {
- rotate3DCenterY = (float) 0.5;
- }
- rotate3DCenterY = view.getHeight() * rotate3DCenterY;
- float from = RexseeUtilities.getFloat(style.animation_rotate_3dx_from, 0f);
- float to = RexseeUtilities.getFloat(style.animation_rotate_3dx_to, 90f);
- float rotate3DDepthZ = RexseeUtilities.getFloat(style.animation_rotate_3dx_depth_z, 310.0f);
- boolean reverse = (style.animation_rotate_3dx_reverse.equalsIgnoreCase("true")) ? true : false;
- animation = new Rotate3dxAnimation(from, to, rotate3DCenterX, rotate3DCenterY, rotate3DDepthZ, reverse);
- animation.setDuration(RexseeUtilities.getInt(style.animation_rotate_3dx_duration, 1000));
- animation.setRepeatCount(RexseeUtilities.getInt(style.animation_rotate_3dx_repeat_count, 0));
- animation.setRepeatMode(style.animation_rotate_3dx_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);
- animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_3dx_start_time, 0));
- animation.setInterpolator(getInterPolator(style.animation_rotate_3dx_interpolator));
- animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());
- } catch (Exception e) {
- }
- return animation;
- }
- public static Animation getSkewAnimation(StyleSheet style, View view, View viewParent) {
- Animation animation = null;
- try {
- float skewCenterX, skewCenterY;
- try {
- skewCenterX = Float.parseFloat(style.animation_skew_center_x.replaceAll("%", ""));
- skewCenterX = skewCenterX / 100;
- if (skewCenterX < 0) skewCenterX = 0;
- if (skewCenterX > 1) skewCenterX = 1;
- } catch (Exception e) {
- skewCenterX = (float) 0.5;
- }
- skewCenterX = view.getWidth() * skewCenterX;
- try {
- skewCenterY = Float.parseFloat(style.animation_skew_center_y.replaceAll("%", ""));
- skewCenterY = skewCenterY / 100;
- if (skewCenterY < 0) skewCenterY = 0;
- if (skewCenterY > 1) skewCenterY = 1;
- } catch (Exception e) {
- skewCenterY = (float) 0.5;
- }
- skewCenterY = view.getHeight() * skewCenterY;
- float fromX = RexseeUtilities.getFloat(style.animation_skew_x_from, 0f);
- float toX = RexseeUtilities.getFloat(style.animation_skew_x_to, 1f);
- float fromY = RexseeUtilities.getFloat(style.animation_skew_y_from, 0f);
- float toY = RexseeUtilities.getFloat(style.animation_skew_y_to, 1f);
- float skewDepthZ = RexseeUtilities.getFloat(style.animation_skew_depth_z, 310.0f);
- boolean reverse = (style.animation_skew_reverse.equalsIgnoreCase("true")) ? true : false;
- animation = new SkewAnimation(fromX, toX, fromY, toY, skewCenterX, skewCenterY, skewDepthZ, reverse);
- animation.setDuration(RexseeUtilities.getInt(style.animation_skew_duration, 1000));
- animation.setRepeatCount(RexseeUtilities.getInt(style.animation_skew_repeat_count, 0));
- animation.setRepeatMode(style.animation_skew_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);
- animation.setStartOffset(RexseeUtilities.getLong(style.animation_skew_start_time, 0));
- animation.setInterpolator(getInterPolator(style.animation_skew_interpolator));
- animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());
- } catch (Exception e) {
- }
- return animation;
- }
- public static Interpolator getInterPolator(String name) {
- if (name.equalsIgnoreCase("AccelerateDecelerate")) {
- return new AccelerateDecelerateInterpolator();
- } else if (name.equalsIgnoreCase("Accelerate")) {
- return new AccelerateInterpolator(10.0f);
- } else if (name.equalsIgnoreCase("Decelerate")) {
- return new DecelerateInterpolator(10.0f);
- } else if (name.equalsIgnoreCase("Anticipate")) {
- return new AnticipateInterpolator(1.0f);
- } else if (name.equalsIgnoreCase("AnticipateOvershoot")) {
- return new AnticipateOvershootInterpolator(1.0f, 1.5f);
- } else if (name.equalsIgnoreCase("Overshoot")) {
- return new OvershootInterpolator(1.0f);
- } else if (name.equalsIgnoreCase("Bounce")) {
- return new BounceInterpolator();
- } else if (name.equalsIgnoreCase("Cycle")) {
- return new CycleInterpolator(1);
- } else if (name.equalsIgnoreCase("Linear")) {
- return new LinearInterpolator();
- } else {
- return new LinearInterpolator();
- }
- }
- }