为什么在视图动画后setVisibility不起作用?

Zip*_*son 63 animation android visibility rotation rotateanimation

为什么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>
Run Code Online (Sandbox Code Playgroud)

..这是我的活动:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Jer*_*oen 162

对我来说,调用clearAnimationView修复了问题.在我的情况下,我想在使用fillAfter设置为true进行翻译后将View设置回原始位置.

  • 我遇到了这个问题并且可以确认clearAnimation可以解决这个问题. (6认同)
  • 我确认这解决了这个问题.更有用的是在`AnimationListener`;中的`onAnimationEnd()`中使用它 (4认同)
  • 有人知道为什么需要这样做吗?这解决了我无法从父视图中删除视图的问题。 (2认同)

ase*_*ovm 21

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


Chr*_*ght 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>
Run Code Online (Sandbox Code Playgroud)

然后代码变为:

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);
Run Code Online (Sandbox Code Playgroud)


小智 6

动画完成后,在setVisibility之前使用此选项:

anim.reverse();
anim.removeAllListeners();
anim.end();
anim.cancel();
Run Code Online (Sandbox Code Playgroud)

动画是你的ObjectAnimator

但是如果您正在使用Animation类,那么就这样做:

view.clearAnimation();
Run Code Online (Sandbox Code Playgroud)

在动画执行的视图上