连续旋转图像

时间:2016-05-05 14:53:34

标签: android animation rotation

我有以下代码尝试连续旋转图像:

Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_animation); image.startAnimation(animation);

rotate_animation.xml文件如下:

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:toDegrees="360" />

上述代码的问题在于它会旋转图像并在再次旋转之前暂停。我正在寻找的是平稳的连续旋转,只有当我明确需要停止时才会停止。

3 个答案:

答案 0 :(得分:2)

转移到属性动画师,这是动画的推荐方法。试试这个 -

ImageView imageview = (ImageView)findViewById(R.id.yourimage);

ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
                    "rotation", 0f, 360f);
imageViewObjectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
imageViewObjectAnimator.setRepeatMode(ObjectAnimator.RESTART);
imageViewObjectAnimator.setInterpolator(new AccelerateInterpolator());
imageViewObjectAnimator.start();

答案 1 :(得分:0)

我已在活动中成功使用了动画旋转,请参阅我的rotate.xml

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_rounded_loading"
android:duration="1000"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite" />

并在activity_main.xml文件中,

 <ImageView
    android:id="@+id/ivLoading"
    android:layout_width="200px"
    android:layout_height="200px"
    android:layout_centerHorizontal="true"
    android:src="@drawable/rotate"
    android:layout_centerVertical="true" />

但它没有处理片段。所以我通过下面的代码将它想象成一个片段,连续旋转图像,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Accessing the layout components to be displayed.
    rootView = inflater.inflate(R.layout.fragment_loading, container, false);
    ivLoading= (ImageView) rootView.findViewById(R.id.ivLoading);

    RotateAnimation rotateAnimation = new RotateAnimation(
            0,//float: Rotation offset to apply at the start of the animation.
            360,//float: Rotation offset to apply at the end of the animation.
            Animation.RELATIVE_TO_SELF,//int: Specifies how pivotXValue should be interpreted
            0.5f,//float: The X coordinate of the point about which the object is being rotated
            Animation.RELATIVE_TO_SELF,//int: Specifies how pivotYValue should be interpreted
            0.5f//float: The Y coordinate of the point about which the object is being rotated
    );
    rotateAnimation.setDuration(1500);//How long this animation should last.
    rotateAnimation.setRepeatCount(Animation.INFINITE);//Sets how many times the animation should be repeated.
    rotateAnimation.setInterpolator(new LinearInterpolator());//Sets the acceleration curve for this animation.
    ivLoading.startAnimation(rotateAnimation);

    return rootView;
}

并在fragment_loading.xml文件中

     <ImageView
    android:id="@+id/ivLoading"
    android:layout_width="200px"
    android:layout_height="200px"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_rounded_loading"
    android:layout_centerVertical="true" />

答案 2 :(得分:0)

Runnable runnable = new Runnable() {
        @Override
        public void run() {
           binding.progressImage.animate().rotationBy(360).withEndAction(this).setDuration(1500).setInterpolator(new LinearInterpolator()).start();
        }
    };

    binding.progressImage.animate().rotationBy(360).withEndAction(runnable).setDuration(1500).setInterpolator(new LinearInterpolator()).start();