0

I wanted to display 9 images one after the other. I have included the 9 images as an array:

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));

This is what I have tried:

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);
    }

}

There is a delay between the first and the second images. I am not able to introduce a delay between the rest. I have no clue about introducing the delay. Any suggestions would be appreciated.

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 但你不能轻易地动态改变它——比如改变速度或其他东西。我刚刚为此编写了一个简单的类,因此我可以为一个 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 回答