Android 动画总结

1.补间动画 View Animation(Tween Animation)

1.1位移动画

xml定义:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:toXDelta="300"
    android:fromYDelta="100%p"
    android:toYDelta="0"
    android:duration="200"/>
<!--
 fromXDelta  动画起始位置的横坐标
 toXDelta    动画起结束位置的横坐标
 fromYDelta  动画起始位置的纵坐标
 toYDelta    动画结束位置的纵坐标
 duration    动画的持续时间
 100%p 表示自身的长度的位置
 -->  

代码实现:

Animation  animation = new TranslateAnimation(0,50,0,50);
参数1:x轴的起始位置
参数2:x轴的终止位置
参数3: y轴的起始位置
参数4:y轴的终止位置
相对于原图位置的原点(图片的右上角为0,0),如果不想用这个点作为参照点,可以使用其他构造
TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)
参数1,参数3,参数5,参数7就是设置参照点的方式

1.2旋转动画

xml定义:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="200"  />
<!--
 fromDegrees:表示旋转的起始角度
 toDegrees:表示旋转的结束角度
 duration    动画的持续时间
 from<to:顺时针旋转
 from>to:逆时针旋转
 -->  

代码实现:

Animation animation  = new RotateAnimation(360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
参数1:旋转的起始角度
参数2:旋转的终止角度
参数3:旋转中心的x轴取值参照方式
参数4:中心点x轴的取值
参数5:旋转中心的y轴取值参照方式
参数6:中心点y轴的取值

1.3透明度动画

xml定义:

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.1"
    android:duration="200"/>
<!--
 fromAlpha :起始透明度
 toAlpha:结束透明度
 1.0表示完全不透明
 0.0表示完全透明
 -->  

代码实现:

Animation animation = new AlphaAnimation(1f,0.1f);
参数1: 起始透明度;
参数2: 目标透明度;

1.4缩放动画

xml定义:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.5"
    android:toXScale="1.0"
    android:fromYScale="0.5"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="200"/>
<!--
 fromXScale:表示沿着x轴缩放的起始比例
 toXScale:表示沿着x轴缩放的结束比例
 fromYScale:表示沿着y轴缩放的起始比例
 toYScale:表示沿着y轴缩放的结束比例
 1.0表示原来的大小
 50%表示图片中心点
 -->  

代码实现:

Animation   animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
参数1:x方向起始大小(1f表示原图大小)
参数2:x方向终止大小(0.2f表示原图的0.2倍)
参数3:y方向起始大小(1f表示原图大小)
参数4:y方向终止大小(0.2f表示原图的0.2倍)
参数5:缩放中心点x轴取值的参照方式
参数6:中心点x轴的取值(0.5f表示相对与原图的0.5倍)
参数7:缩放中心点y轴取值参照方式
参数8:中心点y轴的取值(0.5f表示相对与原图的0.5倍)
(注:参照方式:Animation.RELATIVE_TO_SELF:自身   Animation.RELATIVE_TO_PARENT:父控件   Animation.ABSOLUTE:绝对)

1.5 共有属性

android:duration(播放时间)
android:repeatCount: 重复的次数  默认值是0 代表旋转1次,值为-1或者infinite时,表示动画永不停止
android:repeatMode:  设置重复的模式。默认是restart。(当repeatCount的值大于0或者为infinite时才有效)
                     设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。
对应代码方法:
Animation.setDuration(long durationMillis)
Animation.setRepeatCount(int repeatCount)
Animation.setRepeatMode(int repeatMode)

android:interpolator(动画的渲染器)
  accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速
  decelerate_interpolator(动画减速器) 使动画在开始的时候 最快,然后逐渐减速
  accelerate_decelerate_interpolator(动画加速减速器)慢到快(中间位置最快)到慢
  anticipate_interpolator(动画前反向器) 先往动画移动的反方向移动一点然后在沿着设定的动画移动
  overshoot_interpolator(动画后反向器) 最后超出目的值然后缓慢改变到目的值
  anticipate_overshoot_interpolator (动画双反向器) 先往动画的反方向移动一点,然后沿着设定的方向移动到终点之后继续移动一点然后在回弹到最终设定的位置
  bounce_interpolator(动画弹跳器) 快到目的值时值会跳跃
  cycle_interpolator(动画循环器) 动画循环一定次数
  linear_interpolator(动画匀速器) 线性均匀改变
  overshoot_interpolator(动画减速器) 快速到达终点并超出一小步最后回到终点
 使用:android:interpolator="@android:anim/accelerate_interpolator"

对应代码方法:
Animation.setInterpolator(Context context, int resID) //R.anim.accelerate_interpolator
Animation.setInterpolator(Interpolator i)
  TimeInterpolator          一个接口,允许你自定义interpolator,以下几个都是实现了这个接口
  AccelerateInterpolator        加速,开始时慢中间加速
  DecelerateInterpolator        减速,开始时快然后减速
  AccelerateDecelerateInterolator   先加速后减速,开始结束时慢,中间加速
  AnticipateInterpolator       反向 ,先向相反方向改变一段再加速播放
  AnticipateOvershootInterpolator   反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
  BounceInterpolator          跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
  CycleInterpolator          循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2 * mCycles * Math.PI * input)
  LinearInterpolator         线性,线性均匀改变
  OvershottInterpolator        超越,最后超出目的值然后缓慢改变到目的值

2.组合动画

2.1AnimationSet

xml定义:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:shareInterpolator="true" >
    <scale  ... />
    <rotate ...  />
    <translate ...  />
    <alpha ...  />
<!--
 shareInterpolator:里面动画效果是否共享一个Interpolator
 不共享设置android:shareInterpolator="false",每一个动画效果自己添加interpolator。
 -->
</set>  

代码实现:

AlphaAnimation alpha=new AlphaAnimation(0, 1);
alpha.setDuration(1000);
AnimationSet animSet = new AnimationSet(false);
animSet.addAnimation(alpha);

2.2 LayoutAnimation(GridLayoutAnimationController)

xml定义:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="0.5"
    android:animationOrder="normal"
    android:animation="@anim/alpha"
    />
<!--
 delay:延迟多少开始播放
 -->

代码实现:

AlphaAnimation alpha=new AlphaAnimation(0, 1);
alpha.setDuration(1000);
LayoutAnimationController lac=new LayoutAnimationController(alpha);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
ListView.setLayoutAnimation(lac);
LayoutAnimationController.ORDER_NORMAL;    //顺序显示
LayoutAnimationController.ORDER_REVERSE;   //反显示
LayoutAnimationController.ORDER_RANDOM     //随机显示

使用:

<ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
 	android:layoutAnimation="@anim/layoutanimation"/>  

2.逐帧动画 Drawable Animation(Frame Animation)

xml定义:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/girl1" android:duration="100" />
    <item android:drawable="@drawable/girl2" android:duration="100" />
    <item android:drawable="@drawable/girl3" android:duration="100" />
<!--
 oneshot:如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放
 -->
</animation-list> 

代码实现:

AnimationDrawable anim = new AnimationDrawable();
for (int i = 1; i <= 3; i++) {
    int id = getResources().getIdentifier("girl" + i, "drawable", getPackageName());
    Drawable drawable = getResources().getDrawable(id);
    anim.addFrame(drawable, 100);
} 

播放:

View.setBackgroundResource(R.anim.frame);
AnimationDrawable anim = (AnimationDrawable) image.getBackground();
anim.start();
注:想activity一开始就播放请放在onWindowFocusChanged方法里

时间: 2024-08-11 02:44:43

Android 动画总结的相关文章

android动画具体解释六 XML中定义动画

动画View 属性动画系统同意动画View对象并提供非常多比view动画系统更高级的功能.view动画系统通过改变绘制方式来变换View对象,view动画是被view的容器所处理的,由于View本身没有要操控的属性.结果就是View被动画了.但View对象本身并没有变化. 在Android3.0中,新的属性和对应的getter和setter方法被增加以克服此缺点. 属性动画系统能够通过改变View对象的真实属性来动画Views. 并且.View也会在其属性改变时自己主动调用invalidate(

Android开发艺术探索——第七章:Android动画深入分析

Android开发艺术探索--第七章:Android动画深入分析 Android的动画可以分成三种,view动画,帧动画,还有属性动画,其实帧动画也是属于view动画的一种,,只不过他和传统的平移之类的动画不太一样的是表现形式上有点不一样,view动画是通过对场景的不断图像交换而产生的动画效果,而帧动画就是播放一大段图片,很显然,图片多了会OOM,属性动画通过动态的改变对象的属性达到动画效果,也是api11的新特性,在低版本无法使用属性动画,但是我们依旧有一些兼容库,OK,我们还是继续来看下详细

android 动画(1) 补间动画

android动画: 3.0以前,android支持两种动画模式,tween animation,frame animation, 3.0中又引入了一个新的动画系统:property animation, 这三种动画模式在SDK中被称为 property animation,        属性动画: view animation,   补间动画:  给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变. (Tween animation) drawable animatio

android动画详解三 动画API概述

· 属性动画与view动画的不同之处 view动画系统提供了仅动画View 对象的能力,所以如果你想动画非View 对象,你就要自己实现代码. view动画系统实际上还被强制仅能对 View 的少数属性进行动画,比如缩放和旋转,而不能对背景色进行. view动画的另一个坏处是它仅修改View的绘制位置,而不是View的实际位置.例如,如果你动画一个移动穿越屏幕,button的绘制位置是正确的,但实际你可以点击它的位置却没有变,所以你必须去实现你自己的逻辑来处理它. 使用属性动画系统时,这个限制被

Android动画基础

Android动画主要有三种: 1> 视图动画,也叫Tween(补间)动画可以在一个视图容器内执行一系列简单变换(位置.大小.旋转.透明度).譬如,如果你有一个TextView对象,您可以移动.旋转.缩放.透明度设置其文本,当然,如果它有一个背景图像,背景图像会随着文本变化. 补间动画通过XML或Android代码定义,建议使用XML文件定义,因为它更具可读性.可重用性. 2> Drawable动画其实就是Frame动画(帧动画),它允许你实现像播放幻灯片一样的效果,这种动画的实质其实是Dra

Android动画效果——1.帧动画2.补间动画3.跳转画面(三)

Android--动画效果1.帧动画2.补间动画3.跳转画面 插值器类 xml属性值 说明 LinearInterpolator @android:anim/linear_interpolatorr 动画以均匀的速度改变. AccelerateInterpolator @android:anim/accelerate_interpolator 在动画开始时改变速度较慢,然后开始加速. AccelerateDecelerateInterpolator @android:anim/accelerat

Android动画AnimationSet遇到的问题。

之前对Android动画这块一直是一知半解,知道个大概,并不会使用.刚好这几天没有太多的任务要做,可以梳理一下Android动画的一些知识.Android Animation的基础用法就不说了,这里主要记录下简单实用中遇到的问题. 1.XML中AnimationSet的某些属性有些问题. 主要就是android:repeatCount,android:repeatMode无效.这个问题据说是Google的工程师刻意为之.[参考:http://stackoverflow.com/questions

Android 动画详解

这次主要就介绍android动画,android动画目前分为三种形式,Tween Animation 这个只能应用于view对象上面的,Drawable Animation这个是帧动画,就是类似我们有一些列的图片依次播放图片时出现的动画,Property Animation 这个是属性动画,这也是在android3.0之后引进的动画,在手机的版本上是android4.0就可以使用这个动画,下面我们主要就是针对这三种情况进行介绍. Tween Animation 这个动画在Property Ani

【Android动画】之Tween动画 (渐变、缩放、位移、旋转)

Android 平台提供了两类动画. 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转.平移.放缩和渐变). 第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似. 下面就讲一下Tweene Animations. 主要类: Animation  动画 AlphaAnimation 渐变透明度 RotateAnimation 画面旋转 ScaleAnimation 渐变尺寸缩放 TranslateAnimation 位置移动 Animatio

Android动画学习(缓动动画与属性动画的区别)

前言: 在 Android动画学习(概述)中,如果你看过这篇帖子,你应该会对缓动动画和属性动画之间的区别产生疑问,因为在它们的应用中,你会感觉这两种动画有着一些相似的地方,为此,我打算把这两种动画之间的区别做一下说明 区别: 在这里先附上官方文档对于这两种动画的区别说明(我所说的缓动动画对应在下文中的英文为:View Animation,属性动画对应在下文中的英文为:Property Animation): How Property Animation Differs from View Ani