2010-12-06 10 views
13

Androidプラットフォームが初めてです。私は、次のしているJavaコードでAndroid AnimationDrawableアニメーションの再生時間をオンザフライで変更するにはどうすればよいですか?

<animation-list 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:oneshot="false"> 
<item android:drawable="@drawable/image_1" android:duration="200" /> 
<item android:drawable="@drawable/image_1_25" android:duration="200" /> 
<item android:drawable="@drawable/image_1_5" android:duration="200" /> 
<item android:drawable="@drawable/image_1_75" android:duration="200" /> 
<item android:drawable="@drawable/image_2" android:duration="200" /> 
<item android:drawable="@drawable/image_2_25" android:duration="200" /> 
<item android:drawable="@drawable/image_2_5" android:duration="200" /> 
<item android:drawable="@drawable/image_2_75" android:duration="200" /> 
<item android:drawable="@drawable/image_3" android:duration="200" /> 
<item android:drawable="@drawable/image_3_25" android:duration="200" /> 
<item android:drawable="@drawable/image_3_5" android:duration="200" /> 
<item android:drawable="@drawable/image_3_75" android:duration="200" /> 
<item android:drawable="@drawable/image_4" android:duration="200" /> 
<item android:drawable="@drawable/image_4_25" android:duration="200" /> 
<item android:drawable="@drawable/image_4_5" android:duration="200" /> 
<item android:drawable="@drawable/image_4_75" android:duration="200" /> 
</animation-list> 

:私が持っているXMLファイルで

:私は私のアプリでAminationDrawableを使用して16「フレーム」のセットをアニメーション化するには、次のように使用しています

まず、クラスを宣言し、アニメーションを設定するonCreate()メソッドを追加します。その後、

public class MyNewActivity extends Activity 
{ 
    // member variables (accessible from within class methods below). 
    AnimationDrawable mainAnimation; 
    long mSpeed = 50; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_widget); 
     // set up image 
     ImageView mainImage = (ImageView) findViewById(R.id.image_widget); 
     mainImage.setBackgroundResource(R.drawable.animated_image); 
     mainAnimation = (AnimationDrawable) mainImage.getBackground(); 
    }; 
<...snip...> 

...後でユーザーが、私はアニメーションの移動を開始するには、次の呼び出しボタンを押したときに、私、私の描画を開始する上:

private void start() 
{ 
    // start the image rotating. 
    if (mainAnimation.isRunning()) 
     mainAnimation.stop(); 

    int numFrames = mainAnimation.getNumberOfFrames(); 
    for (int ii = 0; ii < numFrames; ii++) 
    { 
     // change the animation speed. 
     Drawable d = mainAnimation.getFrame(ii); 
     mainAnimation.scheduleDrawable(d, mainAnimation, mSpeed); 
    } 
} 
<...snip...> 

だから、他の場所で、私はに場所を持っているコードでメンバ変数mSpeedを調整します。私がこれをしてstart()を呼び出すと、アニメーションが始まりますが、速度は常に同じです(本質的には上記のXMLで定義されたものです)。私の質問は、このアニメーションを移動するフレームの「期間」を変更する方法ですユーザー入力に基づいてより速く/遅くなりますか?「継続時間」の状態を変更する方法はなく、上記のScheduleDrawable()コールが図面のフレーム期間を変更するという前提の下にありました。

+1

良い質問です。私は開発者のドキュメントを見ることからこれを行う方法はありませんが、私はその答えを見ることに興味があります。 – kcoppock

答えて

20

私は同じ問題を抱えていましたが、それAnimationDrawableを拡張して自分のAnimationDrawableクラスを実装することによりAnimationDrawableのsource codeを読み取り、Run()メソッドをオーバーライドして、私は次のように期間を設定することができますsetDuration()メソッドを追加します。

元の実行方法を見直しても、同じことを行うが、フレームを追加するときに指定した時間をscheduleSelf(this, SystemClock.uptimeMillis() + duration);と呼んで変更した。私はすべての私のフレームのために同じを使用するので、また、期間を追加するが、新しい期間の配列を使用することができます。

import android.graphics.drawable.AnimationDrawable; 
import android.os.SystemClock; 

public class MyAnimationDrawable extends AnimationDrawable { 

    private volatile int duration;//its volatile because another thread will update its value 
    private int currentFrame; 

    public MyAnimationDrawable() { 
     currentFrame = 0; 
    } 

    @Override 
    public void run() { 
     int n = getNumberOfFrames(); 
     currentFrame++; 
     if (currentFrame >= n) { 
      currentFrame = 0; 
     } 
     selectDrawable(currentFrame); 
     scheduleSelf(this, SystemClock.uptimeMillis() + duration); 
    } 

    public void setDuration(int duration) { 
     this.duration = duration; 
     //we have to do the following or the next frame will be displayed after the old duration 
     unscheduleSelf(this); 
     selectDrawable(currentFrame); 
     scheduleSelf(this, SystemClock.uptimeMillis()+duration); 
    } 
} 

これは私の最初の回答です。私はそれがあなたを助け、それがうまく説明されることを願っています。

+0

助けてくれてありがとう!それは完璧な意味合いがあります。 – markmalin

+0

あなたはこの回答を受け入れることができますので、私はチャットに入るための評判がさらに高くなります。 – Amal

+3

'AnimationDrawable'クラスを' MyAnimationDrawable'にキャストしましたか? 私は 'rocketImage.getBackground()'メソッドから返されるオブジェクトに問題があります。 <! - language:lang-js - > ' イメージビューrocketImage =(ImageView)findViewById(R.id.rocket_image); rocketImage.setBackgroundResource(R.drawable.rocket_thrust); rocketAnimation =(AnimationDrawable)rocketImage.getBackground(); ' 何か提案がありますか? – Gurnetko

関連する問題