Animation
来源:互联网 发布:sql注入防范 编辑:程序博客网 时间:2024/06/04 19:08
在Android系统中,官方给我们提供了两种类型的动画:属性动画(Property Animation) 和 视图动画(View Animation),而视图动画又包含了两种类型:补间动画(Tween animation) 和 帧动画(Frame animation)。
- Property Animation(属性动画):通过改变对象的属性来实现动画效果。
View Animation(视图动画):包括了以下两种动画类型。
- Tween Animation(补间动画):通过对视图进行一系列的动作变化实现动画效果。
- Frame Animation(帧动画):通过一组图片有序播放实现动画视觉效果。
Tween Animation,翻译为补间动画,是View Animation两种中的一种,是Android开发中使用最普遍的动画,当然也是使用率最高的一种动画,因为它能够基本满足我们的动画需要,主要是通过对控件实现透明度(alpha)、尺寸(scale)、位置(translate)、旋转rotate)进行改变,通过集合(set)的方式,实现连续的动画效果。
使用方式
- 在XML文件中定义一系列的动画标签,所有便签在set便签里面构成集合
- Java文件中代码实现
四个便签都有对应的Java类:AlphaAnimation, RotateAnimation, ScaleAnimation, TranslateAnimation。
- AnimationSet:代表着一组可以一起播放的动画。
- AlphaAnimation:控制一个对象透明度的动画。
- RotateAnimation:控制一个对象旋转的动画。
- ScaleAnimation:控制一个对象尺寸的动画。
- TranslateAnimation:控制一个对象位置的动画。
监听
Animation.AnimationListener:动画监听器。主要包括监听动画执行中、执行结束、循环执行三个过程:
mAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // 动画执行时 } @Override public void onAnimationEnd(Animation animation) { // 动画结束时 } @Override public void onAnimationRepeat(Animation animation) { // 动画循环时 } });
属性及其对应方法(所有子类都拥有)
- android:detachWallpaper: 是否在壁纸上运行 java : setDetachWallpaper(boolean detachWallpaper)
- android:duration: 动画时长,单位毫秒。 java : setDuration(long)
- android:fillAfter: 设置为true,控件动画结束时将保持动画最后一帧(xml文件中,需要设置在set便签才生效)。java : setFillAfter(boolean)
- android:fillBefore: 设置为true,控件动画结束时将保持动画开始第一帧 java : setFillBefore(boolean)
- android:fillEnabled: 设置fillAfter和fillBefore的可用性false则都是false反之则反 java : setFillEnabled(boolean)
- android:interpolator: 插值器。设置动画速率的变化(譬如加速、减速、匀速等) java : setInterpolator(Interpolator)
- android:repeatCount: 动画重复次数。 java : setRepeatCount(int)
- android:repeatMode: 重复模式,有reverse(倒序)和restart(重复)两种,必须配合repeatCount一起使用。 java : setRepeatMode(int)
- android:startOffset: 延迟一定毫秒之后才开始动画。 java : setStartOffset(long)
- android:zAdjustment: 表示被设置动画的内容在动画运行时在Z轴上的位置,有以下三个值
- normal 默认值,保持内容在Z轴上的位置不变
- top 保持在Z周最上层
- bottom 保持在Z轴最下层
java : setZAdjustment(int)
Interpolator
Interpolator,又名插值器,主要是实现动画的速率变化。Interpolator作为一个接口,然后抽象类BaseInterpolator实现Interpolator接口,在BaseInterpolator的子类就是一系列Android提供的插值器。
用法:
- 在XML的标签下设置:android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
- 在JAVA代码中使用:animation.setInterpolator(new AccelerateDecelerateInterpolator());
1、AccelerateDecelerateInterpolator:开始和结束速度慢,中间部分加速。
2、AccelerateInterpolator:开始缓慢,然后加速。
3、AnticipateInterpolator: 开始后退,然后前进。
4、AnticipateOvershootInterpolator: 开始后退,然后前进,直到超出目标值,再后退至目标值。
5、BounceInterpolator:在结束时弹跳。
6、CycleInterpolator:在指定数量的周期内重复动画,速度变化遵循正弦规律。
7、DecelerateInterpolator:开始加速,结束缓慢。
8、LinearInterpolator:匀速。
9、OvershootInterpolator:前进,直到超出目标值,再后退至目标值。
10、PathInterpolator:根据路径变化改变速率。
AnimationSet
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <scale android:duration="3000" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.4" android:toYScale="1.0" /> <set android:duration="3000" android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="3000"> <scale android:fromXScale="1.4" android:fromYScale="0.6" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.0" android:toYScale="0.0" /> <rotate android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-360" /> </set></set>
java代码设置
//参数为false,不共享Interpolator AnimationSet set = new AnimationSet(false); set.setFillAfter(true); // Scale动画 ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.4f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); scaleAnimation.setDuration(3000); set.addAnimation(scaleAnimation); // mSet集合 AnimationSet mSet = new AnimationSet(true); mSet.setInterpolator(new AccelerateInterpolator()); mSet.setDuration(3000); mSet.setStartOffset(3000); // Scale动画 ScaleAnimation mScaleAnimation = new ScaleAnimation(1.4f, 0.0f, 0.6f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); mSet.addAnimation(mScaleAnimation); // Rotate动画 RotateAnimation mRotateAnimation = new RotateAnimation(0.0f, -360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); mSet.addAnimation(mRotateAnimation); set.addAnimation(mSet); image.startAnimation(set);
构造函数
- AnimationSet(Context context, AttributeSet attrs)
:当需要加载AttributeSet时使用的构造函数。 - AnimationSet(boolean
shareInterpolator):当从代码实现AnimationSet时,传入是否子动画是否共享插值器参数。
Java方法
- void addAnimation (Animation a): 把一个子动画加到Animation Set中去。
- long computeDurationHint (): 获取最长时间的子动画的时间,单位毫秒。
- List getAnimations (): 获取所有子动画对象的集合。
- long getStartTime ():获取动画什么时候应该播放,如果方法返回常量 START_ON_FIRST_FRAME
(-1),表明动画未播放。
- boolean getTransformation (long currentTime, Transformation t):**
获取帧动画执行到目前时的变换信息,存到Transformation中,主要用作调试用。
- void initialize (int width, int height, int parentWidth, int
parentHeight):** 初始化动画的宽高和父动画的宽高。
- void reset (): 重置动画的初始状态。
- void restrictDuration (long durationMillis):
限制动画不能超过durationMillis个毫秒。
- boolean willChangeBounds (): 动画是否会改变view的尺寸边界。true为会。
- boolean willChangeTransformationMatrix (): 动画是否会改变view的矩阵。
- animation
- animation
- Animation
- Animation
- Animation
- Animation
- Animation
- Animation
- Animation
- Animation
- animation
- Animation
- Animation
- animation
- Animation
- Animation
- Animation
- Animation
- leetcode:Remove K Digits
- web项目中不适用于java.sql.DriverManager描述的JDBC 4.0 Drivers及以后版本连接数据库是不需要用forName()?
- android实现多图片放大缩小的切换
- bwlabel函数
- DWR实例浅析
- Animation
- java获取不同时间的工具类
- 7.learningAction(行为库)2
- 网络编程-实验1-C与S通信
- Spring 学习2--Spring特殊语义注释类定义bean
- 测试博客
- Java异常
- 基于Primeton ESB的高可用MQ集群配置
- C# Keycode对照表