0

9枚の画像を次々と表示したかったのです。9つの画像を配列として含めました。

imageHolders = new ArrayList<ImageView>();
    imageHolders.add((ImageView) view.findViewById(R.id.imgOne));
    imageHolders.add((ImageView) view.findViewById(R.id.imgTwo));
    imageHolders.add((ImageView) view.findViewById(R.id.imgThree));
    imageHolders.add((ImageView) view.findViewById(R.id.imgFour));
    imageHolders.add((ImageView) view.findViewById(R.id.imgFive));
    imageHolders.add((ImageView) view.findViewById(R.id.imgSix));
    imageHolders.add((ImageView) view.findViewById(R.id.imgSeven));
    imageHolders.add((ImageView) view.findViewById(R.id.imgEight));
    imageHolders.add((ImageView) view.findViewById(R.id.imgNine));

これは私が試したことです:

public void handleMessage(Message msg) {
        int currentImage = 0;
        int nextImage = 0;
        // Logic to change the images
        for (final ImageView imageView : imageHolders) {
            currentImage = Integer.parseInt(imageView.getTag().toString());
            if (currentImage > 1) {
                nextImage = currentImage - 1;
            } else {
                nextImage = 9;
            }
                     imageView.setTag(""+nextImage);

            new CountDownTimer(10000, 1000) {

                 public void onTick(long millisUntilFinished) {

                 }

                 public void onFinish() {
                        imageView.setVisibility(VISIBLE);
                 }
              }.start();        

        }
        super.handleMessage(msg);
    }

}

最初の画像と2番目の画像の間に遅延があります。残りの間に遅延を導入することはできません。遅延を導入することについての手がかりはありません。任意の提案をいただければ幸いです。

4

3 に答える 3

3

これを行うAnimationDrawableを使用するだけで、android:duration属性を使用して期間(ミリ秒単位)を設定できます。

于 2012-07-01T17:02:17.293 に答える
1

AnimationDrawableがおそらく最良の答えですが、別の解決策についてはAsyncTaskを見ることができます。

http://developer.android.com/reference/android/os/AsyncTask.html

次に、バックグラウンドで一定時間スリープしてから、次の画像を表示できます。

ただし、このタスクが完了したときに処理するイベントが必要な場合があり、リスナーで次のタスクを呼び出します。

于 2012-07-01T17:02:22.860 に答える
1

単純な静的アニメーション(すべての画像が同じ位置にある)を作成したい場合は、AnimationDrawableを使用できますが、速度の変更など、動的に簡単に変更することはできません。1つのImageViewに複数のアニメーションを設定して速度を変更できるように、このための簡単なクラスを作成しました。

public class ImageSequence extends ImageView {

ArrayList<Drawable> draws = new ArrayList<Drawable>();
ArrayList<Integer> bnds = new ArrayList<Integer>(); //bounds of sequences - index of first and last frame.
ArrayList<String> names = new ArrayList<String>(); //sequences names.
Timer timer = new Timer(true);
TimerTask task;
int p = -1; //current playback position.
int cS = -1; //current sequence.


public ImageSequence(Context context) {
    super(context);
    createTask();
}

public void addSequence(String name, int[] res){
    names.add(name);
    bnds.add(draws.size());
    bnds.add(draws.size() + res.length - 1);
    for (int i = 0; i < res.length; i++){
        draws.add(getContext().getResources().getDrawable(res[i]));
    }
}

public void play(String sequence, float speed){     
    cS = -1;
    for(int i = 0; i < names.size(); i++){
        if(names.get(i) == sequence){ cS = i; break;}
    }
    if(cS<0) return;
    p = -1;
    task.run();
    task.cancel();
    if(speed <= 0) return;
    createTask();
    timer.schedule(task, (long) (1000/speed), (long) (1000/speed));
}

public void stop(){
    task.cancel();
}

public void cancelTimer(){
    timer.cancel();
}

private void createTask() {
    task = new TimerTask(){
        public void run() {
            p++;
            mHandler.obtainMessage(1).sendToTarget();
        }
    };
}

public Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        int l = p;
        if(l > bnds.get(cS*2+1) || l < bnds.get(cS*2)) l = bnds.get(cS*2);
        p = l;
        ImageSequence.this.setImageDrawable(draws.get(l));
    }
};
}

外部からは次のように使用できます。

seq.addSequence("orange", new int[]{R.drawable.ic_oval_diode_orange, R.drawable.ic_oval_diode_off});
seq.addSequence("green", new int[]{R.drawable.ic_oval_diode_green, R.drawable.ic_oval_diode_off});
seq.addSequence("red", new int[]{R.drawable.ic_oval_diode_red, R.drawable.ic_oval_diode_off});
seq.play("red", 4);

私はAndroidとJavaの経験があまりありませんが、それは機能し、私のために仕事をします。それが誰かを助けることを願っています。

于 2015-05-17T18:23:53.603 に答える