emm*_*mby 209 animation android view
我正在使用a 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" />
Run Code Online (Sandbox Code Playgroud)
当我将它应用于我的ImageView
使用时AndroidUtils.loadAnimation()
,效果很好!
spinner.startAnimation(
AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );
Run Code Online (Sandbox Code Playgroud)
一个问题是图像旋转似乎在每个周期的顶部暂停.
换句话说,图像旋转360度,暂停,然后再旋转360度等.
我怀疑问题是动画是使用像android:iterpolator="@android:anim/accelerate_interpolator"
(AccelerateInterpolator
)的默认插值器,但我不知道如何告诉它不插入动画.
如何关闭插值(如果这确实是问题)以使我的动画循环顺利进行?
Bak*_*yor 195
你是对的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" />
Run Code Online (Sandbox Code Playgroud)
并添加到您的动画XML:
android:interpolator="@anim/linear_interpolator"
Run Code Online (Sandbox Code Playgroud)
特别注意:如果你的旋转动画在一个集合中,设置插值器似乎不起作用.旋转顶部元素可以修复它.(这会节省你的时间.)
Rab*_*s G 83
我也有这个问题,并尝试在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);
Run Code Online (Sandbox Code Playgroud)
Lui*_*dez 35
这很好用
<?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" />
Run Code Online (Sandbox Code Playgroud)
反向旋转:
<?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" />
Run Code Online (Sandbox Code Playgroud)
Vit*_*nko 30
也许,这样的事情会有所帮助:
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();
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你可以旋转超过360像:
imageView.animate().rotationBy(10000)...
Run Code Online (Sandbox Code Playgroud)
Ash*_*man 13
ObjectAnimator.ofFloat(view, View.ROTATION, 0f, 360f).setDuration(300).start();
Run Code Online (Sandbox Code Playgroud)
尝试这个。
修剪包含<set>
-Element的<rotate>
-Element可以解决问题!
感谢Shalafi!
所以你的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"
/>
Run Code Online (Sandbox Code Playgroud)
以编程方式旋转对象。
//顺时针旋转:
public void rorate_Clockwise(View view) {
ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 180f, 0f);
// rotate.setRepeatCount(10);
rotate.setDuration(500);
rotate.start();
}
Run Code Online (Sandbox Code Playgroud)
//逆时针旋转:
public void rorate_AntiClockwise(View view) {
ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 0f, 180f);
// rotate.setRepeatCount(10);
rotate.setDuration(500);
rotate.start();
}
Run Code Online (Sandbox Code Playgroud)
视图是您的ImageView或其他小部件的对象。
rotation.setRepeatCount(10); 用于重复旋转。
500是您的动画持续时间。
小智 5
正如 hanry 上面提到的,放置线性迭代器就可以了。但如果旋转位于集合内,则必须放置 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>
Run Code Online (Sandbox Code Playgroud)
如果 Sharedinterpolator 不为 false,则上述代码会出现故障。
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)
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
215988 次 |
最近记录: |