4

この画像のアニメーションを作成しようとしています。

バーなし

この画像になります:

バー

3つのバーが1...2 ... 3 ...のようにフェードインし、その後なしに戻ります。この時点で、アニメーションが繰り返されます。これが私が仕事をしようとしたものです:

    AlphaAnimation anim = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.fade_in);
    anim.setRepeatMode(Animation.RESTART);
    anim.setRepeatCount(Animation.INFINITE);
    anim.setStartOffset(3000);

    LayoutAnimationController controller = new LayoutAnimationController(anim, 0.5f);

    rl.setLayoutAnimation(controller);

    rl.startLayoutAnimation();

これはほぼ機能しますが、問題は、これらのWi-Fiバーのビューグループの最初の子が、最後の子がアニメーションを完了するのを待たずに消えることです。結果は、素敵な1..2..3ではなく、1 ... 2 ... 3..1..2.3 ... 1 ... 3...1..2.3のようなビートになります。 .1..2..3。サイクルが完了するまでコントローラーを待機させてから、最初の子を再びオフにする方法を見つける必要があります。私はこれに関する解決策を見つけることができませんでした、誰かがいくつかの入力を持っていますか?私はこれを行う方法を完全に変更することにオープンです。このLayoutAnimationControllerクラスは、この時点までの最良の方法のように見えました。

ありがとう!

4

1 に答える 1

2

画像にアニメーションを使わなければならないときは、フレームを用意しました。私はこのクラスでAnimatioDrawableを使用しました:

* This class is a hack to simply get an on finish callback for an
* AnimationDrawable.
*
*/
public class CustomAnimationDrawable extends AnimationDrawable {
// We need to keep our own frame count because mCurFrame in AnimationDrawable is private
private int mCurFrameHack = -1;

// This is the Runnable that we will call when the animation finishes
private Runnable mCallback = null;

/*
* We override the run method in order to increment the frame count the same
* way the AnimationDrawable class does. Also, when we are on the last frame we
* schedule our callback to be called after the duration of the last frame.
*/
@Override
public void run() {
super.run();

mCurFrameHack += 1;
if (mCurFrameHack == (getNumberOfFrames() - 1) && mCallback != null) {
scheduleSelf(mCallback, SystemClock.uptimeMillis() + getDuration(mCurFrameHack));
}
}

/*
* We override this method simply to reset the frame count just as is done in
* AnimationDrawable.
*/
@Override
public void unscheduleSelf(Runnable what) {
// TODO Auto-generated method stub
super.unscheduleSelf(what);
mCurFrameHack = -1;
}

public void setOnFinishCallback(Runnable callback) {
mCallback = callback;
}

public Runnable getOnFinishCallback() {
return mCallback;
}

}

そして私のコードでは、次のように使用します。

        CustomAnimationDrawable staticAnimation = new CustomAnimationDrawable();
    //add frames with resources and duration. step is just a class of mine
        staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
    staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
    staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration());
        staticAnimation.setOneShot(false);

        imgView.setBackgroundDrawable(staticAnimation);
staticAnimation.start();

おそらくそこにある最良の解決策ではありませんが、それは私のニーズにはうまくいきました。

于 2012-06-28T20:22:33.733 に答える