android四种基本动画效果使用
1.点击下载
上图:
包括基础的动画 透明度、放大缩小、平移、旋转、组合动画、闪烁、弹跳动画
1.透明度
final Animation alphaAniamtion = new AlphaAnimation(1.0f,0);
alphaAniamtion.setFillAfter(false);
alphaAniamtion.setDuration(1500);
alphaAniamtion.setRepeatCount(-1);
v.startAnimation(alphaAniamtion);
2.放大缩小
final ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(1000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setFillBefore(false);
scaleAnimation.setRepeatCount(-1);
v.startAnimation(scaleAnimation);
3.平移
final TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.ABSOLUTE,0,Animation.ABSOLUTE,
750,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);
translateAnimation.setDuration(4000);
translateAnimation.setFillAfter(true);
translateAnimation.setFillBefore(true);
translateAnimation.setRepeatCount(-1);
// 使用插值器 LinearInterpolator(匀速)、AccelerateInterpolator(加速)、AccelerateDecelerateInterpolator(先加速再减速)、BounceInterpolator(反弹数次后停止)、DecelerateInterpolator(减速)
translateAnimation.setInterpolator(new AccelerateInterpolator());//设置一个加速的插值器
v.startAnimation(translateAnimation);
4.旋转
final RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);
rotateAnimation.setDuration(1000);
rotateAnimation.setFillAfter(true);
rotateAnimation.setFillBefore(false);
rotateAnimation.setRepeatCount(-1);
v.startAnimation(rotateAnimation);
5.组合动画
/**
* 创建组合动画
*/
private void createAnimationSet(){
final AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.5f, 1f, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.ABSOLUTE, 100); animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setDuration(5000);
animationSet.setFillAfter(true);//设置动画执行后保持最后状态
animationSet.setFillBefore(false);//设置动画执行后不回到原来状态
animationSet.setRepeatCount(3);//这样设置无作用,所以在回调中重新启动动画
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
v.startAnimation(animationSet);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
v.startAnimation(animationSet);
}
6.弹跳
//弹跳动画 插值器实现
final TranslateAnimation down = new TranslateAnimation(0, 0, 0, 150);//位移动画,从button的上方300像素位置开始
down.setFillAfter(true);
down.setInterpolator(new BounceInterpolator());//弹跳动画,要其它效果的当然也可以设置为其它的值
down.setDuration(2000);//持续时间
v.startAnimation(down);
7.闪烁
//闪烁动画
final Animation animation = new AlphaAnimation(1,0.5f);
animation.setDuration(500);//闪烁时间间隔
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
v.startAnimation(animation);
补充:
动画结束的监听事件
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
image5.startAnimation(animationSet);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
8.结束动画
/** 结束动画 */
aniamtion.cancel();
了解更多可以参照: https://www.jianshu.com/p/5d090270a4f5
https://blog.****.net/amanduzhuojiang/article/details/77877900