【转】Android:Animation的简单学习--不错
原文网址:http://blog.csdn.net/huangbiao86/article/details/6683665
Animation动画效果。提供了一系列的动画效果,可以应用大多数 的控件。
一、Animations从总体上来说可以分为两大类:
1、TweenedAnimations:该类提供了旋转,移动,伸展,和淡出竺效果;
2、Frame-by-FrameAmimations:这一类可以创建一个Drawable序列:这些Drawable可以按照指定的时间间歇一个一个的显示。
二、TwenedAnimations的分类
a) Alpha:淡入淡出效果
b) Scale:缩放效果
c) Rotate:旋转效果
d) Translate:移动效果
这四种动画效果对应四个不同的类,都有不同的参数,但是这四个不同的动画效果有共同的参数:下面简单介绍下面几种:
setDuration(long durationMills)设置动画持续时间,单位毫秒 ;
setFillAfter(boolean fillAfter)如果为true的话,动画执行后,控件停留在执行结束的状态;
setFillBefore(boolean fillBefore)
setStartOffSet(long startOffSet)设置动画执行之前等待时间;
setRepeatCount(int repeatCount)设置动画重复的次数
下面简单介绍一下,TewnedAnimation的使用步骤:
1、创建一个AnimationSet,存放动画集合,也可以只有一个动画。
2、根据需要创建对应的Animation
3、根据动画的需求,为Animation创建相对应的数据。
4、将Animation对象添加到AnimationSet
5、使用控件开始执行Animation:textView.startAnimation(AnimateionSet)当然里面的参数也可以是Animate,由于Animation是AnimationSet的父类,所以不会出错
我们来做一个简单的例子,了解Animation的用法 :
1、 在main.xml文件里面添加四个按钮控件,和一个图片控件,用来进行测试,图片用于显示动画效果;
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ImageView android:id="@+id/image"
- android:src="@drawable/icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="100dip"/>
- <Button android:id="@+id/alpha"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/alpha"/>
- <Button android:id="@+id/scale"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/scale"/>
- <Button android:id="@+id/rotate"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/rotate"/>
- <Button android:id="@+id/translate"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/translate"/>
- </LinearLayout>
2、编写Activity类,添加监听:并且在对应的监听事件上 加上动画效果(按上面介绍了的步骤进行)
- public class HelloAnimationActivity extends Activity {
- /** Called when the activity is first created. */
- Button b_alpha;
- Button b_scale;
- Button b_rotate;
- Button b_translate;
- ImageView iv;
- AnimationSet animationSet;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- iv = (ImageView)findViewById(R.id.image);
- b_alpha = (Button)findViewById(R.id.alpha);
- b_scale = (Button)findViewById(R.id.scale);
- b_rotate = (Button)findViewById(R.id.rotate);
- b_translate = (Button)findViewById(R.id.translate);
- b_alpha.setOnClickListener(new AlpahListener());
- b_scale.setOnClickListener(new ScaleListener());
- b_rotate.setOnClickListener(new RotateListener());
- b_translate.setOnClickListener(new TranslateListener());
- }
- class AlpahListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- //创建一个AnimationSet对象
- AnimationSet animationSet = new AnimationSet(true);
- //创建一个AlphaAnimation对象
- AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
- //设置动画执行的时间(单位:毫秒)
- alphaAnimation.setDuration(1000);
- //将AlphaAnimation对象添加到AnimationSet当中
- animationSet.addAnimation(alphaAnimation);
- //使用ImageView的startAnimation方法开始执行动画
- iv.startAnimation(animationSet);
- }
- }
- class ScaleListener implements OnClickListener{
- /**
- * 从大到小的动画,x轴1到0.1,y轴1到0.1。中心为图片的中心(0.5f,0.5f)
- */
- @Override
- public void onClick(View arg0) {
- AnimationSet animationSet = new AnimationSet(true);
- ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0.5f);
- animationSet.addAnimation(scaleAnimation);
- animationSet.setStartOffset(1000);
- animationSet.setFillAfter(true);
- animationSet.setFillBefore(false);
- animationSet.setDuration(2000);
- iv.startAnimation(animationSet);
- }
- }
- class RotateListener implements OnClickListener{
- /**
- * 以图片中心旋转360度;(0.5f,0.5f)表示图片的中心
- */
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- animationSet = new AnimationSet(true);
- RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0.5f);
- rotateAnimation.setDuration(5000);
- animationSet.addAnimation(rotateAnimation);
- iv.setAnimation(rotateAnimation);
- }
- }
- class TranslateListener implements OnClickListener{
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- animationSet = new AnimationSet(true);
- TranslateAnimation translateAnimation = new TranslateAnimation(
- Animation.RELATIVE_TO_SELF, 0f,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0f,
- Animation.RELATIVE_TO_SELF, 1.0f);
- animationSet.addAnimation(translateAnimation);
- iv.setAnimation(animationSet);
- }
- }
- }
下面来看看Animation的第二种用法 :
这种方法跟上一种差不多,只不过是将动画的布局,在xml文件里配置,配置动画;这样做的好处是一个动画配置可以重复应用,这种方法在面向对象的语言中是很注重的;
在上一例子的基础上,在res目录下新建目录anim;在下面新建xml文件,就做为Animation的布局文件。
xml文件编写方法:
1、首先加入set标签。<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
2、再在该标签当中加入rotate,alpha,scale,translate标签;
在代码中用AnimationUtils静态函数,加载动画xml文件。
如:
- <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
- <scale
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="1.0"
- android:toXScale="1.4"
- android:fromYScale="1.0"
- android:toYScale="0.6"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillAfter="false"
- android:duration="1000" />
- <set android:interpolator="@android:anim/decelerate_interpolator">
- <scale
- android:fromXScale="1.4"
- android:toXScale="0.0"
- android:fromYScale="0.6"
- android:toYScale="0.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:startOffset="700"
- android:duration="400"
- android:fillBefore="false" />
- <rotate
- android:fromDegrees="0"
- android:toDegrees="50"
- android:toYScale="1.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:startOffset="700"
- android:duration="1400" />
- </set>
- </set>
上面在一个xml文件中定义了 一系列的动画效果,注意:
android:pivotX="50"这种方法使用绝对位置定位,相当于代码中的 Animation.ABSOLUTE
android:pivotX="50%"这种方法相对于控件本身定位,相当于代码中的 Animation.RELATIVE_TO_SELF
android:pivotX="50%p" 这种方法相对于控件 的父控件定位,相当于代码中上 Animation.RELATIVE_TO_PARENT
如果需要调用 这个动画效果到某个控件 ,可以用下面的方法,给图片添加上面的一系列的动画效果,如果只希望有一个动画效果 ,那么也是一个xml文件:
- class AlpahListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- Animation animation = AnimationUtils.loadAnimation(HelloAnimationActivity.this, R.anim.animation_test);
- iv.startAnimation(animation);
- }
- }
这只是一段监听器代码。
另外,如果我们直接在xml文件里面,直接加两个或多个效果:如下面:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:shareInterpolator="true">
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0.0"
- android:startOffset="500"
- android:duration="2000" />
- <rotate android:fromDegrees="0"
- android:toDegrees="360"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="2000" />
- </set>
这时候,如果给控件添加这个动画效果,那么这个xml文件里面的alpha和rotate会叠加,而且这两个动画效果共享@android:anim/accelerate_interpolator.都是逐渐加速;如果需要单个设置速率就需要在每个效果里添加属性
- android:interpolator="@android:anim/accelerate_interpolator"
Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:
| AccelerateDecelerateInterpolator | 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速 |
| AccelerateInterpolator | 在动画开始的地方速率改变比较慢,然后开始加速 |
| CycleInterpolator | 动画循环播放特定的次数,速率改变沿着正弦曲线 |
| DecelerateInterpolator | 在动画开始的地方速率改变比较慢,然后开始减速 |
| LinearInterpolator | 在动画的以均匀的速率改变 |
Frame-by-FrameAmimations这一类可以创建一个Drawable序列:这些Drawable可以按照指定的时间间歇一个一个的显示,就像win7桌面背景图片切换一样,循环切换;
使用方法:定义资源文件xml:
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true">
- <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
- <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
- <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
- </animation-list>
其中item是轮流切换的图片;
在代码中使用资源文件:
- AnimationDrawable rocketAnimation;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
- rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
- rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
- }
- public boolean onTouchEvent(MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- rocketAnimation.start();
- return true;
- }
- return super.onTouchEvent(event);
- }
如果我们要给每一个动画添加监听器:如下面代码 :
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
- private Button removeButton = null;
- private Button addButton = null;
- private ImageView imageView = null;
- private ViewGroup viewGroup = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- removeButton = (Button)findViewById(R.id.removeButtonId);
- imageView = (ImageView)findViewById(R.id.imageViewId);
- removeButton.setOnClickListener(new RemoveButtonListener());
- viewGroup = (ViewGroup)findViewById(R.id.layoutId);//这里viewGroup是LinearLayout的id,这个viewGroup包括了下面所有的控件。
- addButton = (Button)findViewById(R.id.addButtonId);
- addButton.setOnClickListener(new AddButtonListener());
- }
- private class AddButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- //创建了一个淡入效果的Animation对象
- AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);
- animation.setDuration(1000);
- animation.setStartOffset(500);
- //创建一个新的ImageView
- ImageView imageViewAdd = new ImageView(MainActivity.this);
- imageViewAdd.setImageResource(R.drawable.icon);
- //将新的ImageView添加到viewGroup当中
- viewGroup.addView(imageViewAdd, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- //启动动画
- imageViewAdd.startAnimation(animation);
- }
- }
- private class RemoveButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- //创建一个淡出效果的Animation对象
- AlphaAnimation animation = new AlphaAnimation(1.0f,0.0f);
- //为Animation对象设置属性
- animation.setDuration(1000);
- animation.setStartOffset(500);
- //为Animation对象设置监听器
- animation.setAnimationListener(new RemoveAnimationListener());
- imageView.startAnimation(animation);
- }
- }
- private class RemoveAnimationListener implements AnimationListener{
- //该方法在淡出效果执行结束之后被调用
- @Override
- public void onAnimationEnd(Animation animation) {
- System.out.println("end");
- //从viewGroup当中删除掉imageView控件
- viewGroup.removeView(imageView);
- }
- //该方法在动画重复执行的时候调用
- @Override
- public void onAnimationRepeat(Animation animation) {
- System.out.println("repeat");
- }
- //该方法在动画开始执行的时候,调用 。
- @Override
- public void onAnimationStart(Animation animation) {
- System.out.println("start");
- }
- }
- }
在实际应用中,后者可能会多一些,但是由于写xml的时候,编译器很多地方不会报错,所以维护也是一个问题;
【转】Android:Animation的简单学习--不错的更多相关文章
- Android greenDAO 数据库 简单学习之基本使用
看网上对greenDAO介绍的不错,今天就动手来试一把,看看好不好使. greenDAO 官方网站:http://greendao-orm.com/ 代码托管地址:https://github.com ...
- Android Studio IDE 简单学习和介绍
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 38.Android之ListView简单学习(一)
android中ListView用的很普遍,今天来学习下,本篇主要以本地数据加载到listview,后面会学习从网络获取数据添加到listview. 首先改下布局文件: <?xml versio ...
- Android Animation学习(六) View Animation介绍
Android Animation学习(六) View Animation介绍 View Animation View animation系统可以用来执行View上的Tween animation和F ...
- Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition
Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition Property animation系统还提供了对ViewGroup中的View改变 ...
- Android动画学习笔记-Android Animation
Android动画学习笔记-Android Animation 3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...
- Android Animation学习 实现 IOS 滤镜退出动画
IOS的用户体验做的很好,其中一点很重要的地方就是动画效果. 最近在学习Android的Animation,简单实现了一个IOS相机滤镜退出的动画: 布局文件:activity_animation_d ...
- Android Animation学习(四) ApiDemos解析:多属性动画
Android Animation学习(四) ApiDemos解析:多属性动画 如果想同时改变多个属性,根据前面所学的,比较显而易见的一种思路是构造多个对象Animator , ( Animator可 ...
- Android Animation学习(三) ApiDemos解析:XML动画文件的使用
Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...
随机推荐
- @property在内存管理中的参数问题
// // Created by wanghy on 15/8/14. // // /* retain : release旧值,retain新值(用于OC对象),要配合nonatomic使用. ass ...
- leetcode344——Reverse String(C++)
Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...
- 深度优化LNMP之Nginx [2]
深度优化LNMP之Nginx [2] 配置Nginx gzip 压缩实现性能优化 1.Nginx gzip压缩功能介绍 Nginx gzuo压缩模块提供了压缩文件内容的功能,用户请求 ...
- ubuntu 安装qq
受不了webqq那个界面 ,各种不习惯 .今天在ubuntu 12.04LTS 版本中 ,终于装上了qq2012,下面介绍一下安装方法 1 安装 wine sudo apt-get install ...
- MySQL 查询某时间段范围内的数据 补零
1.创建基础表 CREATE TABLE num (i INT); INSERT INTO num (i) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9) ...
- Android Activity 生命周期详解
学习android开发这么久对于activity的生命周期还没有仔细思考过,所以,我大致的把这些东西整理一下,希望通过这使自己理解的更透彻点吧! 首先看一下Activity生命周期图和它的的四个阶段 ...
- 对React的理解
转自:http://www.cocoachina.com/webapp/20150721/12692.html 现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了R ...
- Gitlab服务器搭建(For fedora23)
1. Install and configure the necessary dependencies sudo yum install curl policycoreutils openssh-se ...
- JSON知多少-JSON数据结构
最近在开发微信平台,要使用JSON进行数据交换,之前用过JSON,但仅限于…… 在开发微信平台中,要使用JSON形式如下:代码片断1: { "button":[ { "t ...
- [转]Vim 复制粘贴探秘
Vim作为最好用的文本编辑器之一,使用vim来编文档,写代码实在是很惬意的事情.每当学会了vim的一个新功能,就会很大地提高工作效率.有人使用vim几十年,还没有完全掌握vim的功能,这也说明了vim ...