• Animations介绍及实例


    Animations的使用
    *一套实现动画的API
    1.什么是Animations
       实现动画效果
    2.Animations的分类
       1)TweenedAnimations
            提供了旋转,移动,伸展,和淡出等等效果
       a.Alpha:淡入淡出效果
       b.Scale:缩放效果
       c.Rotate:旋转效果
       d.Translate:移动效果
       2)Frame-by-FrameAnimations
            创建一个Drawable序列,可以按照时间间歇一个个的显示
    3.Animations的使用方法(一)代码实现
    1)使用TweenedAnimations的步骤
         a.创建一个AnimationSet对象
            可以把几个动画对象放在一起
         b.根据需要创建相应的Animation对象
         c.根据软件的动画的需求,为Animation对象设置相应的数据
         d.将Animation对象添加到AnimationSet对象当中
         e.使用控件对象开始执行AnimationSet
    2)Alpha效果实现(淡入淡出)
         /**创建一个AnimationSet对象*/
         AnimationSet animationSet = 
             new AnimationSet(true);
        /**创建一个AlphaAnimation对象*/
        AlphaAnimation alphaAnimation = 
            new AlphaAnimation(1, 0);
        /**设置动画执行的时间*/
         alphaAnimation.setDuration(1000);
      /**将AlphaAnimation对象添加到AnimationSet中*/
        animationSet.addAnimation(alphaAnimation);
        /**使用image的方法开始执行动画*/
       image.startAnimation(animationSet);     
    3)Rotate效果实现(旋转效果)
        重要的地方就是其构造方法:
        RotateAnimation rotateAnimation = 
                new RotateAnimation(
               /**起始角度*/
               0, 
               /**终止角度*/
               360, 
               /**设置旋转的圆心*/
               pivotX,
               pivotY);
       pivotX,pivotY是设置旋转的圆心
       设置有三种方式:
       a.Animation.ABSOLUTE
       b.Animation.RELATIVE_TO_SELF
        内轴:例如:如果是pivotX为0.5pivotY为0,则圆心的位置就是在图片宽度一半的位置
        外轴:例如:如果是pivotX为0.5pivotY为0.5,则圆心的位置就是在图片的几何中心位置
       c.Animation.RELATIVE_TO_PARENT
    4)translate效果实现(移动效果)
         重要的地方就是其构造方法:
                            TranslateAnimation translateAnimation = new TranslateAnimation(
                               /**设置X方向的起始和终止位置*/
                               Animation.RELATIVE_TO_SELF,0f,
                               Animation.RELATIVE_TO_SELF,0.5f,
                               /**设置Y方向的起始和终止位置*/
                               Animation.RELATIVE_TO_SELF,0f,
                               Animation.RELATIVE_TO_SELF,1f);
    5)Alpha的淡入淡出效果
        重要的地方就是其构造方法:
                        /**
                     * 创建AlphaAnimation对象
                     *  其构造方法是有两个参数
                     *  1代表的是完全不透明
                     *  0代表的是完全的透明
                     *  其方法是表示从完全不透明到完全透明的渐变过程
                     */
                    AlphaAnimation alphaAnimation = 
                        new AlphaAnimation(1, 0);
    6)Scale的缩放效果
        重要的地方就是其构造方法:
                         ScaleAnimation scaleAnimation = 
                          new ScaleAnimation(
                                  /**横纵坐标变到以前的0.1*/
                                  1,0.1f,1,0.1f,
                                  /**设置缩放的旋转轴*/
                                  Animation.RELATIVE_TO_SELF,0.5f,
                                  Animation.RELATIVE_TO_SELF,0.8f);
    4.Animation的通用属性
       这里设置的animationSet的通用属性
                    /**设置动画执行的时间*/
                    animationSet.setDuration(1000);
                    /**动画执行完毕后,停留在控件结束的状态*/
                    animationSet.setFillAfter(true);
                    /**动画执行完毕后,停留在控件开始的状态*/
                    animationSet.setFillBefore(false);
                    /**设置动画执行之前的等待时间*/
                    animationSet.setStartOffset(2000);
                    /**设置动画的重复执行次数*/
                    animationSet.setRepeatCount(3);
    5.Animation的使用(二)xml实现
       使用步骤:
       a.在res文件夹下创建一个名为anim的文件夹
       b.创建xml文件,并首先加入set标签,该标签为
       <set  xmlns:android="http://schemas.android.com/apk/res/android"
               android:interpolator="@android:anim/accelerate_interpolator">
       </set>
       c.在标签中加入rotate  alpha  scale  translate的配置文件
       d.在代码中使用AnimationUtils中装载xml文件,并生成Animation对象
       核心实现代码:
                       Animation animation = AnimationUtils.loadAnimation
                    (MainActivity.this, R.anim.scale);
                    image.startAnimation(animation);
    分别实现的xml文件如下:
    1)Rotate的xml布局:
        <rotate
          /**变换角度*/
          android:fromDegrees="0"
          android:toDegrees="360"
          android:pivotX="50%"
          android:pivotX="50%"
          android:duration="3000"
          />
    2) Alpha的xml布局
        <alpha
           android:fromAlpha="1"
           android:toAlpha="0"
           android:startOffset="500"
           android:duration="500"
          />
    3)Scale的xml布局
          <scale
         android:fromXScale="1"
         android:toXScale="0"
         android:fromYScale="1"
         android:toYScale="0"
         android:pivotX="50%"
         android:pivotY="50%"
         android:duration="3000"
       />
    4)Translate的xml布局实现
        <translate
          android:fromXDelta="50%"
          android:toXDelta="100%"
          android:fromYDelta="0%"
          android:toYDelta="100%"
          android:duration="2000"
        />
    注意:属性android:pivotX的值共有三种设置方法
             a.android:pivotX="50"使用绝对位置定义
         b.android:pivotX="50%"相对于控件本身定位
         c.android:pivotX="50%p"相对于父控件定位
    6.Animation的使用(三)
    1)AnimationSet的使用方法
         AnimationSet继承了Animation,包含一系列的Animation对象
    2)Interpolator的使用方法
         定义了动画变换速率
         <?xml version="1.0" encoding="utf-8"?>
    <set  xmlns:android="http://schemas.android.com/apk/res/android"
           /**逐渐加速*/
          android:interpolator="@android:anim/accelerate_interpolator"
          /**如果此处是true下面动画都按这个速率*/
          /**如果此处是false下面动画都需要分别设置*/
          android:shareInterpolator="true">
         <translate
          android:fromXDelta="50%"
          android:toXDelta="100%"
          android:fromYDelta="0%"
          android:toYDelta="100%"
          android:duration="2000"
          android:interpolator="@android:anim/linear_interpolator"
        />
        <alpha
           android:fromAlpha="1"
           android:toAlpha="0"
           android:startOffset="500"
           android:duration="500"
           android:interpolator="@android:anim/accelerate_interpolator"
          />
        <scale
         android:fromXScale="1"
         android:toXScale="0"
         android:fromYScale="1"
         android:toYScale="0"
         android:pivotX="50%"
         android:pivotY="50%"
         android:duration="2000"
         android:interpolator="@android:anim/cycle_interpolator"
         />
       <rotate
          android:fromDegrees="0"
          android:toDegrees="360"
          android:pivotX="50%"
          android:pivotY="50%"
          android:duration="3000"
          android:interpolator="@android:anim/accelerate_decelerate_interpolator"
       />
    </set>
    速率变换的分类:
    a.AccelerateDecelerateInterpolator在开始和结束时
       速率改变比较慢,在中间的时候加速
    b.AccelerateInterpolator在开始时速率改变比较慢,在中间的时候加速
    c.CycleInterpolator动画循环播放特定的才次数,速率沿着正弦曲线
    d.DecelerateInterpolator在动画开始的地方变换比较慢,然后开始
       减速
    e.LinearInterpolator动画以均匀的速率改变
    在xml可以配置的速率属性:
                accelerate_decelerate_interpolator  加速-减速动画
                accelerate_interpolator        加速-动画
                decelerate_interpolator        减速- 动画
    3)Frame-By-Frame Animations的使用方法
         核心代码:
                    /**为图片设置资源*/
                    image.setBackgroundResource(R.drawable.anim_ghost);
                    /**得到动画*/
                    AnimationDrawable animationDrawable = 
                        (AnimationDrawable)image.getBackground();
                    /**开始执行动画*/
                    animationDrawable.start();
          xml配置:
          在res/drawable下建立一个xml文件
          比如我们叫anim_ghost.xml
          <?xml version="1.0" encoding="utf-8"?>
      <animation-list 
       xmlns:android="http://schemas.android.com/apk/res/android"
       androidneshot="false">
       <item 
          android:drawable="@drawable/g1"
          android:duration="1000"   
        />
       <item 
          android:drawable="@drawable/g2"
          android:duration="1000"   
        />
       </animation-list>
    7.Animation高级使用
       LayoutAnimationController,为Layout中的控件,
       或者是一个ViewGroup控件设置动画效果
       1)ListView与Animation结合使用
        步骤:
        a.在res/anim当中创建一个新文件,名为
           list_anim_layout.xml文件
           为整个layout设置动画效果
            <layoutAnimation
             xmlns:android="http://schema.android.com/apk/res/android"
             android:delay="5"
             android:animationOrder="random"
             android:animation="@anim/list_anim"/>
            b.设置一个动画文件
           <?xml version="1.0" encoding="utf-8"?>
               <set  xmlns:android="http://schemas.android.com/apk/res/android"
                 android:interpolator="@android:anim/accelerate_interpolator">
                <rotate
                  android:fromDegrees="0"
                  android:toDegrees="360"
                  android:pivotX="50%p"
                  android:pivotY="50%p"
                  android:duration="3000"
                 />
                 </set>
        c.在布局文件中为ListView添加如下配置
           android:layoutAnimation="@anim/list_anim_layout"
       2)LayoutAnimationController的使用
           a.创建一个Animation对象
              加载xml文件的方式,或者直接使用Animation进行new
           b.创建LayoutAnimationController对象
              LayoutAnimationController lac = new 
              LayoutAnimationController(animation)
           c.设置控件显示的顺序
           lac.setOrder(LayoutAnimationController.ORDER_NORMAL) 
           d.为ListView设置LyaoutAnimationController属性
           listView.setLayoutAnimation(lac);
       3)AnimationListener的使用方法
            主要包含三个方法:
          /**动画效果结束时调用*/
        a.onAnimationEnd(Animation animation)
          /**动画效果重复时调用*/
        b.onAnimationRepeat(Animation animation)
         /**动画开始时调用*/
        c.onAnimationStart(Animation animation)
           主要是添加控件和删除控件的实现
  • 相关阅读:
    (1)quartz集群调度机制调研及源码分析---转载
    转《深入理解Java虚拟机》学习笔记之最后总结
    liux vim命令
    http请求返回响应码的意思
    java中时间格式yyyyMMddHHmmss的大小写问题
    并发容器之CopyOnWriteArrayList
    快速失败(fail-fast)和安全失败(fail-safe)的区别
    关于java集合类HashMap的理解
    权限管理系统(一):权限系统与RBAC模型概述
    一个简单的实现不同权限的用户登录后看到不同的菜单设计的数据库表清单
  • 原文地址:https://www.cnblogs.com/renyuan/p/2553955.html
Copyright © 2020-2023  润新知