67

为什么 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);
4

6 回答 6

171

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

于 2012-03-27T09:07:33.300 回答
21

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

于 2011-12-31T19:19:25.220 回答
8

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

<?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);
于 2012-02-25T22:58:58.840 回答
8

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

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

其中 anim 是您的 ObjectAnimator

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

view.clearAnimation();

在执行动画的视图上

于 2016-05-20T10:00:10.787 回答
1

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

于 2012-01-02T17:43:48.100 回答
1

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

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

于 2012-01-25T01:30:50.117 回答