2011-12-31 8 views
55

なぜtextViewが見えなくなるのですか?ここでビューがアニメーション化された後にsetVisibilityが機能しないのはなぜですか?

は私のレイアウト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> 

..andここでは私の活動です:

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すべてのアニメーション(3.0アンドロイド前)は、実際にあるビットマップに適用されている10

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

答えて

18

APIレベルで、私はそれを行うための方法を必要とする唯一のAPIレベル11で利用可能です元のビューではなくビューのスナップショット。あとで塗りつぶしをtrueに設定すると、実際にはビューの代わりに画面にビットマップが表示され続けることを意味します。このため、setVisibilityを使用しても表示の変更が行われない理由と、新しい(回転された)境界で表示される予定が表示されない理由があります。 (ただし、あなたは180度回転しているので問題はありません)。

+1

このように、bを隠す方法はありますかitmapは回転した後も維持されますか? – ZippyFerguson

+0

同じ問題... – SunnySonic

1

私はAPIレベル11を必要とし、これを達成するためにsetRotationを使用しました。これは前ハニーコムではできない非常に単純な要件のようです。私がしたかったのは、ボタンを回転させて隠す/表示することでした。

1

私はsetVisibility(View.GONE)を呼び出す前に、duration = 0のアニメーションをsetFillAfter(false)に設定し、/から現在の回転角度に設定します。

これは、setFillAfterビットマップを消去し、ビューを削除できるようにします。

7

これを回避する別の方法は、アニメーション表示を別のビューにラップし、そのラッパー表示の表示を設定することです。その後、

<?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問題を修正を呼び出すための

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); 
+0

私が探していたもの:-) – Couitchy

151

を。私の場合は、fillAfterをtrueに設定して翻訳を行った後、Viewを元の位置に戻したいと思っていました。 setVisibility前

+6

私はこの問題を抱えていて、clearAnimationがそのトリックを行うことを確認できます。 –

+0

誰かがこの回答を受け入れるようにしてください。それは、苦しんだ日の後にあなたのお金を節約するものです;)thx! –

+4

これで問題が解決されることが確認されました。'AnimationListener'の' onAnimationEnd() 'でもっと役に立ちます); – czaku

5

使用これをアニメーションが完了した後:

アニメーションは、あなたのObjectAnimator

ですが、あなたはアニメーションクラスを使用している場合は、ちょうど行う

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

:上

view.clearAnimation(); 

をアニメーションが実行されたビュー

関連する問題