でハンドラからUIを更新し、私はタイマーとして働くべきもの、との問題を抱えています。私はAndroidの "タイマー"を使用してこの記事を読んでいます: http://developer.android.com/resources/articles/timed-ui-updates.htmlは別のRunnableを
私は自分のレイアウトにTextViewとImageViewを持っています。私はこのImageViewにAnimationDrawableを持っています。 AnimationDrawableクラスをオーバーライドしました。アニメーションがいつ完了するかを知りたいからです。しかし、私のアニメーションが終了したときに呼びたい実行可能ファイルは正しく動作します。しかし、TextViewを1秒ごとに更新したい場合、別の実行可能ファイル(以下のコードで)が1回だけ呼び出されます(すべてのアニメーション中に数字 "1"が表示されます)。
TextView timeFlow;
int seconds;
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
seconds++;
timeFlow.setText(String.valueOf(seconds));
}
};
private void startAnimation() {
image = (ImageView) this.findViewById(R.id.image);
recordImage.setBackgroundResource(R.drawable.record_animation);
timeFlow = (TextView) this.findViewById(R.id.time_flow);
timeFlow.setText("...");
image.post(new Runnable() {
@Override
public void run() {
CustomAnimationDrawable currentAnimation = new CustomAnimationDrawable((AnimationDrawable) recordImage.getBackground());
currentAnimation.setOnFinishCallback(runnable);
recordImage.setBackgroundDrawable(currentAnimation);
currentAnimation.start();
handler.removeCallbacks(mUpdateTimeTask);
handler.postDelayed(mUpdateTimeTask, 100);
}
});
}
ああ、と 'handler.postDelayed(mUpdateTimeTask、100)を呼び出すことを忘れないでください。あなたのrun()メソッドの最後に'。 Handler.postDelayedはワンタイムショットです。私はそれを反映するために私の応答を編集します。 – Jason
本当にありがとうございます。しかし、私にはまだ1つの問題があります。私のアニメーションは完了し、実行可能なAnimationDrawable OnFinishCallbackが呼び出されましたが、「タイマー」はまだ動作しています。私はアニメーションが止まったときにそれを止めるべきだと思う。 – woyaru
ああ、それはoncly 'handler.removeCallbacks(mUpdateTimeTask)を使用し、とても簡単だった;'実行可能にAnimationDrawable終了後に呼び出さ。 – woyaru