目录
1.什么是补间动画
2.XML实现方式
3.代码实现方式
4.展现形式
补间动画:属于Android中View动画的一种,就是涵盖了 平移、缩放、旋转 和 透明度四种变化的动画。实现方式有两种:xml文件 和 java代码。
四种补间动画分别为RotateAnimation
、ScaleAnimation
、TranslateAnimation
、AlphaAnimation
,他们的父类为Animation。
首先res下 创建 anim 文件夹,并创建 xxx.xml 文件
代码如下,我们可以在xml里面直接设置动画类型及其属性,下面是一个透明度从0.2到1的动画
看下面的代码,看到set标签里面有四种动画,代表着四种动画可以组合放在一起执行,例如从左侧划出的时候,透明度从0到1。
特别注意,当我们对set标签使用Animation的属性时会对该标签下的所有子控件都产生影响。
在xml中配置好后就可以直接在代码中进行引用。引用方式如下:
//使用系统库自带的AnimationUtils,生成一个动画,第一个参数是上下文,第二个参数是你的xml文件 val animation = AnimationUtils.loadAnimation(this, R.anim.fade_in_10) //开始动画 alphaImg?.startAnimation(animation)
整理了一下相关的xml属性及其对应的java方法
- android:detachWallpaper setDetachWallpaper(boolean) 是否在壁纸上运行
- android:duration setDuration(long) 动画持续时间,毫秒为单位
- android:fillAfter setFillAfter(boolean) 控件动画结束时是否保持动画最后的状态
- android:fillBefore setFillBefore(boolean) 控件动画结束时是否还原到开始动画前的状态
- android:fillEnabled setFillEnabled(boolean) 与android:fillBefore效果相同
- android:interpolator setInterpolator(Interpolator) 设定插值器(指定的动画效果,譬如回弹等)
- android:repeatCount setRepeatCount(int) 重复次数
- android:repeatMode setRepeatMode(int) 重复类型有两个值,reverse表示倒序回放,restart表示从头播放
- android:startOffset setStartOffset(long) 调用start函数之后等待开始运行的时间,单位为毫秒
- android:zAdjustment setZAdjustment(int) 表示被设置动画的内容运行时在Z轴上的位置(top/bottom/normal),默认为normal
四种补间动画分别为RotateAnimation
、ScaleAnimation
、TranslateAnimation
、AlphaAnimation
,他们的父类为Animation。传入想要的参数生成对应动画,再根据需求配置属性
直接上代码:下面实现的是,一张普通图片,根据四个按钮,图片执行不同的动画
//代码实现补间动画,相关的动画里的构造参数可以点到源码去查看val animationCodeAlpha = AlphaAnimation(0f, 1f)//参数:起始透明度,最终透明度animationCodeAlpha.duration = 3000 //动画执行世间animationCodeAlpha.fillAfter = false //是否保存最后状态,为true就会保存,false执行完后会恢复原样animationCodeAlpha.repeatCount = 2 //执行次数,-1 为无数次val animationCodeRotate = RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)animationCodeRotate.duration = 3000animationCodeRotate.fillAfter = falseval animationCodeTranslate = TranslateAnimation(-200f, 0F, 0f, -100f)animationCodeTranslate.duration = 3000animationCodeTranslate.fillAfter = falseval animationCodeScale = ScaleAnimation(0f, 1f, 0f, 1f)animationCodeScale.duration = 3000animationCodeScale.fillAfter = falsealphaTV?.setOnClickListener {animationCodeImg?.animation = animationCodeAlphaanimationCodeAlpha.start()}rotateTV?.setOnClickListener {animationCodeImg?.animation = animationCodeRotateanimationCodeRotate.start()}translateTv?.setOnClickListener {animationCodeImg?.animation = animationCodeTranslateanimationCodeTranslate.start()}scaleTV?.setOnClickListener {animationCodeImg?.animation = animationCodeScaleanimationCodeScale.start()}
上面就是一个标准的使用我们定义的补间动画的模板。至于补间动画的使用,Animation还有如下一些比较实用的方法介绍:
Animation类的方法
- reset() 重置Animation的初始化
- cancel() 取消Animation动画
- start() 开始Animation动画
- setAnimationListener(AnimationListener listener) 给当前Animation设置动画监听
- hasStarted() 判断当前Animation是否开始
- hasEnded() 判断当前Animation是否结束
既然补间动画只能给View使用,那就来看看View中和动画相关的几个常用方法吧,如下:
View类的常用动画操作方法 解释
- startAnimation(Animation animation) 对当前View开始设置的Animation动画
- clearAnimation() 取消当View在执行的Animation动画
tips:补间动画执行之后并未改变View的真实布局属性值。切记这一点,譬如我们在Activity中有一个Image在屏幕上方,我们设置了平移动画移动到屏幕下方然后保持动画最后执行状态呆在屏幕下方,这时如果点击屏幕下方动画执行之后的Image是没有任何反应的,而点击原来屏幕上方没有Image的地方却响应的是点击Image的事件。
补充:差值器
//设置动画为加速动画(动画播放中越来越快)
android:interpolator="@android:anim/accelerate_interpolator"
//设置动画为减速动画(动画播放中越来越慢)
android:interpolator="@android:anim/decelerate_interpolator"
//设置动画为先加速在减速(开始速度最快 逐渐减慢)
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
//先反向执行一段,然后再加速反向回来(相当于我们弹簧,先反向压缩一小段,然后在加速弹出)
android:interpolator="@android:anim/anticipate_interpolator"
//同上先反向一段,然后加速反向回来,执行完毕自带回弹效果(更形象的弹簧效果)
android:interpolator="@android:anim/anticipate_overshoot_interpolator"
//执行完毕之后会回弹跳跃几段(相当于我们高空掉下一颗皮球,到地面是会跳动几下)
android:interpolator="@android:anim/bounce_interpolator"
//循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles* Math.PI* input)
android:interpolator="@android:anim/cycle_interpolator"
//线性均匀改变
android:interpolator="@android:anim/linear_interpolator"
//加速执行,结束之后回弹
android:interpolator="@android:anim/overshoot_interpolator"
代码里:默认匀速哦
animation.setInterpolator(new AccelerateInterpolator()); animation.setInterpolator(new DecelerateInterpolator()); animation.setInterpolator(new AccelerateDecelerateInterpolator()); animation.setInterpolator(new AnticipateInterpolator()); animation.setInterpolator(new AnticipateOvershootInterpolator()); animation.setInterpolator(new BounceInterpolator()); animation.setInterpolator(new CycleInterpolator(2)); animation.setInterpolator(new LinearInterpolator()); animation.setInterpolator(new OvershootInterpolator());