动画

1、分类

        <1>视图动画(view animation)
                (1)帧动画(很多静态图片不断切换的效果,类似gif动态图。
                (2)补间动画(知道开始和结束,将中间的过程给补充起来。组件从初始状态变成结束状态,为了让改变看起来更自然更平滑的的一种动画
        <2>属性动画(property animation)3.0以上
2、帧动画
        <1>创建资源文件
                (1)在res/drawable以animation-list作为根节点
                            a.属性oneshot:是否显示一次
  1. android:oneshot="false"
                            b.子节点<item>设置要显示的图片和时间
  1. <item android:drawable= "@drawable/girl_1" android:duration="500"/>
        <2>使用
                (1)用图片控件设置背景setBackgroundResource
  1. img = (ImageView) findViewById(R.id.img);
  2. //设置图片的背景资源
  3. img.setBackgroundResource(R.drawable.frame);
                (2)然后通过getBackground()获取里面的资源,将图片资源Drawable转变为动画资源AnimationDrawable,再启动
  1. //获取到静态图片
  2. Drawable drawable = img.getBackground();
  3. //强制转变成动态图
  4. AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
  5. //启动动态图片
  6. animationDrawable.start();
3、补间动画
        <1>平移(TranslateAnimation)
                (1)构造方法
                        (1)fromXDelta, toXDelta, fromYDelta, toYDelta
                            fromXDelta:动画的初始x坐标为:控件的原始坐标+参数
                            toXDelta:动画的结束x坐标:控件的原始坐标+参数
                            fromYDelta:动画的初始y坐标为:控件的原始坐标+参数
                            toYDelta:动画的结束y坐标:控件的原始坐标+参数
                        (2)fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue
                            fromXType:
                                    Animation.RELATIVE_TO_SELF
                                    Animation.RELATIVE_TO_PARENT
                            fromXValue:动画的初始x坐标为:控件的原始坐标+参数*控件/父元素的宽度
                            toXValue:动画的结束x坐标:控件的原始坐标+参数*控件/父元素的宽度
                            fromyValue:动画的初始y坐标为:控件的原始坐标+参数*控件/父元素的高度
                (2)设置展示时间setDuration
                (3)设置重复展示次数.setRepeatCount(2): 无限次:Animation.INFINITE
                (4)设置动画停留在最后结束的位置上:.setFillAfter(true)
                (5)设置重复模式:setRepeatMode(Animation.REVERSE/RESTART)
                (6)让某个空间展示动画 控件.startAnimation(ta)
  1. //平移
  2. public void translate(View view){
  3.     //通过XML布局文件实现
  4. //把一个动画资源文件加成一个动画类
  5. Animation animation=AnimationUtils.loadAnimation(this,R.anim.mytranslate);
  6. TranslateAnimation animation2 = (TranslateAnimation) animation;
  7. //启动一个控件展示动画
  8. img.startAnimation(animation2);
  9.  
  10.     //通过代码实现
  11.     TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 0);
  12. animation = new TranslateAnimation(
  13. Animation.RELATIVE_TO_PARENT, 0,
  14. Animation.RELATIVE_TO_PARENT, 0.8f,
  15. Animation.RELATIVE_TO_SELF, 0,
  16. Animation.RELATIVE_TO_SELF, 0);
  17. animation.setDuration(2000);//设置时间
  18. animation.setFillAfter(true);//保证动画最终的效果
  19. animation.setRepeatCount(Animation.INFINITE);//重复次数
  20. animation.setRepeatMode(Animation.REVERSE);//重复模式
  21. img.startAnimation(animation);
  22. }
        <2>缩放(ScaleAnimation)
                (1)构造方法
                        (1)fromX, toX, fromY, toY(其实就是缩放的大小倍数)
                            fromX:动画的初始宽度为参数*控件的宽度
                            toX:动画的结束宽度为参数*控件的宽度
                            fromY:动画的初始高度为参数*控件的高度
                           toY:动画的结束高度为参数*控件的高度
                        (2)fromX, toX, fromY, toY, pivotX, pivotY
                            pivotX, pivotY:缩放点的x,y坐标
                        (3)fromX, toX, fromY, toY, pivotXType,
                            pivotXValue, pivotYType, pivotYValue
                            pivotXType:缩放类型
                            pivotX, pivotY:(如果类型为相对自己)代表缩放点的x坐标:控件的x+参数*控件的宽度
                (2)设置展示时间setDuration
                (3)设置重复展示次数.setRepeatCount(2)
                (4)设置重复模式:setRepeatMode(Animation.REVERSE)
                (5)设置动画停留在最后结束的位置上:.setFillAfter(true)
                (6)让某个空间展示动画 控件.startAnimation(ta)
  1. //缩放
  2. public void scale(View view){
  3. //通过XML布局文件实现动画
  4. Animation animation = AnimationUtils.loadAnimation(this, R.anim.myscale);
  5. ScaleAnimation animation2 = (ScaleAnimation) animation;
  6. img.startAnimation(animation2);
  7.     //通过代码实现动画,以下是三种构造方法
  8. ScaleAnimation animation = new ScaleAnimation(1, 0, 1, 0);
  9. animation = new ScaleAnimation(1, 0, 1, 0, img.getWidth()/2, img.getHeight()/2);
  10. animation = new ScaleAnimation(1, 0, 1, 0,
  11. Animation.RELATIVE_TO_SELF, 0.5f,
  12. Animation.RELATIVE_TO_PARENT, 0.5f);
  13. animation.setDuration(2000);
  14. img.startAnimation(animation);
  15. }
        <3>透明(AlphaAnimation)
                (1)构造方法
                        (1)fromAlpha, toAlpha
                            fromAlpha:开始透明度
                            toAlpha:结束透明度    1:全透明   0:不透
  1. //透明
  2. public void alpha(View view){
  3. //通过XML布局文件实现动画
  4. Animation animation = AnimationUtils.loadAnimation(this, R.anim.myalpha);
  5. img.startAnimation(animation);
  6. /**
  7. * 1, 0.5f:开始和结束的透明度值[0,1]
  8. */
  9. AlphaAnimation animation = new AlphaAnimation(1, 0.5f);
  10. animation.setDuration(2000);
  11. img.startAnimation(animation);
  12. }
        <4>旋转(RotateAnimation)
                (1)构造方法
                        (1)fromDegrees,toDegrees
                            开始角度和结束角度
                        (2)fromDegrees, toDegrees, pivotX, pivotY
                            pivotX, pivotY:旋转点的x,y坐标
                        (3)fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue
                            相对位置
                (2)设置展示时间setDuration
                (3)设置重复展示次数.setRepeatCount(2)
                (4)设置动画停留在最后结束的位置上:.setFillAfter(true)
                (5)设置重复模式:setRepeatMode(Animation.REVERSE/RESTART)
                (6)让某个空间展示动画 控件.startAnimation(ta)
  1. //旋转
  2. public void rotate(View view){
  3. //用XML布局文件实现动画
  4. Animation animation = AnimationUtils.loadAnimation(this, R.anim.myrotate);
  5. img.startAnimation(animation);
 
//通过代码来实现动画
  1. RotateAnimation animation = new RotateAnimation(0, -180);
  2. //img.getWidth()/2:当前的x= 原来的x+参数
  3. animation = new RotateAnimation(0, 360, img.getWidth()/2, img.getHeight()/2);
  4. //Animation.RELATIVE_TO_SELF, 0.5f:当前的x =原来的x + 控件的宽度*参数
  5. //Animation.RELATIVE_TO_PARENT, 0.5f:当前的x = 原来的x + 当前的父布局的宽度*参数
  6. animation = new RotateAnimation(0, 360,
  7. Animation.RELATIVE_TO_SELF, 0.5f,
  8. Animation.RELATIVE_TO_SELF, 0.5f);
  9. animation.setDuration(2000);
  10. img.startAnimation(animation);
  11. //绑定动画的监听器
  12. animation.setAnimationListener(new AnimationListener() {
  13. @Override
  14. public void onAnimationStart(Animation animation) {
  15. }
  16. @Override
  17. public void onAnimationRepeat(Animation animation) {
  18. }
  19. @Override
  20. public void onAnimationEnd(Animation animation) {
  21. }
  22. });
  23. }
        <5>用xml文件写补间动画
                在res文件下创建xml文件,文件夹为anim,设置动画类型,时间,重复次数....startOffset:延迟显示时间
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <scale
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:fromXScale="1"
  5. android:toXScale="2"
  6. android:fromYScale="1"
  7. android:toYScale="2"
  8. android:pivotX="50%"
  9. android:pivotY="50%"
  10. android:duration="2000"
  11. android:fillAfter="true"
  12. android:repeatCount="1"
  13. android:repeatMode="reverse"
  14. >
  15. <!--
  16. 缩放的倍数
  17. android:fromXScale,android:fromYScale="1":初始的x,y的坐标:原来x,y的坐标*参数
  18. android:pivotX,android:pivotY:缩放点的x,y坐标
  19. -->
  20. </scale>
                在java文件中AnimationUtils的加载动画方式加载动画文件, 最后启动就可以了
  1. Animation animation = AnimationUtils.loadAnimation(this, R.anim.myrotate);
  2. img.startAnimation(animation);
        <6>启动所有动画AnimationSet
                (1)set.addAnimation:添加动画
  1. //动画集
  2. AnimationSet set = new AnimationSet(false);
  3. //添加动画类型,可以添加多个
  4. //set.addAnimation(a);
  5. //set.addAnimation(b);
  6. //set.addAnimation(c);
  7. //set.addAnimation(d);
                (2)控件.startAnimation(set)启动
  1. img.startAnimation(set);
4、属性动画
        <1>ObjectAnimator.ofFloat(  imge, "translationX", -100,100)
                    平移translationX,translationY
                    缩放scaleY,scaleX
                    透明alpha
                    旋转rotationX,rotationY,rotation
        <2>常用设置
                (1)oa.start();//开启动画
                (2)oa.setDuration(2000);//设置时间
                (3)设置重复展示次数.setRepeatCount(2)
                (4)设置重复模式:setRepeatMode(Animation.REVERSE)
        <3>动画集(new AnimatorSet();)
                (1)set.playTogether(ObjectAnimator1,ObjectAnimator2);一起展示
                (2)set.playSequentially(ObjectAnimator1,ObjectAnimator2)挨着展示
                (3)set.start();
        <4>用XML文件写属性动画
                (1)在res文件下创建xml文件,文件夹为animator,子节点为objectAnimator,设置动画类型,时间,重复次数...
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="2000"
  4. android:propertyName="translationY"
  5. android:valueFrom="0"
  6. android:valueTo="100" >
  7. <!--
  8. android:propertyName:设置动画类型
  9. android:valueFrom="0":开始的值
  10. android:valueTo="100":结束值
  11. -->
  12. </objectAnimator>
                (2)在java文件中AnimatorInflater的加载动画方式加载动画文件.loadAnimator(this, R.animator.myanimator)
  1. //通过一个动画资源加载器去加载一个动画xml文件
  2. ObjectAnimator oa = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.myobjectanimator);
                (3)最后绑定要展示的控件animator.setTarget(imge);最后启动就可以了
  1. //绑定动画的执行者
  2. oa.setTarget(img);
  3. oa.start();
原文地址:https://www.cnblogs.com/android-blogs/p/5735398.html