2012-02-22 27 views
3

私はAndroidで助けが必要です。 私はスロットマシンゲームを作成しようとしています。だから、私はAnimationDrawableを3つのホイールに使用しています。ここでanimwheel.xmlファイルは次のとおりです。Android:AnimationDrawableで現在のフレームを取得する方法

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/animWheel" 
android:oneshot="false" > 

<item 
    android:drawable="@drawable/fruits1" 
    android:duration="200"/> 
<item 
    android:drawable="@drawable/fruits2" 
    android:duration="200"/> 
<item 
    android:drawable="@drawable/fruits3" 
    android:duration="200"/> 
</animation-list> 

私は属性アンドロイドを使用して、メインのレイアウトで3つの異なるビュー(3 whells)でアニメーションリストをパット:背景

android:background="@drawable/animwheel" 

、ユーザーは3つの異なるボタンをクリックしてアニメーションを停止できます。問題は、ビューが現在表示している画像がどの画像か分かりますか? getCurrentFrame()メソッドなどがありますか?

ありがとうございます!

+2

ここでAnimationDrawable(http://goo.gl/e7kFU)を読んだ後、現在のアニメーションフレームを追跡する変数名mCurFrameがあることがわかります。しかし残念ながら、それは私的です。彼らはそれを公開として設定するとより便利になるでしょう。 – anticafe

答えて

14

「getCurrentFrame()」のような関数は、アプリケーションを実行するたびに変更される16進数値を返すので、すべての関数を試しても成功しません。 ( 'fruits1')のイメージの名前を取得する代わりに、フレームの位置を取得して、ユーザーがフレーム 'fruits2'で停止したアニメーションを停止する必要があります。 0がフレーム 'fruits1'であるため、フレームの番号は1です。どうしますか?

// Create the animation 
myImage.setBackgroundResource(R.anim.myanimation); 
myAnimation = (AnimationDrawable) myImage.getBackground(); 

アニメーションを停止すると、あなたはこのような何か:

myAnimation.stop(); 

// The variable that will guard the frame number 
int frameNumber; 

// Get the frame of the animation 
Drawable currentFrame, checkFrame; 
currentFrame = myAnimation.getCurrent(); 

// Checks the position of the frame 
for (int i = 0; i < myAnimation.getNumberOfFrames(); i++) { 
    checkFrame = myAnimation.getFrame(i); 
    if (checkFrame == currentFrame) { 
     frameNumber = i; 
     break; 
    } 
} 

ので、変数「フレーム番号」は、0、あなたのケースでは、1、または2のフレームの数を守るだろうし フレームがたくさんある場合は、配列を使用して、フレームの番号に基づいて名前( 'fruits1'、 'fruits2' ...)を返す関数を実行できます。

関連する問題