动画

Android动画主要包括以下两种类型

    1. View Animation(View动画) :在View动画框架中包括两种类型的动画: (1) Tween animation(补间动画) 可以对View进行一系列的动画操作,包括淡入淡出、缩放、平移、旋转四种 (2).Frame animation(帧动画) 其实就是将一个完整的动画拆分成一张张单独的图片,然后再将它们连贯起来进行播放,类似于动画片的工作原理

 

                    2.Property Animation(属性动画):它主要是通过修改一个对象的属性值来创建动画

view 动画

view动画-----补间动画

 

  •        
  • 1:xml中
    • alph:渐变透明度动画效果

      scale:渐变尺寸伸缩动画效果

      translate:画面转换位置移动动画效果

      rootate:画面转移旋转动画效果

    • javacode中
    • AlphaAnimation:渐变透明度动画效果

      ScaleAnimation:渐变尺寸伸缩动画效果

      TranslateAnimation:画面转换位置移动动画效果

      RotateAnimation:画面转移旋转动画效果

2:如何在xml文件中定义动画

步骤如下:

①新建 Android 项目

②在res目录中新建anim文件夹

③在anim目录中新建一个my_anim.xml(注意文件名小写)

④在 my_anim.xml 加入动画代码

5.引用方式:.在XML文件中:@[package:]anim/filenam

在Java代码中:R.anim.filename

 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha />
    <scale />
    <translate />
    <rotate />
</set>

3.代码中定义动画

splash = (RelativeLayout)findViewById(R.id.splash);
//以旋转动画为例
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(2000);
rotateAnimation.setFillAfter(true); (播放完毕停留在那个位置)

view动画-----帧动画

帧动画是最容易实现的一种动画,这种动画更多的依赖于完善的UI资源,他的原理就是将一张张单独的图片连贯的进行播放,
从而在视觉上产生一种动画的效果;有点类似于某些软件制作gif动画的方式

步骤如下:

①新建 Android 项目

②在res目录中新建anim文件夹

③在anim目录中新建一个my_anim.xml(注意文件名小写)

④在 my_anim.xml 加入动画代码

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/a_0"
        android:duration="100" />
    <item
        android:drawable="@drawable/a_1"
        android:duration="100" />
    <item
        android:drawable="@drawable/a_2"
        android:duration="100" />
</animation-list>

layoutAnimation

前几天遇到一个需求,将动画顺序改为左上到右下角展开,对某一组控件播放一样的动画,layoutAnimation可以实现

layout-animation可由xml和代码两种方式配置

  • XML配置

由于layout-animation是对于某一组控件的操作,就需要一个基本的动画来定义单个控件的动画。另外还可以定义动画的显示顺序和延迟:

在res目录中新建anim文件夹 ,在anim目录中新建一个my_anim.xm

  1. <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
  2.         android:delay="30%"
  3.         android:animationOrder="reverse"
  4.         android:animation="@anim/slide_right"/>

其中

  1. android:delay表示动画播放的延时,既可以是百分比,也可以是float小数。

  2. android:animationOrder表示动画的播放顺序,有三个取值normal(顺序)、reverse(反序)、random(随机)。

  3. android:animation指向了子控件所要播放的动画。

  4. 如果在xml中文件已经写好LayoutAnimation,可以使用AnimationUtils直接加载:AnimationUtils.loadLayoutAnimation(context,  R.anim.my_anim)

但是LayoutAnimation默认只有三种顺序,即顺序逆序和随机,不能满足需求。去翻翻源码看它是怎么实现的,有没有提供方法自定义顺序?结果翻到了一个LayoutAnimationController#getTransformedIndex(AnimationParameters params)方法,返回值就是播放动画的顺序。并且这个方法是protected的,明显就是可由子类来扩展。既然找到了方法,那么就去实现它
  1.      * custom LayoutAnimationController for playing child animation
  2.      * in any order.
  3.      *
  4.      */
  5. public class CustomLayoutAnimationController extends LayoutAnimationController {
  6.  
  7.     // 7 just lucky number
  8.     public static final int ORDER_CUSTOM  = 7;
  9.  
  10.     private Callback onIndexListener;
  11.  
  12.     public void setOnIndexListener(OnIndexListener onIndexListener) {
  13.         this.onIndexListener = onIndexListener;
  14.     }
  15.  
  16.     public CustomLayoutAnimationController(Animation anim) {
  17.         super(anim);
  18.     }
  19.  
  20.     public CustomLayoutAnimationController(Animation anim, float delay) {
  21.         super(anim, delay);
  22.     }
  23.  
  24.     public CustomLayoutAnimationController(Context context, AttributeSet attrs) {
  25.         super(context, attrs);
  26.     }
  27.  
  28.     /**
  29.      * override method for custom play child view animation order
  30.     */
  31.     protected int getTransformedIndex(AnimationParameters params) {
  32.         if(getOrder() == ORDER_CUSTOM &amp;&amp; onIndexListener != null) {
  33.             return onIndexListener.onIndex(this, params.count, params.index);
  34.         } else {
  35.             return super.getTransformedIndex(params);
  36.         }
  37.     }
  38.  
  39.     /**
  40.      * callback for get play animation order
  41.      *
  42.      */
  43.     public static interface Callback{
  44.  
  45.         public int onIndex(CustomLayoutAnimationController controller, int count, int index);
  46.     }
  47. }

通过复写getTransformedIndex方法,添加自定义顺序ORDER_CUSTOM,让callback自定义控件播放动画的顺序,即可以达到任何想要的效果

属性动画

 

  1. ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);  
  2. anim.setDuration(300);  
  3. anim.start();  

 

posted @ 2017-07-05 15:01  让学习如呼吸一般自然  阅读(272)  评论(0编辑  收藏  举报