如何通过计时器使我的图像可见?

时间:2016-07-23 12:58:44

标签: java android time

我想在android studio上使用多个图像制作简单的动画 我有3张图片(img1,img2,img3) 我希望img1首先可见,然后在半秒后img1看不见和img2可见,然后半秒img2看不见和img3可见,然后半秒后img3看不见和img1可见,所以返回第一个图像像圆圈 1> 2> 3> 1> 2> 3> 1> 2> 3到无限时间,所以我该怎么做呢,

1 个答案:

答案 0 :(得分:0)

class ImageAnimation {

    Activity activity;
    android.os.Handler handler;
    ImageView[] images = new ImageView[3];//three images array
    int current = 0;//current image iterator


    public ImageAnimation(Activity activity) {

            handler = new android.os.Handler();


            images[0] = (ImageView) activity.findViewById(R.id.you_image1_id);
            images[1] = (ImageView) activity.findViewById(R.id.you_image2_id);
            images[2] = (ImageView) activity.findViewById(R.id.you_image3_id);

            current = 0;


    }

    public void animateImages() {


            handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {



                    //loop for all images and animate it
                    for (int i = 0; i < images.length; i++) {

                            if (i!=current)
                                    images[i].animate().alpha(1);//show
                            else
                                    images[i].animate().alpha(0);//hide
                    }

                        if (current<images.length)
                            current++;
                        else
                            current=0;//again first one


                        //recurring using the same method - infinite loop
                        animateImages();

                    }
            }, 500); //half a second

    }
};