2011-02-12 22 views
11

私は、車の "直進、左折、まっすぐ、..."のような動き(訳語)と回転を行うさまざまなステップのアニメーションを作りたいと思っています。プログラムでRotateAnimationsを実行する方法の完全な例?

AnimationSetでこれを行うことはできますが、「RELATIVE_TO_SELF」設定を使用して車の中央付近を回転すると失敗します。私はこのことについて約

Animation a = new RotateAnimation(0,90,Animation.RELATIVE_TO_SELF,0.5f,...) 

を知っています。それでも回転は画面(またはキャンバス?)の左上隅の周りに発生します。

現在、私はアニメーションステップごとに手動で位置を追跡することでこれを解決していますが、これは最適ではありません。

私は私の最初のレイアウト設定が偽であることを疑う

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
> 
    <FrameLayout 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent"> 

     <!-- view that draws some background --> 
     <de.bsd.turtlecar.SampleView android:id="@+id/graph_view" 
       android:layout_height="350px" 
       android:layout_width="fill_parent" 
       android:visibility="invisible" 
       /> 

     <!-- the car --> 
     <ImageView android:id="@+id/car_view" 
        android:src="@drawable/turtle_car" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:visibility="invisible"/> 


    </FrameLayout> 

    <Button ... onClick="run" ... /> 
</LinearLayout> 

これは、(別の場所に表示する必要があり、左上隅に車を示して - アニメーションが、後に始まる基本的にそしてそれが必要。後で移動する)。私は実行ボタンによってトリガーされた私のコードで

:ここでそう時点で

ImageView carView = (ImageView) findViewById(R.id.car_view); 
    print(carView); 
    AnimationSet animationSet = new AnimationSet(true); 

    TranslateAnimation a = new TranslateAnimation(
      Animation.ABSOLUTE,200, Animation.ABSOLUTE,200, 
      Animation.ABSOLUTE,200, Animation.ABSOLUTE,200); 
    a.setDuration(1000); 
    animationSet.addAnimation(a); 

    RotateAnimation r = new RotateAnimation(0f, -90f,200,200); // HERE 
    r.setStartOffset(1000); 
    r.setDuration(1000); 
    animationSet.addAnimation(r); 
    ... 

、回転は動作しますが、私はトラックを維持する必要があります。 RELATIVE_TO_SELFを回転すると、回転は画面の(0,0)回りに発生します。

その他の質問:アニメーションが終了した後で車をスクリーンに保持するにはどうすればよいですか?

私は間違ったトラックにいるのですか?

答えて

17

アニメーションにsetFillAfter(true)を追加してみてください。それは確かに最終的な場所に車を維持し、それはあまりにも

TranslateAnimation a = new TranslateAnimation(
     Animation.ABSOLUTE,200, Animation.ABSOLUTE,200, 
     Animation.ABSOLUTE,200, Animation.ABSOLUTE,200); 
a.setDuration(1000); 
a.setFillAfter(true); //HERE 
animationSet.addAnimation(a); 

RotateAnimation r = new RotateAnimation(0f, -90f,200,200); // HERE 
r.setStartOffset(1000); 
r.setDuration(1000); 
r.setFillAfter(true); //HERE 
animationSet.addAnimation(r); 
... 
+13

注お使いの回転ポイントの問題を解決することがあります。最後のステップを、ここで 'carView.startAnimation(animationSet)を呼び出すことです;' – Nilzor

関連する問題