java – 具有无限旋转口吃的ObjectAnimator
作者:互联网
我有一个animator,infinite_rotation,定义为:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="rotation"
android:repeatCount="infinite"
android:valueFrom="0"
android:valueTo="360"
android:duration="2000" />
</set>
当时间(时间不确定)到来我不再需要它时,我调用infinite_animator.cancel().然后在其布局容器上播放fade_out动画:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="alpha"
android:repeatCount="0"
android:valueTo="0.0"
android:duration="1000" />
</set>
生成的动画是,旋转按预期停止,但在淡出时停顿.我错过了什么?
以下是它的外观:
更新:
我在使用Kitkat OS的旧三星Tab 4上测试了上述问题.我刚刚使用Marshmallow OS测试了一款相当新的三星Tab 4.动画效果很好.所以我想更好的问题是如何修复旧设备/操作系统上的草率动画?
这是动画调用:
private void animateRefreshButton() {
ImageView iv_refresh = (ImageView) findViewById(R.id.iv_refresh);
if(infinite_animator == null) {
infinite_animator = AnimatorInflater.loadAnimator(this, R.animator.refresh_rotate);
}
infinite_animator.setTarget(iv_refresh);
infinite_animator.start();
}
当应用程序确定刷新完成时,将启动hideRefreshButton().这是取消和淡出动画的调用:
private void hideRefreshButton() {
if(infinite_animator != null) {
infinite_animator.cancel();
}
Animator anim = AnimatorInflater.loadAnimator(this, R.animator.refresh_fadeout);
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {}
@Override
public void onAnimationEnd(Animator animation) {
framelayout_container_of_iv_refresh.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});
anim.setTarget(framelayout_container_of_iv_refresh);
anim.start();
}
解决方法:
我个人不会因为“problems” it has而使用Animation API.相反,在这种情况下我会使用Animators API.
有这个片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.imageView);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, View.ROTATION, 0.0f, 360.0f);
objectAnimator.setDuration(2000);
objectAnimator.setRepeatCount(Animation.INFINITE);
objectAnimator.setInterpolator(new LinearInterpolator());
objectAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
view.animate()
.alpha(0.0f)
.setDuration(1000);
}
});
objectAnimator.start();
view.setOnClickListener((v) -> objectAnimator.cancel());
}
然后这将是输出:
我已经在模拟器(API 19)上测试过:按预期工作.
标签:java,android,animation,android-animation,objectanimator 来源: https://codeday.me/bug/20190611/1216894.html