- 浏览: 327008 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
jnssvh:
楼主,还发代码吗?jnssvh@aliyun.com
SurfaceView简单例子 -
hou_anne:
讲解的非常详细
Android TabActivity实现多页显示效果 -
小鱼小鹰:
现在还能吗。。。296252344@qq.com
自定义控件(SurfaceView与view结合) -
flyar:
你好!我按照你的代码实现多点广播,但是在一台机子休眠,另一台手 ...
UDP广播与多播简单实现 -
herber2010:
图有错误~~
java实现快速排序
半小时搞定Android Animation 之一
作者:Legend
QQ:158067568
Android提供了两种动画的机制,可以通过SurfaceView来一帧一帧的绘制,同样也可以通过Animation机制。
Animations分类
Animations分为两种机制:Tweened Animations和Frame-by-FrameAnimations。
Tweened类似于flash,通过旋转、移动、缩放等实现动画效果,而Frame-by-FrameAnimations是类似于电影,通过改变每一帧的显示来实现动画效果。
其实Animation就是先写好对象要发生的动作,比如说平移,变形等。然后你需要哪个View或者View的子类,来完成这些预定义好的动作,只要给他们添加,就可以了。这部分的内容还是蛮多的,但是相对简单,本节只介绍Tweened Animations。
Tweened Animations
如图1所示,Tweened Animations有如下四种方式实现动画,对于每一种方式,Android都提供了一个类来实现。AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation 。
图1 Tweened Animations四种方式实现动画
对于每一种,笔者都会给出例子来详细解释。出于由简到难的考虑,首先介绍Alpha。
Alpha
alpha这个词详细如果有过opengl 开发经验的朋友一定不会陌生,没错,他就是设置透明度的。让我们先看一组图标,这样你就会加深对alpha的认识。如图2所示,实现了透明度变化的图标和按钮的效果。
图2 颜色渐变demo
如果要实现view的透明度变化,就要去实现alpha 的Animation。具体方法分为如下4部分:
q 实例化AnimationSet对象
q 实例化Animation对象
q 设置Animation对象属性
q 为View添加AnimationSet
这里要强调的就是Animation的构造方法如下:
AlphaAnimation aa = new AlphaAnimation(1, 0);//2.创建需要的animation
相对来说,alpha的构造方法最为简单。其第一个参数是出示时的透明度,0为完全透明,1为不透明。第二个参数为动画结束时的透明度。
aa.setDuration(2000);//设置动画的时间
通过上面这句可以设置动画播放的时间。
具体代码我会在最后给出。
Scale
scale为缩放,在了解Scale之前我们先看看其运行效果。如图3所示。
图3 scale demo
以上是Scale最简单的构造方法:
ScaleAnimation aa = new ScaleAnimation(1, 2, 1, 2);//2.创建需要的animation,这种构造方法是以自身为pivot
q 第1个参数为初始时x的大小,其中1为自身长度,同一2为自身长度的2倍,下同
q 第2个参数为结束动画时x的大小
q 第3个参数为初始时y的大小
q 第4个参数为结束动画时y的大小
如果你以为你已经学会了Scale,那我只能说你是浅尝辄止。下面介绍其另一个构造方法,该方法更加灵活。
ScaleAnimation aa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
q 其四个参数同上,不解释
q 第五个参数是设置枢轴,可以是以自身,亦可以使用父view的,甚至可以谁用绝对数值。这里之后会解释。如果不明白可先忽略。
q 第六个参数是设置x枢轴的坐标。
q 接下来的两个参数同上,是设置y坐标的。
也许这里读者肯定不会明白枢轴的意思,那么我就用上面的方法来构造Animation并运行看看效果。请看图4.
图4 scale Demo2
如图所示,View控件在自身大小变化的同时,位置也在改变。后面的几个参数是设置其初始位置。如果以父view为参考,值为0.5f的话,那么就是当前父View的中间。
读者可能会问到,怎么不是屏幕的中心位置,我认为他是根据布局文件来决定位置的,当前的布局文件为LinearLayout,其设置为向下一次添加内容,如果设置y坐标为0.5f,则Android会认为你是要在当前布局接下来添加内容的位置的y轴中间。
Transfer
如果明白了枢轴的概念,那么Transfer平移就很简单了,如图5所示,为平移的例子。
图5 平移 demo
以上平移的实例化方法如下:
TranslateAnimation aa = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1);
读者结合api,相信理解这个方法应该还是比较容易的,这里就不在赘述,如果有问题,可以给留言。
Rotate
旋转,这个也是比较容易,看演示,如图6所示。
图6 旋转demo
其构造方法如下:
RotateAnimation aa = new RotateAnimation(0, 270, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.2f);
综合
Animation不但可以单独使用,还可以设置多个Animation然后添加到AnimationSet中。在对View调用,在例子中我给出了一个综合的例子,读者自行查看,检测自己是否已经明白这块知识。
代码
本节参考AnimationDemo1例子。
限于本人水平有限,如有不足之处,请多多指正。
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="alpha" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src = "@drawable/icon" > </ImageView> <Button android:text="scale" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/iv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src = "@drawable/icon" > </ImageView> <Button android:text="rotate" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/iv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src = "@drawable/icon" > </ImageView> <Button android:text="transf" android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/iv4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src = "@drawable/icon" > </ImageView> <Button android:text="complex" android:id="@+id/Button05" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <ImageView android:id="@+id/iv5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src = "@drawable/icon" > </ImageView> </LinearLayout> </ScrollView>
package cn.edu.heut.zcl; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; public class DemoActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ Button alphaBut ; Button scaleBut ; Button rotateBut ; Button transfBut ; Button complexBut ; ImageView iv1; ImageView iv2; ImageView iv3; ImageView iv4; ImageView iv5; AnimationSet as ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); alphaBut = (Button)findViewById(R.id.Button01); scaleBut = (Button)findViewById(R.id.Button02); rotateBut = (Button)findViewById(R.id.Button03); transfBut = (Button)findViewById(R.id.Button04); complexBut = (Button)findViewById(R.id.Button05); alphaBut.setOnClickListener(this); scaleBut.setOnClickListener(this); rotateBut.setOnClickListener(this); transfBut.setOnClickListener(this); complexBut.setOnClickListener(this); iv1 = (ImageView)findViewById(R.id.iv1); iv2 = (ImageView)findViewById(R.id.iv2); iv3 = (ImageView)findViewById(R.id.iv3); iv4 = (ImageView)findViewById(R.id.iv4); iv5 = (ImageView)findViewById(R.id.iv5); } @Override public void onClick(View v) { Button b = (Button)v; switch(b.getId()){ case R.id.Button01://alpha alphaAnimation(); break; case R.id.Button02://scale scaleAnimation(); break; case R.id.Button03://rotate rotateAnimation(); break; case R.id.Button04 ://transf transfAnimation(); break; case R.id.Button05 ://complex complexAnimation(); break; } } /** * 综合例子 */ private void complexAnimation() { as = new AnimationSet(true);//1.实例化AnimationSet TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0); ta.setDuration(5000);//设置动画的时间 AlphaAnimation aa = new AlphaAnimation(1, 0.3f);//2.创建需要的animation aa.setDuration(3000); as.addAnimation(ta);//3.将animation加入AnimationSet as.addAnimation(aa);//3.将animation加入AnimationSet as.setFillAfter(true);//最终停止 complexBut.startAnimation(as);//4.开始动画 iv5.startAnimation(as);//开始动画 } /** * 平移 */ private void transfAnimation() { as = new AnimationSet(true);//1.实例化AnimationSet TranslateAnimation aa = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1); aa.setDuration(5000);//设置动画的时间 as.addAnimation(aa);//3.将animation加入AnimationSet transfBut.startAnimation(as);//4.开始动画 iv4.startAnimation(as);//开始动画 } /** * 旋转 */ private void rotateAnimation() { as = new AnimationSet(true);//1.实例化AnimationSet RotateAnimation aa = new RotateAnimation(0, 270, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.2f); aa.setDuration(5000);//设置动画的时间 as.addAnimation(aa);//3.将animation加入AnimationSet rotateBut.startAnimation(as);//4.开始动画 iv3.startAnimation(as);//开始动画 } /** * 变化大小 */ private void scaleAnimation() { as = new AnimationSet(true);//1.实例化AnimationSet // ScaleAnimation aa = new ScaleAnimation(1, 2, 1, 2);//2.创建需要的animation,这种构造方法是以自身为pivot ScaleAnimation aa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); aa.setDuration(7000);//设置动画的时间 as.addAnimation(aa);//3.将animation加入AnimationSet scaleBut.startAnimation(as);//4.开始动画 iv2.startAnimation(as);//开始动画 } /** * 透明度渐变 */ private void alphaAnimation(){ as = new AnimationSet(true);//1.实例化AnimationSet AlphaAnimation aa = new AlphaAnimation(1, 0);//2.创建需要的animation aa.setDuration(2000);//设置动画的时间 as.addAnimation(aa);//3.将animation加入AnimationSet alphaBut.startAnimation(as);//4.开始动画 iv1.startAnimation(as);//开始动画 } }
评论
TranslateAnimation(0, 200, 0, 0),下次在希望这个View从这个地方开始动画,怎么做?求指导。
图片平移动画时,比如从(0,0)点开始到(480,0)点结束【x轴,y轴】,现在想实现能够从另外一边出来(即感觉是一张图片的效果)???
<translate android:fromXDelta="0" android:toXDelta="480"
android:fromYDelta="0" android:toYDelta="0" android:duration="5000" />
发表评论
-
eclipse 快捷键 for macbook (自用备份)
2011-11-06 19:31 1696产看方法被谁调用: cmd + shift + g -
android去除状态栏和下面视图之前的黑线
2011-08-30 11:13 3498<style name=" ... -
sqlite3 常用命令 for android
2011-08-16 10:59 13341)创建数据库文件: ... -
解决Window系统adb shell后中文显示乱码
2011-08-16 10:52 6070Android中使用的是UTF-8字符,而CMD默认字符集 ... -
(转)Eclipse xml formatter setting for android
2011-08-16 10:03 1691Android里使用很多xml 定义layout,我们 ... -
Android TabActivity实现多页显示效果
2011-03-05 01:28 29449TabActivity实现多页显示 ... -
ListView进阶系列之一 内容顺序淡淡显示
2011-03-04 10:06 5900ListView进阶系列之一 内容顺序淡淡显示 listVi ... -
android布局文件中的include
2011-02-27 00:46 8961居然没有发现,在xml文件中可以使用include,我晕 一 ... -
【收集】Android 面试题
2011-02-25 23:42 9812这个是我自己整理的一些面试题,都是在网上找的,非原创。面试前可 ... -
handler异步控制Runnable
2011-02-20 11:03 91651 关于改变Activity中TextView的方法 ... -
Android消息机制入门一
2011-02-19 19:54 2063Android消息机制入门 ... -
自定义控件(SurfaceView与view结合)
2011-02-14 15:59 16106自定义控件(SurfaceView与view结合) 作 ... -
SurfaceView简单例子
2011-02-13 23:53 8312SurfaceView简单例子 作者:Legend ... -
SurfaceView概述
2011-02-13 21:20 16355SurfaceView概述 作者 ... -
Surface概述
2011-02-13 16:18 3233Surface概述 作者:L ... -
Activity之间的相互调用与传递参数
2011-01-05 19:08 11957Activity之间的相互调用与传递参数 作 ... -
SharedPreferenced
2010-12-25 21:49 1948SharedPreferenced ... -
Android中进行单元测试
2010-12-23 13:35 1613Android中进行单元测试 作者:LEGEND ... -
Android 与 MVC
2010-12-23 12:30 3723Android & MVC 作者:LEGEND ... -
android中文件IO
2010-12-23 01:31 3221Android中文件的读写 ...
相关推荐
这是一个动画示例程序 实现淡入淡出, 缩放, 移动, 旋转 具体介绍请参见 http://write.blog.csdn.net/postedit/6406466
Android 中 Tweened animation的实例详解 Tweened animation有四种类型,下面主要介绍Scale类型。 运行效果如下: Android SDK提供了2种方法:直接从XML资源中读取Animation,使用Animation子类的构造函数来初始化...
本文实例讲述了Android编程之Animation动画用法。分享给大家供大家参考,具体如下: Animations 一、Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行...
Animation从总体来说可以分为两类: Tweened Animations:该类提供了旋转,移动,...Frame-By-Frame Animations:该类可以创建一个Drawable序列,这些Drawable可以按照指定的事件间隔一个一个显示,和动画片差不多。
ViewAnimation Tweened Animations学习
这里我们通过代码实现一些滑动翻页的动画效果。 Animation实现动画有两个方式:帧动画(frame-by-frame animation)和补间动画(tweened animation) 本示例通过继承Animation自定义Rotate3D,实现3D翻页效果。效果...
Animation 实现补间动画效果 Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。 Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。
在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(frame-by-frame animation)和补间动画(tweened animation)。逐帧动画的工作原理很...
源码通过属性动画实现了以前Tweened Animation的所有效果,但它还支持自己特有的动画形式,比如backgroundColor,而这是Tweened Animation无法完成的。
动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画...Android动画模式 ...Animation主要有两种动画模式: ...一种是tweened
2:res文件夹里面的多个文件夹的各自介绍 res/anim/ XML文件,它们被编译进逐帧动画(frame by frame animation)或补间动画(tweened animation)对象 res/drawable/ .png、.9.png、.jpg文件,它们被编译进以下的...
这是一个Android的补间动画小实例 ,大家可以进行参考