动画的简单介绍
2013-11-08 15:04
891 查看
一、帧动画
res/drawable/frame_animaiton.xml
duration的含义是:该图片播放持续的时间
Animation.java
imageView.getBackground下面就很有可能播放不出来了。原因是主线程执行ad.start()的方法优先于另外的一个线程执行getBackground加载图片。
二、Tween动画
(1)透明度
AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f); //从透明到不透明
animation1.setDuration(2000); //动画显示的时间
animation1.setRepeatCount(1); //重复一次,一共播放两次
animation1.setRepeatMode(Animation.REVERSE); //重复的动作为反转
animation1.setFillAfter(true); //停止在最后的位置
imageView.startAnimation(animation1);
(2)缩放
ScaleAnimation animation2 = new ScaleAnimation(0.5f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//参数的含义:X轴从50%-->200% Y轴从20%-->200% ,缩放点是相对于自身的50%,即中点
animation2.setDuration(2000);
animation2.setRepeatCount(1);
animation2.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation2);
(3)旋转
(4)平移
TranslateAnimation animation4 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
//相对于父窗体,X的起始位置是0(即图标当前所在的位置),X的移动距离是半个屏幕的像素
//相对于父窗体,Y的起始位置是0.5f(即图标当前所在的位置往下半个屏幕为起点),Y的移动距离是半个屏幕的像素
animation4.setDuration(2000);
animation4.setRepeatCount(1);
animation4.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation4);
(5)动画组合的效果
AnimationSet set = new AnimationSet(false);
RotateAnimation animation5 = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation5.setDuration(2000);
animation5.setRepeatCount(1);
animation5.setRepeatMode(Animation.REVERSE);
TranslateAnimation animation6 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
animation6.setDuration(2000);
animation6.setRepeatCount(1);
animation6.setRepeatMode(Animation.REVERSE);
set.addAnimation(animation5);
set.addAnimation(animation6);
imageView.startAnimation(set);
三、XML定义Tween动画
(1)res/anim/translate_animation
fillAfter的意思是保持为动画最后的样子
(2)res/anim/set_animation
res/drawable/frame_animaiton.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/a1" android:duration="1000"/> <item android:drawable="@drawable/a2" android:duration="1000"/> <item android:drawable="@drawable/a3" android:duration="1000"/> <item android:drawable="@drawable/a4" android:duration="1000"/> </animation-list></span>oneshot的含义是:动画是否只播放一次
duration的含义是:该图片播放持续的时间
Animation.java
<span style="font-size:18px;">public class MainActivity extends Activity { private ImageView imageView; private Button button; private AnimationDrawable ad; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); button = (Button) findViewById(R.id.button); imageView.setBackgroundResource(R.drawable.frame_animation); ad = (AnimationDrawable) imageView.getBackground(); //获得AnimationDraable对象 button.setOnClickListener(new OnClickListener() { public void onClick(View v) { ad.start(); //动画开始播放 } }); } }</span>还有一点值得注意的是,getBackground这个方式异步的,它会在新的线程上运行,所以如果把ad.start()直接放在
imageView.getBackground下面就很有可能播放不出来了。原因是主线程执行ad.start()的方法优先于另外的一个线程执行getBackground加载图片。
二、Tween动画
(1)透明度
AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f); //从透明到不透明
animation1.setDuration(2000); //动画显示的时间
animation1.setRepeatCount(1); //重复一次,一共播放两次
animation1.setRepeatMode(Animation.REVERSE); //重复的动作为反转
animation1.setFillAfter(true); //停止在最后的位置
imageView.startAnimation(animation1);
(2)缩放
ScaleAnimation animation2 = new ScaleAnimation(0.5f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//参数的含义:X轴从50%-->200% Y轴从20%-->200% ,缩放点是相对于自身的50%,即中点
animation2.setDuration(2000);
animation2.setRepeatCount(1);
animation2.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation2);
(3)旋转
<span style="font-size:18px;">RotateAnimation animation3 = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //从0度旋转到180度,旋转的轴心是相对于自身的50%,即中心 animation3.setDuration(2000); animation3.setRepeatCount(1); animation3.setRepeatMode(Animation.REVERSE); </span><pre name="code" class="java">imageView.startAnimation(animation3);
(4)平移
TranslateAnimation animation4 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
//相对于父窗体,X的起始位置是0(即图标当前所在的位置),X的移动距离是半个屏幕的像素
//相对于父窗体,Y的起始位置是0.5f(即图标当前所在的位置往下半个屏幕为起点),Y的移动距离是半个屏幕的像素
animation4.setDuration(2000);
animation4.setRepeatCount(1);
animation4.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation4);
(5)动画组合的效果
AnimationSet set = new AnimationSet(false);
RotateAnimation animation5 = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation5.setDuration(2000);
animation5.setRepeatCount(1);
animation5.setRepeatMode(Animation.REVERSE);
TranslateAnimation animation6 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
animation6.setDuration(2000);
animation6.setRepeatCount(1);
animation6.setRepeatMode(Animation.REVERSE);
set.addAnimation(animation5);
set.addAnimation(animation6);
imageView.startAnimation(set);
三、XML定义Tween动画
(1)res/anim/translate_animation
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="20%p" android:toXDelta="80%p" android:fromYDelta="20%p" android:fillAfter="true" android:toYDelta="80%p" android:duration="2000"> </translate></span>20%p的意思是相对于父控件的20%,20%就是相对于自身的20%
fillAfter的意思是保持为动画最后的样子
<span style="font-size:18px;">Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.translate_animation); imageView.startAnimation(animations);</span>
(2)res/anim/set_animation
<span style="font-size:18px;"><set> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXDelta="20%p" android:fromYDelta="20%p" android:toXDelta="80%p" android:toYDelta="80%p" > </translate> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromAlpha="1.0" android:toAlpha="0.2" > </alpha> </set></span>
<span style="font-size:18px;">Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.set_animation); imageView.setAnimation(animation1);</span>
相关文章推荐
- 【cocos2d-x】cocostudio::ColliderDetector 简单介绍 骨骼动画绑定碰撞区域进行碰撞检测
- Android动画--属性动画简单介绍和基本用法(二)
- 【Unity】动画系统简单介绍
- 介绍简单实用APP动画效果的两个关键
- Android 补间动画,具体介绍了透明度、平移,旋转,缩放4种动画效果的实现,简单易懂
- CoreAnimation基础介绍、简单使用CALayer以及多种动画效果
- UIKit基础:14-序列帧动画的简单介绍
- Rebound动画框架简单介绍
- iOS动画简单使用及介绍
- android 动画简单介绍
- Android动画--逐帧动画和补间动画简单介绍和基本用法(一)
- Rebound动画框架简单介绍
- Core Animation基础介绍、简单使用CALayer以及多种动画效果
- ios-序列帧动画核心代码简单介绍以及封装
- 关于Android动画插值器的简单介绍以及如何自定义自己的插值器
- Android 补间动画的简单介绍及使用
- 前端动画渲染引擎pixi.js系列(1)简单介绍及应用
- 网站设计之Flash简单动画入门介绍(一)字体闪烁及渐显
- iOS中的动画-简单介绍
- CSS3动画属性简单介绍