Android 为什么';设置视图动画后是否设置可见性工作?

Android 为什么';设置视图动画后是否设置可见性工作?,android,animation,rotation,visibility,rotateanimation,Android,Animation,Rotation,Visibility,Rotateanimation,为什么文本视图不可见 以下是我的布局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="

为什么文本视图不可见

以下是我的布局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>
我的目标是旋转视图,然后通过设置setVisibility在代码中隐藏和显示它。下面的方法可以工作,但是setRotation仅在API级别11中可用。我需要一种方法在API级别10中实现它

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

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

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

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


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

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

<?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>

对我来说,调用视图的
clearAnimation
修复了这个问题。在我的例子中,我希望在将fillAfter设置为true进行转换后将视图设置回其原始位置。

在动画完成后设置可见性之前使用此选项:

anim.reverse();
anim.removeAllListeners();
anim.end();
anim.cancel();
anim是您的对象动画师

但如果您使用的是动画类,则只需执行以下操作:

view.clearAnimation();

在执行动画的视图上

那么,有没有办法隐藏旋转后持续存在的位图?我遇到了这个问题,可以确认clearAnimation是否成功。请有人接受这个答案,因为这是在几天的努力后保存**的答案;)谢谢!我确认这解决了问题。为了更有用,请在
AnimationListener
;)中的
onAnimationEnd()中使用它有人知道为什么需要这样做吗?这修复了我遇到的一个问题,视图没有从父视图中删除。jtietema,如果您能在回答中添加czaku的评论,那会更好,谢谢。这正是我想要的:-)
anim.reverse();
anim.removeAllListeners();
anim.end();
anim.cancel();
view.clearAnimation();