现在的位置: 首页 > 综合 > 正文

Rexsee API介绍:Animations动画学习笔记及源码

2018年02月10日 ⁄ 综合 ⁄ 共 16038字 ⁄ 字号 评论关闭

 在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源码

  1. package rexsee.core.animation;  
  2. import rexsee.core.style.StyleSheet;  
  3. import rexsee.core.utilities.RexseeUtilities;  
  4. import android.view.View;  
  5. import android.view.animation.AccelerateDecelerateInterpolator;  
  6. import android.view.animation.AccelerateInterpolator;  
  7. import android.view.animation.AlphaAnimation;  
  8. import android.view.animation.Animation;  
  9. import android.view.animation.AnimationSet;  
  10. import android.view.animation.AnticipateInterpolator;  
  11. import android.view.animation.AnticipateOvershootInterpolator;  
  12. import android.view.animation.BounceInterpolator;  
  13. import android.view.animation.CycleInterpolator;  
  14. import android.view.animation.DecelerateInterpolator;  
  15. import android.view.animation.Interpolator;  
  16. import android.view.animation.LinearInterpolator;  
  17. import android.view.animation.OvershootInterpolator;  
  18. import android.view.animation.RotateAnimation;  
  19. import android.view.animation.ScaleAnimation;  
  20. import android.view.animation.TranslateAnimation;  
  21. public class Animations extends Animation {  
  22.        public Animations() {  
  23.                super();  
  24.        }  
  25.        public static Animation getAnimation(StyleSheet style, AnimationListener listener, View view, View viewParent) {  
  26.                if (view == null) return null;  
  27.                if (viewParent == null) {  
  28.                        try {  
  29.                                viewParent = (View) view.getParent();  
  30.                        } catch (Exception e) {  
  31.                                viewParent = view;  
  32.                        }  
  33.                }  
  34.                AnimationSet animation = new AnimationSet(true);  
  35.                String[] types = style.animation_type.split("\\+");  
  36.                int length = 0;  
  37.                                animation.setDuration(Integer.parseInt(style.animation_rotate_duration));  
  38.                        animation.setRepeatCount(Integer.parseInt(style.animation_rotate_repeat_count));  
  39.                        animation.setRepeatMode(style.animation_rotate_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);  
  40.                        animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_start_time, 0));  
  41.                        animation.setInterpolator(getInterPolator(style.animation_rotate_interpolator));  
  42.                        animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());  
  43.                } catch (Exception e) {  
  44.                }  
  45.                return animation;  
  46.        }  
  47.        public static Animation getAlphaAnimation(StyleSheet style, View view, View viewParent) {  
  48.                Animation animation = null;  
  49.                try {  
  50.                        float from = Float.parseFloat(style.animation_alpha_from.replaceAll("%", "")) / 100;  
  51.                        float to = Float.parseFloat(style.animation_alpha_to.replaceAll("%", "")) / 100;  
  52.                        animation = new AlphaAnimation(from, to);  
  53.                        animation.setDuration(Integer.parseInt(style.animation_alpha_duration));  
  54.                        animation.setRepeatCount(Integer.parseInt(style.animation_alpha_repeat_count));  
  55.                        animation.setRepeatMode(style.animation_alpha_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);  
  56.                        animation.setStartOffset(RexseeUtilities.getLong(style.animation_alpha_start_time, 0));  
  57.                        animation.setInterpolator(getInterPolator(style.animation_alpha_interpolator));  
  58.                        animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());  
  59.                } catch (Exception e) {  
  60.                }  
  61.                return animation;  
  62.        }  
  63.        public static Animation getScaleAnimation(StyleSheet style, View view, View viewParent) {  
  64.                Animation animation = null;  
  65.                try {  
  66.                        float scaleCenterX, scaleCenterY;  
  67.                        try {  
  68.                                scaleCenterX = Float.parseFloat(style.animation_scale_center_x.replaceAll("%", ""));  
  69.                                scaleCenterX = scaleCenterX / 100;  
  70.                                if (scaleCenterX < 0) scaleCenterX = 0;  
  71.                                if (scaleCenterX > 1) scaleCenterX = 1;  
  72.                        } catch (Exception e) {  
  73.                                scaleCenterX = (float) 0.5;  
  74.                        }  
  75.                        try {  
  76.                                scaleCenterY = Float.parseFloat(style.animation_scale_center_y.replaceAll("%", ""));  
  77.                                scaleCenterY = scaleCenterY / 100;  
  78.                                if (scaleCenterY < 0) scaleCenterY = 0;  
  79.                                if (scaleCenterY > 1) scaleCenterY = 1;  
  80.                        } catch (Exception e) {  
  81.                                scaleCenterY = (float) 0.5;  
  82.                        }  
  83.                        float fromX = Float.parseFloat(style.animation_scale_x_from);  
  84.                        float toX = Float.parseFloat(style.animation_scale_x_to);  
  85.                        float fromY = Float.parseFloat(style.animation_scale_y_from);  
  86.                        float toY = Float.parseFloat(style.animation_scale_y_to);  
  87.                        animation = new ScaleAnimation(fromX, toX, fromY, toY, Animation.RELATIVE_TO_PARENT, scaleCenterX, Animation.RELATIVE_TO_PARENT, scaleCenterY);  
  88.                        animation.setDuration(Integer.parseInt(style.animation_scale_duration));  
  89.                        animation.setRepeatCount(Integer.parseInt(style.animation_scale_repeat_count));  
  90.                        animation.setRepeatMode(style.animation_scale_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);  
  91.                        animation.setStartOffset(RexseeUtilities.getLong(style.animation_scale_start_time, 0));  
  92.                        animation.setInterpolator(getInterPolator(style.animation_scale_interpolator));  
  93.                        animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());  
  94.                } catch (Exception e) {  
  95.                }  
  96.                return animation;  
  97.        }  
  98.        public static Animation getTranslateAnimation(StyleSheet style, View view, View viewParent) {  
  99.                Animation animation = null;  
  100.                try {  
  101.                        float fromX = Float.parseFloat(style.animation_translate_x_from);  
  102.                        float toX = Float.parseFloat(style.animation_translate_x_to);  
  103.                        float fromY = Float.parseFloat(style.animation_translate_y_from);  
  104.                        float toY = Float.parseFloat(style.animation_translate_y_to);  
  105.                        fromX = fromX / 100;  
  106.                        toX = toX / 100;  
  107.                        fromY = fromY / 100;  
  108.                        toY = toY / 100;  
  109.                        animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, fromX, Animation.RELATIVE_TO_PARENT, toX, Animation.RELATIVE_TO_PARENT, fromY, Animation.RELATIVE_TO_PARENT, toY);  
  110.                        animation.setDuration(Integer.parseInt(style.animation_translate_duration));  
  111.                        animation.setRepeatCount(Integer.parseInt(style.animation_translate_repeat_count));  
  112.                        animation.setRepeatMode(style.animation_translate_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);  
  113.                        animation.setStartOffset(RexseeUtilities.getLong(style.animation_translate_start_time, 0));  
  114.                        animation.setInterpolator(getInterPolator(style.animation_translate_interpolator));  
  115.                        animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());  
  116.                } catch (Exception e) {  
  117.                }  
  118.                return animation;  
  119.        }  
  120.        public static Animation getRotate3dyAnimation(StyleSheet style, View view, View viewParent) {  
  121.                Animation animation = null;  
  122.                try {  
  123.                        float rotate3DCenterX, rotate3DCenterY;  
  124.                        try {  
  125.                                rotate3DCenterX = Float.parseFloat(style.animation_rotate_3dy_center_x.replaceAll("%", ""));  
  126.                                rotate3DCenterX = rotate3DCenterX / 100;  
  127.                                if (rotate3DCenterX < 0) rotate3DCenterX = 0;  
  128.                                if (rotate3DCenterX > 1) rotate3DCenterX = 1;  
  129.                        } catch (Exception e) {  
  130.                                rotate3DCenterX = (float) 0.5;  
  131.                        }  
  132.                        rotate3DCenterX = view.getWidth() * rotate3DCenterX;  
  133.                        try {  
  134.                                rotate3DCenterY = Float.parseFloat(style.animation_rotate_3dy_center_y.replaceAll("%", ""));  
  135.                                rotate3DCenterY = rotate3DCenterY / 100;  
  136.                                if (rotate3DCenterY < 0) rotate3DCenterY = 0;  
  137.                                if (rotate3DCenterY > 1) rotate3DCenterY = 1;  
  138.                        } catch (Exception e) {  
  139.                                rotate3DCenterY = (float) 0.5;  
  140.                        }  
  141.                        rotate3DCenterY = view.getHeight() * rotate3DCenterY;  
  142.                        float from = RexseeUtilities.getFloat(style.animation_rotate_3dy_from, 0f);  
  143.                        float to = RexseeUtilities.getFloat(style.animation_rotate_3dy_to, 90f);  
  144.                        float rotate3DDepthZ = RexseeUtilities.getFloat(style.animation_rotate_3dy_depth_z, 310.0f);  
  145.                        boolean reverse = (style.animation_rotate_3dy_reverse.equalsIgnoreCase("true")) ? true : false;  
  146.                        animation = new Rotate3dyAnimation(from, to, rotate3DCenterX, rotate3DCenterY, rotate3DDepthZ, reverse);  
  147.                        animation.setDuration(RexseeUtilities.getInt(style.animation_rotate_3dy_duration, 1000));  
  148.                        animation.setRepeatCount(RexseeUtilities.getInt(style.animation_rotate_3dy_repeat_count, 0));  
  149.                        animation.setRepeatMode(style.animation_rotate_3dy_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);  
  150.                        animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_3dy_start_time, 0));  
  151.                        animation.setInterpolator(getInterPolator(style.animation_rotate_3dy_interpolator));  
  152.                        animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());  
  153.                } catch (Exception e) {  
  154.                }  
  155.                return animation;  
  156.        }  
  157.        public static Animation getRotate3dxAnimation(StyleSheet style, View view, View viewParent) {  
  158.                Animation animation = null;  
  159.                try {  
  160.                        float rotate3DCenterX, rotate3DCenterY;  
  161.                        try {  
  162.                                rotate3DCenterX = Float.parseFloat(style.animation_rotate_3dx_center_x.replaceAll("%", ""));  
  163.                                rotate3DCenterX = rotate3DCenterX / 100;  
  164.                                if (rotate3DCenterX < 0) rotate3DCenterX = 0;  
  165.                                if (rotate3DCenterX > 1) rotate3DCenterX = 1;  
  166.                        } catch (Exception e) {  
  167.                                rotate3DCenterX = (float) 0.5;  
  168.                        }  
  169.                        rotate3DCenterX = view.getWidth() * rotate3DCenterX;  
  170.                        try {  
  171.                                rotate3DCenterY = Float.parseFloat(style.animation_rotate_3dx_center_y.replaceAll("%", ""));  
  172.                                rotate3DCenterY = rotate3DCenterY / 100;  
  173.                                if (rotate3DCenterY < 0) rotate3DCenterY = 0;  
  174.                                if (rotate3DCenterY > 1) rotate3DCenterY = 1;  
  175.                        } catch (Exception e) {  
  176.                                rotate3DCenterY = (float) 0.5;  
  177.                        }  
  178.                        rotate3DCenterY = view.getHeight() * rotate3DCenterY;  
  179.                        float from = RexseeUtilities.getFloat(style.animation_rotate_3dx_from, 0f);  
  180.                        float to = RexseeUtilities.getFloat(style.animation_rotate_3dx_to, 90f);  
  181.                        float rotate3DDepthZ = RexseeUtilities.getFloat(style.animation_rotate_3dx_depth_z, 310.0f);  
  182.                        boolean reverse = (style.animation_rotate_3dx_reverse.equalsIgnoreCase("true")) ? true : false;  
  183.                        animation = new Rotate3dxAnimation(from, to, rotate3DCenterX, rotate3DCenterY, rotate3DDepthZ, reverse);  
  184.                        animation.setDuration(RexseeUtilities.getInt(style.animation_rotate_3dx_duration, 1000));  
  185.                        animation.setRepeatCount(RexseeUtilities.getInt(style.animation_rotate_3dx_repeat_count, 0));  
  186.                        animation.setRepeatMode(style.animation_rotate_3dx_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);  
  187.                        animation.setStartOffset(RexseeUtilities.getLong(style.animation_rotate_3dx_start_time, 0));  
  188.                        animation.setInterpolator(getInterPolator(style.animation_rotate_3dx_interpolator));  
  189.                        animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());  
  190.                } catch (Exception e) {  
  191.                }  
  192.                return animation;  
  193.        }  
  194.        public static Animation getSkewAnimation(StyleSheet style, View view, View viewParent) {  
  195.                Animation animation = null;  
  196.                try {  
  197.                        float skewCenterX, skewCenterY;  
  198.                        try {  
  199.                                skewCenterX = Float.parseFloat(style.animation_skew_center_x.replaceAll("%", ""));  
  200.                                skewCenterX = skewCenterX / 100;  
  201.                                if (skewCenterX < 0) skewCenterX = 0;  
  202.                                if (skewCenterX > 1) skewCenterX = 1;  
  203.                        } catch (Exception e) {  
  204.                                skewCenterX = (float) 0.5;  
  205.                        }  
  206.                        skewCenterX = view.getWidth() * skewCenterX;  
  207.                        try {  
  208.                                skewCenterY = Float.parseFloat(style.animation_skew_center_y.replaceAll("%", ""));  
  209.                                skewCenterY = skewCenterY / 100;  
  210.                                if (skewCenterY < 0) skewCenterY = 0;  
  211.                                if (skewCenterY > 1) skewCenterY = 1;  
  212.                        } catch (Exception e) {  
  213.                                skewCenterY = (float) 0.5;  
  214.                        }  
  215.                        skewCenterY = view.getHeight() * skewCenterY;  
  216.                        float fromX = RexseeUtilities.getFloat(style.animation_skew_x_from, 0f);  
  217.                        float toX = RexseeUtilities.getFloat(style.animation_skew_x_to, 1f);  
  218.                        float fromY = RexseeUtilities.getFloat(style.animation_skew_y_from, 0f);  
  219.                        float toY = RexseeUtilities.getFloat(style.animation_skew_y_to, 1f);  
  220.                        float skewDepthZ = RexseeUtilities.getFloat(style.animation_skew_depth_z, 310.0f);  
  221.                        boolean reverse = (style.animation_skew_reverse.equalsIgnoreCase("true")) ? true : false;  
  222.                        animation = new SkewAnimation(fromX, toX, fromY, toY, skewCenterX, skewCenterY, skewDepthZ, reverse);  
  223.                        animation.setDuration(RexseeUtilities.getInt(style.animation_skew_duration, 1000));  
  224.                        animation.setRepeatCount(RexseeUtilities.getInt(style.animation_skew_repeat_count, 0));  
  225.                        animation.setRepeatMode(style.animation_skew_repeat_mode.equalsIgnoreCase("normal") ? 1 : 2);  
  226.                        animation.setStartOffset(RexseeUtilities.getLong(style.animation_skew_start_time, 0));  
  227.                        animation.setInterpolator(getInterPolator(style.animation_skew_interpolator));  
  228.                        animation.initialize(view.getWidth(), view.getHeight(), viewParent.getWidth(), viewParent.getHeight());  
  229.                } catch (Exception e) {  
  230.                }  
  231.                return animation;  
  232.        }  
  233.        public static Interpolator getInterPolator(String name) {  
  234.                if (name.equalsIgnoreCase("AccelerateDecelerate")) {  
  235.                        return new AccelerateDecelerateInterpolator();  
  236.                } else if (name.equalsIgnoreCase("Accelerate")) {  
  237.                        return new AccelerateInterpolator(10.0f);  
  238.                } else if (name.equalsIgnoreCase("Decelerate")) {  
  239.                        return new DecelerateInterpolator(10.0f);  
  240.                } else if (name.equalsIgnoreCase("Anticipate")) {  
  241.                        return new AnticipateInterpolator(1.0f);  
  242.                } else if (name.equalsIgnoreCase("AnticipateOvershoot")) {  
  243.                        return new AnticipateOvershootInterpolator(1.0f, 1.5f);  
  244.                } else if (name.equalsIgnoreCase("Overshoot")) {  
  245.                        return new OvershootInterpolator(1.0f);  
  246.                } else if (name.equalsIgnoreCase("Bounce")) {  
  247.                        return new BounceInterpolator();  
  248.                } else if (name.equalsIgnoreCase("Cycle")) {  
  249.                        return new CycleInterpolator(1);  
  250.                } else if (name.equalsIgnoreCase("Linear")) {  
  251.                        return new LinearInterpolator();  
  252.                } else {  
  253.                        return new LinearInterpolator();  
  254.                }  
  255.        }  
  256. }

 

转载:http://www.eoeandroid.com/thread-152049-1-1.html

抱歉!评论已关闭.