Android动画
- View Animation 视图动画(Tween Animation 补间动画),只能用来设置View的动画
- Drawable Animation 帧动画(Frame动画),一帧帧地显示资源文件中的Drawable
- Property Animation 属性动画,在android3.0以上的系统才有。这动画可以设置给任何的Object,包括那些还没有渲染到屏幕的view.
为什么要引入属性动画?
- 补间动画只能够作用在View上的
- 补间动画只能够实现移动、缩放、旋转和淡入淡出这四种动画操作,不能改变View的背景等
- 补间动画只是改变了View的显示效果而已,而不会真正去改变View的属性
View Animation 补间动画
视图动画也叫补间动画,指在一个视图容器中执行一些变换。包含有:位置、大小、旋转、透明 补间动画。
一般通过xml实现,不建议是用android代码实现,因为代码实现的可读性比较差。
补间动画的相关类
- AlphaAnimation <alpha>放在res/anim/目录下 透明渐变动画效果
- RotateAnimation <rotate>放在res/anim/目录下 旋转转移动画效果
- ScaleAnimation <scale>放在res/anim/目录下 缩放动画效果
- TranslateAnimation <translate>放在res/anim/目录下 移动动画效果
- AnimationSet <set> 放在res/anim/目录下 持有动画的容器
补间动画之ScaleAnimation使用
ScaleAnimation是尺寸变化动画的类,控制View的尺寸变化。
使用java代码实现
/** * * @param fromX 起始x轴位置,0为最小,1为原始,float形 * @param toX 同上 * @param fromY 同上 * @param toY 同上 * @param pivotXType 用来约束pivotXValue的取值。 * 取值有三种:Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT * Type: * Animation.ABSOLUTE:绝对,如果设置这种类型,后面pivotXValue取值就必须是像素点;比如:控件X方向上的中心点,pivotXValue的取值mIvScale.getWidth() / 2f * Animation.RELATIVE_TO_SELF:相对于控件自己,设置这种类型,后面pivotXValue取值就会去拿这个取值是乘上控件本身的宽度;比如:控件X方向上的中心点,pivotXValue的取值0.5f * Animation.RELATIVE_TO_PARENT:相对于它父容器(这个父容器是指包括这个这个做动画控件的外一层控件), 原理同上, * @param pivotXValue 配合pivotXType使用,原理在上面 * @param pivotYType 同from/to * @param pivotYValue 原理同上 */ scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); //动画持续时间 scaleAnimation.setDuration(1000); //动画持续次数,不设置默认0(也就是播放一次,N+1次,< 0 表示无限次) scaleAnimation.setRepeatCount(1); //动画播放完后倒置播放 scaleAnimation.setRepeatMode(Animation.REVERSE); //播放N+1此后最后是否保持最后的状态 scaleAnimation.setFillAfter(true);
# 启动动画 textView.startAnimation(scaleAnimation); # 清除动画 textView.clearAnimation();
使用xml实现
/res/anim/anim_scale.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"> <scale android:duration="100" android:fillAfter="true" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.15" android:toYScale="1.15" /> </set>
# 加载anim Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale); # 启动anim if (null != animation) { textView.startAnimation(animation); }
参考文章
© 版权声明
上面的xml配置复制错了吧
哈哈哈 多谢,已经修改。