Animation的使用
来源:互联网 发布:qq三国转职js技能 编辑:程序博客网 时间:2024/06/11 00:58
Android中的动画分为两种
- Frame Animation 帧动画:一张张图进行轮播,像电影一样进行播放
- Tween Animation 补间动画:四种动画模式,不改变动画的属性
- 缩放 scale: 对控件进行缩小,放大处理
- 平移translate:通过X轴和Y轴,对控件的位置进行移动,
- 渐变alpha: 控制控件的透明度,完全透明值为0,完全不透明值为1
- 旋转Rotation:根据某个指定点来使控件进行旋转指定的角度
- 属性动画:补间动画的一种分支,同样具有四中动画模式,与原本补间动画不 同的是,进行属性动画时,会改变控件自身的属性
Android中的动画的使用方式
- 帧动画: 尽量不要在onCreate()方法中调用帧动画,因为在onCreate中调用AnimationDrawable的start方法时,窗口Window对象还没有完全初始化,AnimationDrawable不能完全追加到窗口Window对象。
- 在资源文件drawable中创建一个根标签为animation-list的xml文件
- 在根标签中建立item标签,两个参数
- Drawable: 图片资源
- Duration: 该图片资源存在的时间
- 根据图片资源的数量,来确定创建的item标签的数量,item标签与图片资源是一一对应的关系
- 使用这一个帧动画最好使用Imageview作为载体
- 在java代码中使用.setBackgroundResource方法加载所创建的动画资源进作为载体的控件
- 调用.getBackground()方法并进行强制转换生成AnimationDrawable对象
- 生成的AnimationDrawable调用.start方法即可播放动画
- 生成的AnimationDrawable调用.stop方法即可停止播放动画
- 补间动画
- 补间动画:
具体如何实现:1.)alpha渐变透明度动画效果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)获取AnimationAnimation alphaAnimation = AnimationUtils.loadAnimation(this,R.anim.anim_alpha);imageView.startAnimation(alphaAnimation);
Java代码方式:Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);alphaAnimation.setDuration(500);//设置动画持续时间为500毫秒alphaAnimation.setFillAfter(false);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)imageView.startAnimation(alphaAnimation); - 属性动画:
2.)scale渐变尺寸缩放动画效果
xml方式:
<?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为重新执行,方向不变
Java方式:
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.)rotate画面旋转动画效果
xml方式:
<?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" />
fromDegrees 动画开始时的角度
toDegrees 动画结束时物件的旋转角度,正代表顺时针
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置
Java方式:
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.)translate画面位置移动动画效果
xml方式:
<?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的座标
Java方式:
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.)set组合动画效果
xml方式:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:duration="500" android:fromAlpha="1.0" android:toAlpha="0.0" /> <scale 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" /></set>
如何使用
AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set); imageView.startAnimation(animationSet);
Java方式
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);
动画监听器Animation.AnimationListener:
有时可能我们要在动画的每个周期里面做不同的操作,这时候就要借助动画监听器了
alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { //动画开始时调用 } @Override public void onAnimationEnd(Animation animation) { //动画结束时调用 } @Override public void onAnimationRepeat(Animation animation) { //动画重复时调用 } });
ObjectAnimator 动画的执行类
Duration动画的持续时间,默认300ms
Animator sets:动画集合
AnimatorSet用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
AnimatorInflater用户加载属性动画的xml文件
单个属性动画的执行方法(旋转):
多个属性动画的执行方法
Android中常用的插值器
AccelerateDecelerateInterpolator============动画开始与结束的地方速率改变比较慢,在中间的时候加速。
AccelerateInterpolator===================动画开始的地方速率改变比较慢,然后开始加速。
AnticipateInterpolator ==================开始的时候向后然后向前甩。
AnticipateOvershootInterpolator=============开始的时候向后然后向前甩一定值后返回最后的值。
BounceInterpolator=====================动画结束的时候弹起。
CycleInterpolator======================动画循环播放特定的次数,速率改变沿着正弦曲线。
DecelerateInterpolator===================在动画开始的地方快然后慢。
LinearInterpolator======================以常量速率改变。
OvershootInterpolator====================向前甩一定值后再回到原来位置。
PathInterpolator========================新增的,就是可以定义路径坐标,然后可以按照路径坐标来跑动;注意其坐标并不是 XY,而是单方向,也就是我可以从0~1,然后弹回0.5然后又弹到0.7有到0.3,直到最后时间结束。
0 0
- Animation的巧妙使用
- Animation的巧妙使用
- android Animation的使用
- 关于Animation的使用
- -webkit-animation的使用
- animation的使用
- Animation的使用
- Property Animation 的使用
- -webkit-animation的使用
- 使用UIView的animation
- Animation的使用
- -webkit-animation的使用
- Animation的使用
- Animation的使用
- animation.css的使用
- unity的animation的使用
- Android Animation ------ ViewFlipper的使用
- Animation控件的使用语法
- 编辑的学习opencv上的例子例题
- HBase启动和停止命令
- c++中的内存对齐
- TOU-TOC 问题
- 一台电脑不能打开Github而其他电脑可以打开Github 解决方法
- Animation的使用
- 建造者模式
- ubuntu 开机自启动设置
- Android四大组件
- xampp集成包如何重置mysql的密码
- Vue.js 系列教程 3:Vue-cli,生命周期钩子
- Spark 与 大数据基础
- Lint
- Java代码优化