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

clearAnimation私にとっては、ビューを呼び出すことで問題が解決しました。私の場合、fillAfter を true に設定して翻訳を行った後、View を元の位置に戻したいと考えていました。

于 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

これに対する回避策を思いつきました。基本的に、setVisibility(View.GONE)を呼び出す直前に、duration = 0 setFillAfter(false)でアニメーションを実行し、現在の回転角度から/への角度を設定します。

これにより、setFillAfterビットマップがクリアされ、ビューが消えます。

于 2012-01-25T01:30:50.117 に答える
1

最終的に API レベル 11 を要求し、これを達成するために setRotation を使用しました。これは非常に単純な要件のように思えますが、ハニカムの前には実行できません。私がやりたかったのは、ボタンを回転させてから非表示/表示することだけでした。

于 2012-01-02T17:43:48.100 に答える