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();

了解更多可以参照:

安卓进阶第五篇之常见动画的使用

Android Animation动画的基本使用与介绍

人已赞赏
Android文章

Android版本更新知识(检测、升级)总结

2020-2-23 13:55:46

Android文章

android 自定义阴影 ShadowView

2020-2-23 19:27:19

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索