【问题标题】:Refresh android ImageButton after rotation旋转后刷新android ImageButton
【发布时间】:2017-06-10 12:30:38
【问题描述】:

我的片段中有一个ImageButton,它的背景设置为一个android 可绘制图标。在某些情况下,该按钮需要旋转(这可行)。我的问题是,当我在另一个活动中返回片段时,我希望将按钮“重置”到其原始位置。

这是按钮的设置方式:

@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() 方法中,但它们都不起作用。如何重置/重新加载原始按钮?

【问题讨论】:

    标签: android imagebutton


    【解决方案1】:

    clearAnimation()的代码

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

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

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

    【讨论】:

    • button.animate().rotation(0.0f); 成功了,我以某种方式认为这不会旋转已经旋转的按钮,但我想它会记住原始状态并重置它。感谢您的帮助,我将其移至onStop()
    猜你喜欢
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多