如何在 Android 中进行平滑的图像旋转? [英] How to make a smooth image rotation in Android?

查看:27
本文介绍了如何在 Android 中进行平滑的图像旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RotateAnimation 来旋转我在 Android 中用作自定义循环微调器的图像.这是我的 rotate_inlimitly.xml 文件,我把它放在 res/anim/ 中:

I'm using a RotateAnimation to rotate an image that I'm using as a custom cyclical spinner in Android. Here's my rotate_indefinitely.xml file, which I placed in res/anim/:

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

当我使用 AndroidUtils.loadAnimation() 将其应用到我的 ImageView 时,效果很好!

When I apply this to my ImageView using AndroidUtils.loadAnimation(), it works great!

spinner.startAnimation( 
    AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );

一个问题是图像旋转似乎在每个循环的顶部暂停.

The one problem is that the image rotation seems to pause at the top of every cycle.

换句话说,图像旋转 360 度,短暂暂停,然后再次旋转 360 度,等等.

In other words, the image rotates 360 degrees, pauses briefly, then rotates 360 degrees again, etc.

我怀疑问题在于动画使用的是默认插值器,例如 android:iterpolator="@android:anim/accelerate_interpolator" (AccelerateInterpolator),但我不知道如何告诉它不要插入动画.

I suspect that the problem is that the animation is using a default interpolator like android:iterpolator="@android:anim/accelerate_interpolator" (AccelerateInterpolator), but I don't know how to tell it not to interpolate the animation.

如何关闭插值(如果这确实是问题所在)以使我的动画循环顺畅?

How can I turn off interpolation (if that is indeed the problem) to make my animation cycle smoothly?

推荐答案

关于 AccelerateInterpolator,你说得对;你应该改用 LinearInterpolator.

You are right about AccelerateInterpolator; you should use LinearInterpolator instead.

您可以使用动画 XML 文件中的内置 android.R.anim.linear_interpolatorandroid:interpolator="@android:anim/linear_interpolator".

You can use the built-in android.R.anim.linear_interpolator from your animation XML file with android:interpolator="@android:anim/linear_interpolator".

或者您可以在您的项目中创建自己的 XML 插值文件,例如将其命名为 res/anim/linear_interpolator.xml:

Or you can create your own XML interpolation file in your project, e.g. name it res/anim/linear_interpolator.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearInterpolator xmlns:android="http://schemas.android.com/apk/res/android" />

并添加到您的动画 XML 中:

And add to your animation XML:

android:interpolator="@anim/linear_interpolator"

特别注意:如果您的旋转动画在一个集合内,则设置插值器似乎不起作用.使旋转顶部元素修复它.(这将节省您的时间.)

Special Note: If your rotate animation is inside a set, setting the interpolator does not seem to work. Making the rotate the top element fixes it. (this will save your time.)

这篇关于如何在 Android 中进行平滑的图像旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆