Android中的动画有哪几类,它们的特点和区别是什么?
动画主要是分为了两种,
安卓3.0之前主要就是View Animation 其中分为两种,补间动画&帧动画.
3.0之后出现新的叫做属性动画(Property Animation);
大体上View Animation用法如下:
Tween Animation实际开发中用的很多,可以实现图片的透明,缩放,平移,旋转以及插补器五种效果,很多app一开始进入界面的时候就是一个动画的界面,一般叫做splashActivity,上面用的就是补间动画,看上去有的旋转一下,有的是一开始空白逐渐变为正常,这都是补间动画的效果.
ps: /03ViewAnimationDemo 是着一个demo 补间动画:View Animation api解释: 创建:既可以在xml文件中也可以在代码中创建 在xml文件中创建的话要把文件存放在 res /anim/目录下 可以有透明 缩放 平移 旋转 插补器 集合的形式 The animation XML file belongs in the res/anim/ directory of your Android project. The file must have a single root element: this will be either a single <alpha>, <scale>, <translate>, <rotate>, interpolator element, or <set> element that holds groups of these elements (which may include another <set>). 基本框架: /* * 透明 */ public void alpha(View v) { AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);// 配置参数 从无到有 aa.setDuration(2000);//时间 aa.setRepeatCount(2);//次数 aa.setRepeatMode(Animation.REVERSE);// Animation.RESTART ic_icon.setAnimation(aa);// 加载动画 aa.start();//开始 } 其中 aa . setRepeatMode ( Animation . REVERSE ); // Animation.RESTART 从头开始 还是倒着转回来 旋转动画: RotateAnimation ra = new RotateAnimation(0, 360); ra.setDuration(1000); ra.setRepeatCount(2); ra.setRepeatMode(Animation.REVERSE); //这是重复的模式 限定你执行完之后 是倒转回来 还是从头来一次 ic_icon.setAnimation(ra);// 加载动画 ra.start();//开始 旋转方式是按照 自己当前位置的左上角为中心 进行旋转 public void rotateToself(View v) { int pivotXType = Animation.RELATIVE_TO_SELF ; float pivotXValue = 0.5f;//选择的中心点 int pivotYType = Animation.RELATIVE_TO_SELF; float pivotYValue = 0.5f; //相对于自己的旋转 RotateAnimation ra = new RotateAnimation(0, 360, pivotXType, pivotXValue, pivotYType, pivotYValue); 参数设置为按照 相对于自己 前四个参数是设置旋转的中心点 0.5f 从当前的位置(控方框的左上角) 往右(往下)走自己的0.5个单位 就是中心点 public void rotateToParent(View v) { //相对于父类的旋转 int pivotXType = Animation.RELATIVE_TO_PARENT ; float pivotXValue = 0.2f;//选择的中心点 int pivotYType = Animation.RELATIVE_TO_PARENT; float pivotYValue = 0.2f; //相对于自己的旋转 RotateAnimation ra = new RotateAnimation(0, 360, pivotXType, pivotXValue, pivotYType, pivotYValue); 参数设置为按照 相对于父布局 前四个参数是设置旋转的中心点 0.2f 从当前的位置往右(往下)走父布局的0.2个单位 就是中心点 android.view.animation.RotateAnimation.RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) Constructor to use when building a RotateAnimation from code Parameters: fromDegrees Rotation offset to apply at the start of the animation. toDegrees Rotation offset to apply at the end of the animation. pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. pivotXValue The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. pivotYValue The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge. This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. (2)缩放 和旋转是一样的 参数的多出来的就是限定你围绕旋转的中心点的坐标 一般是4个参数 确定绕哪一个中心点旋转 平移: 一是按照自己的初始位置还是 控件的左上角从00位置 向左向下都动30 public void transate(View v) { TranslateAnimation ta = new TranslateAnimation(0, 30, 0, 30); 二是设置初始的位置 和方式 public void transateRelative(View v) { int fromXType = Animation.RELATIVE_TO_PARENT; float fromXValue = -0.5f; int toXType = Animation.RELATIVE_TO_PARENT; float toXValue = 0.5f; int fromYType = Animation.RELATIVE_TO_PARENT; float fromYValue = -0.5f; int toYType = Animation.RELATIVE_TO_PARENT; float toYValue = 0.5f; TranslateAnimation ta = new TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue); 概述:初始位置是相对于父布局 -0.5f 就是在当前位置 向左(向上)走一个父布局的0.5个单位 为初始点 0.5f 向右(向下)走一个 走一个父布局的0.5个单位 为终点 (3)动画插值器为当前的动画插入一些特效 api简介: public interface Interpolator implements TimeInterpolator android.view.animation.Interpolator Known Indirect Subclasses //直接的实现类 可以拿过来直接new的 当然你也可以添加方法 AccelerateDecelerateInterpolator, AccelerateInterpolator, AnticipateInterpolator, AnticipateOvershootInterpolator, BaseInterpolator, BounceInterpolator, CycleInterpolator, DecelerateInterpolator, FastOutLinearInInterpolator, FastOutSlowInInterpolator, LinearInterpolator, LinearOutSlowInInterpolator, OvershootInterpolator, PathInterpolator Class Overview An interpolator defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated, repeated, etc. 减速的插补器 反弹的插补器 设置一个动画插入器 给动画的执行过程添加一些特效 动画的集合: public void set(View v) { // AnimatorSet set = new AnimatorSet(); AnimationSet set = new AnimationSet(false);// 不分享插补器 set.addAnimation(ta);//平移 set.addAnimation(ra);//旋转 // 这是重复的模式 限定你执行完之后 是倒转回来 还是从头来一次 ic_icon.setAnimation(set); set.start();// 开始 这就是把所有的动画集合起来 一起弄: 总结: 显然补间动画 显然默认的初始的坐标原点就是自己控件本身的左上角位置; (2)补间动画 通过XMl文件的形式: 创建xml文件 选择类型 会自动就有 几种Root Elements 让你选择 如下图所示: 然后工程文件下会自动生成res/anim文件夹 如下图所示: 那么具体的demo.xml文件中怎么书写 代码中是怎么调用的呢? /* * xml文件的形式 开启动画是怎么处理的 就是通过那个AnimationUtils.直接调用 */ public void xmlTranslate(View v) { Animation axml = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.demo); ic_icon.setAnimation(axml); axml.start(); } <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXDelta="-50" android:fromYDelta="0" android:repeatCount="2" android:repeatMode="reverse" android:toXDelta="50" android:toYDelta="0" > </translate> 以上就是以平移 为列子 xml格式创建动画的形式; 补间动画总结: view动画并没有改变他真实的位置 ,看到是跑到了别的地方实际上 并不会改变它真实的位置 简单的就是给图标设置一个点击事件就好 可以点击试一下 (2)属性动画:顾名思义 就是改变他的属性 改变他的坐标 是真正的从一个位置移动到另一个位置 通过xml文件也能定义一个动画 文件变成了res/animator 具体的属性名字 都是可以添加的 最大的区别就是确实会改变动画的位置 代码的方式创建属性动画: 这一代码充分诠释了属性动画的使用方式 /* * 扫描结束之后 开启开门动画 translationX 设置方式 */ public void startOpendoorAnim() { AnimatorSet set = new AnimatorSet(); // avLeft.setAlpha(alpha); // avLeft.setTranslationX(translationX) 照着抄写 ObjectAnimator ofLeftMoveAnim = ObjectAnimator.ofFloat(avLeft, "translationX", 0, -avLeft.getWidth()); ObjectAnimator ofLeftAlphaAnim = ObjectAnimator.ofFloat(avLeft, "alpha", 1.0f, 0.0f);// 渐变消失 ObjectAnimator ofRightMoveAnim = ObjectAnimator.ofFloat(avRight, "translationX", 0, avRight.getWidth()); ObjectAnimator ofRightAlphaAnim = ObjectAnimator.ofFloat(avRight, "alpha", 1.0f, 0.0f); ObjectAnimator oaResult = ObjectAnimator.ofFloat(avResult, "alpha", 0.0f, 1.0f); // 添加 set.playTogether(ofLeftMoveAnim, ofLeftAlphaAnim, ofRightMoveAnim, ofRightAlphaAnim, oaResult); set.setDuration(2000); set.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); btnavRescan.setEnabled(true);// 禁止点击 } @Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); btnavRescan.setEnabled(false);// 禁止点击 } }); set.start();// 开始动画 } xml文件的方式创建属性的动画 也是一点击就会出现的 然后在项目目录下创建文件夹 如下图所示