2012-01-23 15 views
0

私は、複合画面に、RelativeLayoutを使って画面の右上隅に付いているボタンがいくつか混在しています。私はこのビューを右に動かすには、 "open-close"ボタンをクリックし、ユーザーがそのボタンの1つを選択/クリックするか、または "close-close"ボタンで再びクリックしてから、見えなくなる。問題は、それが左にアニメートしてから元の場所に戻ることです!この問題を解決するにはどうすればよいですか?Android TranslateAnimation animation

コード:

public class AlarmCommandComponent extends LinearLayout { 

    ImageButton carOffButton; 
    ImageButton carOnButton; 
    ImageButton armButton; 
    ImageButton disArmButton; 
    ImageButton openCloseButton; 
    LayoutAnimationController controller; 
    Animation animation; 

    public AlarmCommandComponent(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.car_alarm_view, this); 

     openCloseButton = (ImageButton) findViewById(R.id.alarmOpenCloseButton); 

     AnimationSet set = new AnimationSet(true); 
     animation = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, //fromXType 
       0.0f,      //fromXValue 
       Animation.RELATIVE_TO_SELF, //toXType 
       -1.0f,      //toXValue 
       Animation.RELATIVE_TO_SELF, //fromYType 
       0.0f,      //fromYValue 
       Animation.RELATIVE_TO_SELF, //toYType 
       0.0f);      //toYValue 
     animation.setDuration(500); 
     set.addAnimation(animation); 
     LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f); 
     this.setLayoutAnimation(controller); 
     this.setPadding(0, 7, 7, 10); 
     this.setBackgroundColor(Color.BLACK); 

     openCloseButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       openCloseButton_onClick(v); 
      } 
     }); 
    } 

    public void openCloseButton_onClick(View v) { 
     this.startAnimation(animation); 
    } 

} 

任意のアイデア?

答えて

2

デフォルトでは、アニメーションはオブジェクトを初期状態にリセットします。あなたは指定する必要がfillAftertrueへ:あなたは新しい位置に物理的にだけでなく、それをシフトする必要が翻訳した後、ボタンを機能させるためには

animation.setFillAfter(true); 
+0

感謝を!それは動作しますが、私の「クローズド・クローズ」ボタンはこのような状況ではクリックに反応しないようです。私は正しい? –

+0

プレハニカムアニメーションフレームワークは、レイアウトの視覚的な部分だけを移動します。物理的な場所は同じで、あなた自身で移動する必要があります。 HoneycombはAnimatorクラスを導入しました。具体的にはPropertyAnimatorが導入されました。これはオブジェクトのアニメーション化と物理的な移動の両方を行います。 – DeeV

+0

私はAndroid 2.1で作業していますので、実際のオブジェクトをスナップショットだけでなくアニメーションで移動するにはどうすればよいですか? –

1

。あなたはプログラムでレイアウトの位置を変更することでこれを行うことができます。アニメーションリスナー&内部onAnimationEndを設定

はこれを行う -

​​

をこれを読んAPIをハニカムする前に存在しているアニメーションの制約についての詳細を知るためには - http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html

+0

私はスライド・ドロワーとフェード・イン・アウトで仕事をしました!私は、プログラムのレイアウトを扱うことは、中規模から大規模のアプリケーションでは良い習慣ではないと思います。 –

+0

しかし、スライド式の引き出しには、下から上、右から左に切り替えることができるという制約があります。引き出しが左から右へ、または上から下へ開く必要がある場合は機能しません。とにかく、幸運! – alchemist