安卓动画之TweenAnimation补间动画详细攻略
2017-01-13 16:06
155 查看
一、简单介绍
补间动画是安卓简单基础的动画,能实现一些基本的平移、缩放、旋转、改变透明度的功能
实现补间动画有两种方式,通过代码或者xml配置文件(建一个anim资源文件夹,放在里面):下面可见
*动画类型 XML配置方式 Java代码实现方式
渐变透明度动画效果 AlphaAnimation
渐变尺寸缩放动画效果 ScaleAnimation
画面旋转动画效果 RotateAnimation
画面位置移动动画效果 TranslateAnimation
组合动画效果 AnimationSet*
二、具体实现
1.透明度
xml资源文件
fromAlpha:开始时透明度
toAlpha: 结束时透明度
duration:动画持续时间
fillAfter:设置动画结束后保持当前的位置
XML方式加载方式通过AnimationUtils.loadAnimation(this, R.anim.anim_alpha)获取Animation
代码实现:
2.伸缩
配置文件
fromXDelta,fromYDelta 起始时X,Y座标,屏幕右下角的座标是X:320,Y:480
toXDelta, toYDelta 动画结束时X,Y的座标
interpolator 指定动画插入器
fromXScale,fromYScale, 动画开始前X,Y的缩放,0.0为不显示, 1.0为正常大小
toXScale,toYScale, 动画最终缩放的倍数, 1.0为正常大小,大于1.0放大
pivotX, pivotY 动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从自身中间开始
startOffset, 动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒
duration,一次动画效果消耗的时间,单位毫秒,值越小动画速度越快
repeatCount,动画重复的计数,动画将会执行该值+1次
repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变
代码实现
3.旋转
代码方式
4.平移
fromXDelta,fromYDelta 起始时X,Y座标,屏幕右下角的座标是X:320,Y:480
toXDelta, toYDelta 动画结束时X,Y的座标
代码实现
5,最重要的组合动画·
xml配置文件方式
代码实现方式:
补间动画是安卓简单基础的动画,能实现一些基本的平移、缩放、旋转、改变透明度的功能
实现补间动画有两种方式,通过代码或者xml配置文件(建一个anim资源文件夹,放在里面):下面可见
*动画类型 XML配置方式 Java代码实现方式
渐变透明度动画效果 AlphaAnimation
渐变尺寸缩放动画效果 ScaleAnimation
画面旋转动画效果 RotateAnimation
画面位置移动动画效果 TranslateAnimation
组合动画效果 AnimationSet*
二、具体实现
1.透明度
xml资源文件
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fillAfter="false" android:fromAlpha="1.0" android:toAlpha="0.0" />
fromAlpha:开始时透明度
toAlpha: 结束时透明度
duration:动画持续时间
fillAfter:设置动画结束后保持当前的位置
XML方式加载方式通过AnimationUtils.loadAnimation(this, R.anim.anim_alpha)获取Animation
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha); imageView.startAnimation(alphaAnimation);
代码实现:
Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f); alphaAnimation.setDuration(500);//设置动画持续时间为500毫秒 alphaAnimation.setFillAfter(false);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置) imageView.startAnimation(alphaAnimation);
2.伸缩
配置文件
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:startOffset="0" android:toXScale="1.5" android:toYScale="1.5" />
fromXDelta,fromYDelta 起始时X,Y座标,屏幕右下角的座标是X:320,Y:480
toXDelta, toYDelta 动画结束时X,Y的座标
interpolator 指定动画插入器
fromXScale,fromYScale, 动画开始前X,Y的缩放,0.0为不显示, 1.0为正常大小
toXScale,toYScale, 动画最终缩放的倍数, 1.0为正常大小,大于1.0放大
pivotX, pivotY 动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从自身中间开始
startOffset, 动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒
duration,一次动画效果消耗的时间,单位毫秒,值越小动画速度越快
repeatCount,动画重复的计数,动画将会执行该值+1次
repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变
代码实现
Animation scaleAnimation = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(500);//设置动画持续时间为500毫秒 scaleAnimation.setFillAfter(true);//如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态 scaleAnimation.setFillBefore(false);//如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态 scaleAnimation.setRepeatCount(3);//设置动画循环次数 scaleAnimation.setRepeatMode(Animation.REVERSE); scaleAnimation.setStartOffset(0); scaleAnimation.setInterpolator(this, android.R.anim.decelerate_interpolator);//设置动画插入器 imageView.startAnimation(scaleAnimation);
3.旋转
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromDegrees="0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-360" />
代码方式
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(500); rotateAnimation.setFillAfter(true); rotateAnimation.setInterpolator(this, android.R.anim.accelerate_decelerate_interpolator);//设置动画插入器 imageView.startAnimation(rotateAnimation);
4.平移
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromXDelta="100" android:fromYDelta="0" android:interpolator="@android:anim/cycle_interpolator" android:toXDelta="0" android:toYDelta="0" />
fromXDelta,fromYDelta 起始时X,Y座标,屏幕右下角的座标是X:320,Y:480
toXDelta, toYDelta 动画结束时X,Y的座标
代码实现
Animation translateAnimation = new TranslateAnimation(0, 100, 0, 0); translateAnimation.setDuration(500); translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);//设置动画插入器 translateAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置) imageView.startAnimation(translateAnimation);
5,最重要的组合动画·
xml配置文件方式
AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set); imageView.startAnimation(animationSet);
代码实现方式:
AnimationSet animationSet = new AnimationSet(true); Animation alphaAnimation = new AlphaAnimation(1.0f, 0.1f); alphaAnimation.setDuration(500);//设置动画持续时间为500毫秒 Animation scaleAnimation = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(500);//设置动画持续时间为500毫秒 scaleAnimation.setRepeatMode(Animation.REVERSE); scaleAnimation.setStartOffset(0); scaleAnimation.setInterpolator(this, android.R.anim.decelerate_interpolator); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(scaleAnimation); imageView.startAnimation(animationSet);
相关文章推荐
- 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
- 玩转手机高级篇——最详细的安卓手机开机动画制作教程
- Android 属性动画:这是一篇很详细的 属性动画 总结&攻略
- 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
- 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
- Android 属性动画:这是一篇很详细的 属性动画 总结&攻略
- 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
- 安卓手机修改hosts攻略-摘自网络
- Android-屏幕适配全攻略(绝对详细)(一)
- 发布安全Web站点详细攻略:ISA2006系列之十四
- 安卓学习笔记---Android仿美团加载数据、小人奔跑进度动画对话框(以及顺丰快递员奔跑效果)
- Android:这是一份很详细的Socket使用攻略
- 安卓动画代码和布局
- 安卓属性动画之插值器(TimeInterpolator)和估值器(TypeEvaluator)
- 属性动画详细解析
- 安卓动画总结【非原创】
- 安卓动画——让控件停留在动画结束的位置
- 【Android UI设计和开发】动画(Animation)详细说明(一)
- 工作笔记之安卓开机动画与logo修改
- 安卓App开场图片动画的实现