Android动画-View动画,插值器
来源:互联网 发布:mysql精确两位小数 编辑:程序博客网 时间:2024/05/22 09:46
原文地址 http://blog.csdn.net/qq_25806863/article/details/70169620
补间动画又叫View动画。Android中的补间动画分四种,渐变、缩放、平移,旋转。还有个set是他们的组合,
在XML文件中的标签对应是 <alpha>,<scale>,<translate>,<rotate>
对应的四个类是AlphaAnimation,ScaleAnimation,TranslateAnimation,RotateAnimation
四个类。
这四个类都继承Animation类,Animation是个抽象类。
<alpha>
让透明度渐变, ScaleAnimation <scale>
放大或缩小 TranslateAnimation <translate>
移动位置 RotateAnimation <rotate>
绕某一点旋转使用
View动画是作用在View上的。使用有两种方式,以一个简单的旋转动画为例:
- 在res中新建anim文件夹,在这个文件夹里写上xml动画文件。如res/anim/anim_test.xml.
- 在java文件中用这个xml文件生成Animation类。
- 调用View的startAnimation来启动动画。
xml文件:
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="180" android:duration="2000" android:pivotX="50%" android:pivotY="50%" />
java activity:
btnRun = (Button) findViewById(R.id.btn_run);ivIc = (ImageView) findViewById(R.id.iv_ic);final Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_test);btnRun.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ivIc.startAnimation(animation); }});
- 直接在Java文件中new一个Animation的子类,然后调用View的startAnimation来启动动画。
final RotateAnimation animation = new RotateAnimation(0,180,50,50); animation.setDuration(2000); btnRun.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ivIc.startAnimation(animation); } });
应该能发现这两个动画并不一样。因为在xml文件中`pivotX="50% pivotY="50%""
指的是在要动画的view的高宽的50%为动画中心,也就是View的正中心。这里是旋转,就是旋转中心了。 而在Java中设置的两个50,意思是从这个View的左上角向右偏移50像素,向下偏移50像素为动画中心。
一般推荐用XML来写View动画,有以下好处:
- xml文件以配置的形式,让动画看起来更直观,可读性强。
- 一个动画文件可以被多个View使用,重用性强。
- 便于更换动画。更换一个View的动画只要更换load的xml文件就行,不需要修改其他代码。
既然xml以配置的形式来定义动画,那么只要弄明白这些属性的意义就能灵活实现自己需要的动画了。
Animation的属性
这四个子类独有的属性并不多,大部分都是继承自父类Animation的。所以先看看Animation的属性。
android:repeatMode
- reverse
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:repeatMode="reverse" android:toDegrees="360" />
- restart
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:repeatMode="restart" android:toDegrees="360" />
AlphaAnimation的独有属性
5秒从全透变成完全不透 从0-1;
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000"> <alpha android:fromAlpha="0" android:toAlpha="1" /></set>
ScaleAnimation独有属性
从图片的右下角开始缩放,宽高各从从原来的2倍缩放到原来的十分之一:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000"> <scale android:pivotX="100%" android:pivotY="100%" android:fromXScale="2" android:fromYScale="2" android:toXScale="0.1" android:toYScale="0.1" /></set>
TranslateAnimation独有属性
从自身的左上角移到自身的右下角:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000"> <translate android:fromXDelta="-100%" android:fromYDelta="-100%" android:toXDelta="100%" android:toYDelta="100%" /></set>
RotateAnimation独有属性
从360度转到-90度:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000"> <rotate android:pivotX="50%" android:pivotY="50%" android:fromDegrees="360" android:toDegrees="-90" /></set>
AnimationSet
AnimationSet代表动画的集合,在XML文件中的标签是<set>
,前面已经出现过了。
影这个标签可以把不同的动画组合起来作用在同一个View上。
使用AndroidStudio时发现只有在<set>
标签里面,才能自动提示出来Animation的属性,在<alpha>
等标签里只提示他自己独有的那几个属性,不知道有没有解决办法。
例如要同时缩放旋转,并改变透明度:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000"> <rotate android:pivotX="50%" android:pivotY="50%" android:fromDegrees="360" android:toDegrees="-90" /> <alpha android:fromAlpha="1" android:toAlpha="0.2"/></set>
还能用同一种动画拼成一个连续动画,只是这样做好像有点low:
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:fromXDelta="0" android:fromYDelta="0" android:toYDelta="-100%" android:toXDelta="-100%" android:duration="2000" /> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200%" android:duration="2000" android:startOffset="2000"/> <translate android:fromXDelta="0" android:fromYDelta="0" android:toYDelta="200%" android:duration="2000" android:startOffset="4000"/> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="-200%" android:duration="2000" android:startOffset="6000"/> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="100%" android:toYDelta="-100%" android:duration="2000" android:startOffset="8000"/></set>
android:shareInterpolator
AnimationSet也有个属性android:shareInterpolator
.值为true或flase。表示集合中的动画是佛共享同一个插值器。
插值器Interpolator
Interpolator继承自TimeInterpolator,用来定义动画在不同的时间有不同的速度。也叫插补器。
插值器可以通过在xml文件中用android:interpolator
设置,也可以在java中通过animation.setInterpolator()
来设置.
在java中设置可以传一些参数,对插值器进行更详细的设置,比较灵活。
下面以让一个图片顺时针旋转360度为例,其他动画都类似的作用:
AccelerateDecelerateInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/accelerate_decelerate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
AccelerateInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/accelerate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
DecelerateInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/decelerate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
BounceInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/bounce_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
CycleInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/cycle_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
LinearInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/linear_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
AnticipateOvershootInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/anticipate_overshoot_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
AnticipateInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/anticipate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
OvershootInterpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:interpolator="@android:anim/overshoot_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/></set>
对View动画的监听
Animation里有一个AnimationListener类提供了对动画开始,重复,和结束的监听。
animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Log.i(TAG, "动画开始时做操作: "); } @Override public void onAnimationEnd(Animation animation) { Log.i(TAG, "动画重复时做操作: "); } @Override public void onAnimationRepeat(Animation animation) { Log.i(TAG, "动画结束时做操作: "); } });
- Android动画-View动画,插值器
- Android动画-View动画
- Android动画二:View动画
- Android动画之------View动画
- Android动画之View动画
- Android View 动画问题
- Android View.startAnimation()动画
- android view动画
- android view 执行动画
- android view动画问题
- android View动画初探
- Android view动画问题
- android中的view动画
- android动画 -- view Animation
- Android读书笔记-----View动画
- android view动画类型
- android view 动画学习
- android View动画
- rails 生产环境部署练习(nginx + passenger + capistrano)
- 同余与模运算
- WebSocket的事件触发机制
- mysql concat的用法
- java发送简单邮件
- Android动画-View动画,插值器
- Intent传递List<Object>
- 浏览器缓存机制剖析
- ReactNative textinput 常用方法
- oracle与sqlserver的十大区别
- 离散基础 (2). 关于“解空间的大小”的计算问题
- javaweb中实现在线人数统计
- pagecontext.request.getcontextpath和request.getContextPath()
- akka-http使用CORS(跨域资源共享)实现跨域请求