补间动画(Tween Animation)

补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧之间补充渐变的动画效果来实现的。补间动画的优点是可以节省空间。 目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类库中。

AlphaAnimation:透明度(alpha)渐变效果,对应<alpha/>标签。

TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应<translate/>标签。

ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应<scale/>标签。

RotateAnimation:旋转渐变,可以指定旋转的参考点,对应<rotate/>标签。

AnimationSet:组合渐变,支持组合多种渐变效果,对应<set/>标签。

补间动画的效果同样可以使用XML语言来定义,这些动画模板文件通常会被放在Android项目的res/anim/目录下。

下面是具体实现源码:

MainActivity.java封装实现的方法:

package com.example.lesson19_tween;

import com.example.lesson19_tween.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView1);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} // 透明动画
public void alphaImpl(View v) { Animation animation = AnimationUtils.loadAnimation(this,
R.anim.alpha_demo);
imageView.startAnimation(animation);
} // 旋转动画
public void rotateImpl(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.rotate_demo);
imageView.startAnimation(animation);
} // 缩放动画
public void scaleImpl(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.scale_demo);
imageView.startAnimation(animation);
} // 移动效果
public void translateImpl(View v) {
// XML文件
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.translate_demo); animation.setRepeatCount(Animation.INFINITE);//循环显示
imageView.startAnimation(animation); /*
* 第一种 imageView.setAnimation(animation); animation.start();
*/
// 第二种 // Java代码
/*
* TranslateAnimation translateAnimation = new TranslateAnimation(0,
* 200, 0, 0); translateAnimation.setDuration(2000);
* imageView.startAnimation(translateAnimation);
*/
} // 综合实现set_demo.xml中的动画
public void setAll(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.set_demo);
imageView.startAnimation(animation);
} }

alpha_demo.xml

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

rotate_demo.xml

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"/>
<!--
fromDegrees:表示旋转的起始角度
toDegrees:表示旋转的结束角度
repeatCount:旋转的次数 默认值是0 代表旋转1次 如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止
repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。
repeatCount=-1 或者infinite 循环了
还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。
-->

scale_demo.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.2"
android:toXScale="1.5"
android:fromYScale="0.2"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/> <!--
fromXScale:表示沿着x轴缩放的起始比例
toXScale:表示沿着x轴缩放的结束比例 fromYScale:表示沿着y轴缩放的起始比例
toYScale:表示沿着y轴缩放的结束比例 图片中心点:
android:pivotX="50%"
android:pivotY="50%" -->

translate_demo.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="320"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"/> <!--
android:interpolator 动画的渲染器
1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速
2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速
3、accelerate_decelerate_interpolator(动画加速减速器)
中间位置分层: 使动画在开始的时候 最慢,然后逐渐加速
使动画在开始的时候 最快,然后逐渐减速 结束的位置最慢
fromXDelta 动画起始位置的横坐标
toXDelta 动画起结束位置的横坐标
fromYDelta 动画起始位置的纵坐标
toYDelta 动画结束位置的纵坐标
duration 动画的持续时间
-->

set_demo.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true" > <scale
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" /> <rotate
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" /> <translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" /> <alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" /> </set>

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="15dp"
android:onClick="translateImpl"
android:text="@string/text_translate" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="21dp"
android:onClick="rotateImpl"
android:layout_toRightOf="@+id/imageView1"
android:text="@string/text_rotate" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="32dp"
android:onClick="scaleImpl"
android:text="@string/text_scale" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:onClick="alphaImpl"
android:text="@string/text_alpha" /> <Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true"
android:onClick="setAll"
android:text="@string/text_set" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button5"
android:layout_marginTop="48dp"
android:layout_toRightOf="@+id/button3"
android:src="@drawable/ic_launcher" /> </RelativeLayout>

效果如下:

在实际项目中,我们经常使用补间动画,原因是补间动画使用起来比较方便,功能也比逐帧动画强大不少,而且还可以很方便地进行动画叠加,实现更加复杂的效果。实际上,set_demo.xml中的<set/>标签对应的就是AnimationSet类,即“动画集合”的概念,支持加入多种动画效果,如渐变动画(alpha)、大小动画(scale),线性动画(translate)等。另外,在Android系统中,所有与动画相关的类都归类在android.view.animation包之下,大家可以参考SDK文档进行进一步学习。

至此,我们已经初步了解了如何在Android系统中使用各种动画效果,包括逐帧动画和补间动画。显而易见的是,在Android平台之上,开发者们可以很方便地使用各种动画效果来为应用产品增色。

 
 

Android开发(26)--补间动画(Tween)的实现的更多相关文章

  1. Android开发之补间动画、XML方式定义补间动画

    四种补间动画: 1.透明: 2.缩放: 3.位移: 4.旋转: //点击按钮 实现iv 透明的效果 动画 public void click1(View v) { //1.0意味着着完全不透明 0.0 ...

  2. Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)

    1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...

  3. Android 深入ViewPager补间动画,实现类京东商城首页广告Banner切换效果

    如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 某天看到京东商城首页的滑动广告的Banner,在流动切换的时候有立体的动画效果,感觉很有意思,然后研究了下 ...

  4. Android开发UI之补间动画-Tween Animation

    Tween Animation-补间动画 官网链接-http://developer.android.com/reference/android/view/animation/Animation.ht ...

  5. android 帧动画,补间动画,属性动画的简单总结

      帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...

  6. Android基础笔记(十)- 帧动画、补间动画具体解释、对话框

    帧动画 补间动画Tween Animation 对话框以及面试中的注意点 帧动画 帧动画非常easy,我们首先看一下Google官方解释This is a traditional animation ...

  7. 【Android - 进阶】之Animation补间动画

    补间动画也叫View动画,它只能针对View进行动画操作,且补间动画操作的只是View中可见的部分,即只操作界面,对于可点击区域等都不会进行操作. 在Android中,补间动画的顶级类是Animati ...

  8. Android--Tween补间动画

    前言 Android除了支持逐帧动画之外,也提供了对补间动画的支持,补间动画就是指开发人员只需要指定动画的开始.动画结束的"关键帧",而动画变化的"中间帧"由系 ...

  9. android104 帧动画,补间动画,属性动画

    ##帧动画FrameAnimation* 多张图片快速切换,形成动画效果* 帧动画使用xml定义 package com.itheima.frameanimation; import android. ...

随机推荐

  1. Hibernate之dynamic-update

    问题:设置了dynamic-update, 可是事实上并没有按照期望进行了update. 案例代码如下: 1.持久化对象 package com.jdw.hibernate.entities; imp ...

  2. C++中的引用到底是什么

    这也算是一个老生常谈的问题,写这个其实就是想趁着暑假把博客丰富一下. 咱随便在谷哥.度娘.病软引擎上搜搜都可以得到各种关于引用的解释,无非就是"引用不同于指针,引用是一个变量的别名" ...

  3. 对web应用中单一入口模式的理解及php实现

    在我们web应用的开发中,经常会听见或看见单一入口模式,在我开始学习tp框架的时候也不理解为什么要运用一个单一入口模式,只是会使用,最近自己在搞一个小东西的时候才明白为什么在web开发中要运用单一入口 ...

  4. [问题解决] Could not update ICEauthority file /home/username/.ICEauthority

    错误: Could not update ICEauthority file /home/username/.ICEauthority 发生场景: 虚拟机下的ubuntu server12.04 解决 ...

  5. Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)

    因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...

  6. Android 获取屏幕分辨率

    原文:Android 获取屏幕分辨率 得到一个屏幕尺寸的三种方法如下:        // 通过WindowManager获取        DisplayMetrics dm = new Displ ...

  7. ProcessBuilder 和 Runtime(转)

    ProcessBuilder.start() 和 Runtime.exec() 方法都被用来创建一个操作系统进程(执行命令行操作),并返回 Process 子类的一个实例,该实例可用来控制进程状态并获 ...

  8. Android 天天爱消除辅助

    简介 <天天爱消除>是一款移植于手游的消除类益智游戏,该游戏只有通过手机登录QQ跟微信才能进行,这样一来这款游戏必然会大红大紫. 功能 开发Android自动化触屏事件,录制操作脚本,实现 ...

  9. 轻轻谈一下seaJs——模块化开发的利器

    "仅做一件事,做好一件事." 这个应该就是seaJs的精髓了. 我在自己的一些项目中使用过seaJs.对其算是了解一二.如今就班门弄斧.轻轻地谈一下. 首先上一段度娘的话: &qu ...

  10. springMvc 支持hibernate validator

    SpringMVC 支持Hibernate Validator 发表于9个月前(2014-08-04 11:34)   阅读(1780) | 评论(0) 11人收藏此文章, 我要收藏 赞0 5月23日 ...