private static float currentValue; //记录当前旋转的进度 private static long mDuration = 22000; //旋转的速度 //设置匀速转动,AccelerateInterpolator为加速效果、DecelerateInterpolator为减速效果 LinearInterpolator lin = new LinearInterpolator();
*播放动画的方法 * 我也曾尝试使用Animation这个工具类来实现iv_rotate.startAnimation(animation);旋转的功能, * 但是出现了一个问题不能解决 * 那就是每次暂停以后,图片会归位到初始的状态,而不能记录当前状态 * 要记录当前的状态,必须使用 ObjectAnimator这个类来定义动画的操作对象 * */ public void rotateAnomaition(Long mDuration, LinearInterpolator linearInterpolator) { /* 图片旋转的动画类的对象*/ animation = ObjectAnimator.ofFloat(iv_rotate, "Rotation", currentValue - 359, currentValue); // 设置持续时间 animation.setDuration(mDuration); animation.setInterpolator(linearInterpolator); // 设置循环播放 animation.setRepeatCount(ObjectAnimator.INFINITE); // 设置动画监听 animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // TODO Auto-generated method stub // 监听动画执行的位置,以便下次开始时,从当前位置开始 // animation.getAnimatedValue()为flort类型 currentValue = (float) animation.getAnimatedValue(); } }); animation.start(); //开启动画,这个方法一般就是从起始位置开启旋转动画 }