我正在使用RotateAnimation
来旋转我在Android中用作自定义循环微调器的图像。这是我的rotate_indefinitely.xml
文件,我放在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" />
当我使用ImageView
将它应用于我的AndroidUtils.loadAnimation()
时,效果很好!
spinner.startAnimation(
AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );
一个问题是图像旋转似乎在每个周期的顶部暂停。
换句话说,图像旋转360度,暂停,然后再旋转360度等。
我怀疑问题是动画是使用像android:iterpolator="@android:anim/accelerate_interpolator"
(AccelerateInterpolator
)这样的默认插值器,但我不知道如何告诉它不要插入动画。
如何关闭插值(如果这确实是问题)以使我的动画循环顺利进行?
你是对的AccelerateInterpolator;你应该使用LinearInterpolator。
您可以使用android.R.anim.linear_interpolator
的动画XML文件中的内置android:interpolator="@android:anim/linear_interpolator"
。
或者,您可以在项目中创建自己的XML插值文件,例如将它命名为res/anim/linear_interpolator.xml
:
<?xml version="1.0" encoding="utf-8"?>
<linearInterpolator xmlns:android="http://schemas.android.com/apk/res/android" />
并添加到您的动画XML:
android:interpolator="@anim/linear_interpolator"
特别注意:如果你的旋转动画在一个集合中,设置插值器似乎不起作用。旋转顶部元素可以修复它。 (这会节省你的时间。)
以编程方式旋转对象。
//顺时针旋转:
public void rorate_Clockwise(View view) {
ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 180f, 0f);
// rotate.setRepeatCount(10);
rotate.setDuration(500);
rotate.start();
}
// AntiClockwise旋转:
public void rorate_AntiClockwise(View view) {
ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 0f, 180f);
// rotate.setRepeatCount(10);
rotate.setDuration(500);
rotate.start();
}
view是ImageView或其他小部件的对象。
rotate.setRepeatCount(10);用来重复你的轮换。
500是你的动画持续时间。
如果你使用像我这样的集合动画,你应该在set标签内添加插值:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<rotate
android:duration="5000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:startOffset="0"
android:toDegrees="360" />
<alpha
android:duration="200"
android:fromAlpha="0.7"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="1.0" />
</set>
这对我有用。
是否有可能因为你从0到360,你在0/360花费的时间比你期望的多一点?也许设定为359或358。
在Android中,如果要为对象设置动画并使其将对象从location1移动到location2,动画API会计算出中间位置(补间),然后使用计时器在适当的时间将适当的移动操作排到主线程上。这种方法很好,除了主线程通常用于许多其他事情 - 绘画,打开文件,响应用户输入等。排队的计时器通常会被延迟。编写良好的程序将始终尝试在后台(非主要)线程中执行尽可能多的操作,但是您不能总是避免使用主线程。需要您在UI对象上操作的操作始终必须在主线程上完成。此外,许多API将操作作为线程安全的形式汇集回主线程。
视图全部绘制在同一GUI线程上,该线程也用于所有用户交互。
因此,如果您需要快速更新GUI或者渲染需要花费太多时间并影响用户体验,那么请使用SurfaceView。
旋转图像示例:
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private DrawThread drawThread;
public MySurfaceView(Context context) {
super(context);
getHolder().addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
drawThread = new DrawThread(getHolder(), getResources());
drawThread.setRunning(true);
drawThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
drawThread.setRunning(false);
while (retry) {
try {
drawThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
class DrawThread extends Thread{
private boolean runFlag = false;
private SurfaceHolder surfaceHolder;
private Bitmap picture;
private Matrix matrix;
private long prevTime;
public DrawThread(SurfaceHolder surfaceHolder, Resources resources){
this.surfaceHolder = surfaceHolder;
picture = BitmapFactory.decodeResource(resources, R.drawable.icon);
matrix = new Matrix();
matrix.postScale(3.0f, 3.0f);
matrix.postTranslate(100.0f, 100.0f);
prevTime = System.currentTimeMillis();
}
public void setRunning(boolean run) {
runFlag = run;
}
@Override
public void run() {
Canvas canvas;
while (runFlag) {
long now = System.currentTimeMillis();
long elapsedTime = now - prevTime;
if (elapsedTime > 30){
prevTime = now;
matrix.preRotate(2.0f, picture.getWidth() / 2, picture.getHeight() / 2);
}
canvas = null;
try {
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(picture, matrix, null);
}
}
finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
活动:
public class SurfaceViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MySurfaceView(this));
}
}
尝试使用超过360以避免重新启动。
我使用3600而不是360,这对我来说很好用:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="3600"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite"
android:duration="8000"
android:pivotX="50%"
android:pivotY="50%" />
ivBall.setOnClickListener(View.OnClickListener {
//Animate using XML
// val rotateAnimation = AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely)
//OR using Code
val rotateAnimation = RotateAnimation(
0f, 359f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
rotateAnimation.duration = 300
rotateAnimation.repeatCount = 2
//Either way you can add Listener like this
rotateAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {
}
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationEnd(animation: Animation?) {
val rand = Random()
val ballHit = rand.nextInt(50) + 1
Toast.makeText(context, "ballHit : " + ballHit, Toast.LENGTH_SHORT).show()
}
})
ivBall.startAnimation(rotateAnimation)
})
我也有这个问题,并尝试在xml中设置线性插值器而没有成功。对我有用的解决方案是在代码中将动画创建为RotateAnimation。
RotateAnimation rotate = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(5000);
rotate.setInterpolator(new LinearInterpolator());
ImageView image= (ImageView) findViewById(R.id.imageView);
image.startAnimation(rotate);
这很好用
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1600"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="358" />
反向旋转:
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1600"
android:fromDegrees="358"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="0" />
也许,这样的事情会有所帮助:
Runnable runnable = new Runnable() {
@Override
public void run() {
imageView.animate().rotationBy(360).withEndAction(this).setDuration(3000).setInterpolator(new LinearInterpolator()).start();
}
};
imageView.animate().rotationBy(360).withEndAction(runnable).setDuration(3000).setInterpolator(new LinearInterpolator()).start();
顺便说一句,你可以旋转超过360像:
imageView.animate().rotationBy(10000)...
尝试使用toDegrees="359"
,因为360°和0°是相同的。
修剪包裹<set>
-Element的<rotate>
-Element解决了这个问题!
丁字裤是萨拉菲!
所以你的Rotation Ccw.xml应该是这样的:
<?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:duration="2000"
android:fillAfter="false"
android:startOffset="0"
android:repeatCount="infinite"
android:interpolator="@android:anim/linear_interpolator"
/>
ObjectAnimator.ofFloat(view, View.ROTATION, 0f, 360f).setDuration(300).start();
试试这个。
无论我尝试了什么,我都无法使用代码(和setRotation)来平滑旋转动画。我最终做的是让学位变化如此之小,以至于小的停顿是不明显的。如果您不需要进行太多旋转,则执行此循环的时间可以忽略不计。效果是平稳旋转:
float lastDegree = 0.0f;
float increment = 4.0f;
long moveDuration = 10;
for(int a = 0; a < 150; a++)
{
rAnim = new RotateAnimation(lastDegree, (increment * (float)a), Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rAnim.setDuration(moveDuration);
rAnim.setStartOffset(moveDuration * a);
lastDegree = (increment * (float)a);
((AnimationSet) animation).addAnimation(rAnim);
}
正如汉利上面提到的那样,线性插值很好。但是如果旋转在一个集合中,你必须把android:shareInterpolator =“false”让它变得平滑。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
**android:shareInterpolator="false"**
>
<rotate
android:interpolator="@android:anim/linear_interpolator"
android:duration="300"
android:fillAfter="true"
android:repeatCount="10"
android:repeatMode="restart"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" />
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:duration="3000"
android:fillAfter="true"
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0"
android:toYScale="0" />
</set>
如果Sharedinterpolator不是false,则上面的代码会产生毛刺。