本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!
Android中动画的应用,在应用管理软件、购物软件、追星软件等比较广泛;比如常见的进度条设计,此处尤其指圆形的那种。比如清理小火箭,从下向上飞出;比如清理软件提示,由深色渐变成浅色。这些都是动画的应用场景。
Android动画分为两种,一种叫帧动画,就像flash一样,学名Frame,进度条一般使用这种;另一种叫补间动画,学名Tween,可以移动位置、变化颜色、变换大小、翻转变化。
先说帧动画
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@mipmap/icon_s01"
android:duration="30" />
<item
android:drawable="@mipmap/icon_s02"
android:duration="30" />
<item
android:drawable="@mipmap/icon_s03"
android:duration="30" />
</animation-list>
应用:
先写布局
<ImageView
android:id="@+id/loading_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/anim_progress_round"/>
再执行代码
ImageView loadingImage = child.findViewById(R.id.loading_iv);
AnimationDrawable animation = (AnimationDrawable) loadingImage.getBackground();
if (animation.isRunning()) {
animation.stop();
}
animation.start();
获得背景的AnimationDrawable对象,执行start方法即可。
补间动画:
基类Animation,子类ScaleAnimation、AlphaAnimation、RotateAnimation、TranlateAnimation,分别用于大小变换、色彩变换、翻转变换和位移变换。
常用方法有setDutation-设置运行时间,setFillAfter-设置运行结束是否保持最后状态,setFillBefore-设置运行结束是否保质最初状态,setRepeatCount-设置重复执行次数;setRepeatMode-设置重复类型,如REVERSE会倒着再执行repeatCount遍;setInterpolater-设置插补器,可以让控件回弹,控制速度。
//accelerate_decelerate_interpolator先慢后快再慢linear_interpolator匀速decelerate_interpolator先快后慢
mAnimationTranslate.setInterpolator(mContext, android.R.anim.decelerate_interpolator);
例:
mAnimationTranslate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f,
Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
mAnimationTranslate.setDuration(3000);
mAnimationTranslate.setRepeatMode(Animation.ABSOLUTE);
mAnimationTranslate.setRepeatCount(0);
mRocketIv.startAnimation(mAnimationTranslate);
设置相关参数,执行startAnimation即可。
ScaleAnimation:fromXScale-X轴方向缩放比例,toXScale、fromYScale和toYScale同理;pivoteX起点X坐标,pivoteY同理。
AlphaAnimation:fromAlpha-起始透明度,toAlpha-最终透明度。
RotateAnimation:fromDegress-起始角度,toDegress-最终角度,正数为顺时针,负数为逆时针。
TranslateAnimation:fromXType-起始位移类型,fromYType同理;fromXValue-起始X坐标,fromYValue同理;
从左边折出来
ScaleAnimation animation = new ScaleAnimation(0f, 1f, 1f, 1f);
animation.setDuration(1000);//设置动画持续时间
mChannelLayout.setAnimation(animation);
从左上角弹出来
ScaleAnimation animation = new ScaleAnimation(0f, 1f, 0f, 1f);
animation.setDuration(1000);//设置动画持续时间
mChannelLayout.setAnimation(animation);
从左向右的动画:
AnimationUtils.makeInAnimation(this, true);
从右回到左的动画:
AnimationUtils.makeOutAnimation(this, false);
最后AnimationSet可以对以上四种补间动画类型进行组合,制造出更加炫酷的效果。
话外:通过AnimationSet可以将上述4种动画,使用AnimatorInflater和ObjectAnimator,联合起来一起(togethor)或者顺序(sequentially)执行,做更复杂的效果。
动画修饰:
AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
AnticipateInterpolator 开始的时候向后然后向前甩
AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator 动画结束的时候弹起
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方快然后慢
LinearInterpolator 以常量速率改变
OvershootInterpolator 向前甩一定值后再回到原来位置