旋转后刷新 android ImageButton

Refresh android ImageButton after rotation

我的片段中有一个 ImageButton,其背景设置为 android 可绘制图标。在某些情况下,该按钮需要旋转(这有效)。我的问题是,当我在进入另一个 activity 后回到片段时,我希望按钮 "reset" 回到原来的位置。

按钮的设置方式如下:

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    button = (ImageButton)view.findViewById(R.id.doButtonThing);
    button.setOnClickListener(this);
    if (condition) {
        button.animate().rotation(90f).setInterpolator(new AccelerateDecelerateInterpolator());
    }
}

我试过使用:

button.clearAnimation();
button.invalidate();
button.postInvalidate();

onResume() 方法中,但其中 none 有效。我怎样才能reset/reload原来的按钮?

clearAnimation()的代码

/**
 * Cancels any animations for this view.
 */
public void clearAnimation() {
    if (mCurrentAnimation != null) {
        mCurrentAnimation.detach();
    }
    mCurrentAnimation = null;
    invalidateParentIfNeeded();
}

这给我的印象是它清除了当前的 运行 动画(尽管我不确定)。您可以使用 button.animate().rotation(0.0f); 来反向旋转按钮,而不是 button.clearAnimation();

此外,我建议您稍后再调用该函数,而不是 onResume(),也许 onStop()。在生命周期中,onResume() 出现在 onViewCreated(final View view, Bundle savedInstanceState) 之后,在 onResume() 中调用它会使您的动画无效。