【问题标题】:Why doesn't setVisibility work after a view is animated?为什么在视图动画后 setVisibility 不起作用?
【发布时间】:2012-01-31 04:19:16
【问题描述】:

为什么 textView 不可见?

这是我的布局 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
    android:id="@+id/tvRotate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Rotate Me"
/>
</LinearLayout>

..这是我的活动:

public class RotateMeActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tvRotate = (TextView) findViewById(R.id.tvRotate);

        RotateAnimation r = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        r.setDuration(0);
        r.setFillAfter(true);
        tvRotate.startAnimation(r);
        tvRotate.setVisibility(View.INVISIBLE);
    }
}

我的目标是旋转视图,然后通过设置 setVisibility 在代码中隐藏和显示它。以下方法有效,但 setRotation 仅在 API 级别 11 中可用。我需要一种在 API 级别 10 中执行此操作的方法。

tvRotate.setRotation(180);//instead of the RotateAnimation, only works in API Level 11
tvRotate.setVisibility(View.INVISIBLE);

【问题讨论】:

    标签: android animation rotation visibility rotateanimation


    【解决方案1】:

    对我来说,调用 View 的 clearAnimation 解决了这个问题。在我的情况下,我想在将 fillAfter 设置为 true 进行翻译后将 View 设置回其原始位置。

    【讨论】:

    • 我遇到了这个问题,可以确认 clearAnimation 可以解决问题。
    • 请有人接受这个答案,因为这是在挣扎了几天后拯救你的人的答案;)谢谢!
    • 我确认这可以解决问题。为了更有帮助,请在 onAnimationEnd() 中使用它 AnimationListener ;)
    • 有谁知道为什么需要这个?这解决了我遇到的一个问题,即视图没有从父级中删除。
    • jtietema,如果你能在你的答案中添加 czaku 的评论会更好,谢谢。
    【解决方案2】:

    所有动画(在 android 3.0 之前)实际上都应用于位图,它是视图的快照,而不是原始视图。当您将 fill after 设置为 true 时,这实际上意味着位图将继续显示在屏幕上,而不是您的视图上。这就是使用setVisibility 时可见性不会改变的原因,也是您的视图不会在其新(旋转)边界中接收触摸事件的原因。 (但由于您旋转 180 度,这不是问题)。

    【讨论】:

    • 那么,有没有办法隐藏旋转后仍然存在的位图?
    【解决方案3】:

    解决此问题的另一种方法是将动画视图包装在另一个视图中并设置该包装视图的可见性。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" >
        <FrameLayout 
            android:id="@+id/animationHoldingFrame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                 android:id="@+id/tvRotate"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Rotate Me"
            />
        </FrameLayout>
    </LinearLayout>
    

    然后代码变成这样:

    TextView tvRotate = (TextView) findViewById(R.id.tvRotate);
    FrameLayout animationContainer = (FrameLayout)findViewById(R.id.animationHoldingFrame)
    
    RotateAnimation r = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    r.setDuration(0);
    r.setFillAfter(true);
    tvRotate.startAnimation(r);
    animationContainer.setVisibility(View.INVISIBLE);
    

    【讨论】:

    • 正是我想要的:-)
    【解决方案4】:

    在动画完成后的 setVisibility 之前使用这个:

    anim.reverse();
    anim.removeAllListeners();
    anim.end();
    anim.cancel();
    

    其中 anim 是您的 ObjectAnimator

    但如果您使用的是 Animation 类,那么只需执行以下操作:

    view.clearAnimation();
    

    在执行动画的视图上

    【讨论】:

      【解决方案5】:

      我最终需要 API 级别 11 并使用 setRotation 来完成此操作。这似乎是一个非常简单的要求,但在 Honeycomb 之前无法完成。我想做的只是旋转一个按钮,然后隐藏/显示它。

      【讨论】:

        【解决方案6】:

        我想出了一个解决方法:基本上就在你调用 setVisibility(View.GONE) 之前,做一个持续时间 = 0 setFillAfter(false) 的动画,并将从/到的角度设置为当前的旋转角度。

        这将清除 setFillAfter 位图并允许视图消失。

        【讨论】:

          猜你喜欢
          • 2023-03-28
          • 2014-12-02
          • 2017-04-14
          • 1970-01-01
          • 2014-10-06
          • 2013-04-17
          • 2019-11-18
          • 2022-11-11
          • 1970-01-01
          相关资源
          最近更新 更多