显示并删除图像几秒钟

时间:2017-04-21 05:44:20

标签: android postdelayed

我需要显示1秒钟的图像,在该时间结束时使其隐藏1秒钟,并在时间结束时再次显示,因为我走了一个列表而不再显示我以下图片。

但是我的代码只显示了1秒的图像,但下面的图片并没有向我显示。

这是我的代码:

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNextImage() {
    // loads the image at position currentPosition
    final Bits item = L.get(currentPosition);
    imageBit.setImageBitmap(BitmapFactory.decodeFile(item.getbImage()));
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            imageBit.setImageBitmap(BitmapFactory.decodeFile(item.getbImage()));
        }
    },1000);

            handler.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       nameBit.setText(item.getbText());

                       imageBit.setVisibility(View.GONE);

                   }
               },1000);
            currentPosition++; // updates the current position
            if (L.size() > currentPosition) { // more images to show?

                // loads the next image after some delay
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        showNextImage();
                    }
                }, 1000); // in millis, 1000 for one second delay

            }
}

2 个答案:

答案 0 :(得分:1)

您可以使用Handler来完成此操作。只需带一面旗帜isShowing。以下是粗略的想法,您可以根据需要进行更改。

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        showHideImage();
    }
};

private void showHideImage() {
    if(isShowing) {
        isShowing = false;
        showImage();    //Do whatever you want to
    } else {
        isShowing = true;
        hideImage();
    }
    yourImageView.postDelayed(runnable, ONE_SECOND);
}

您只需在开始时拨打showHideImage()一次,其余部分将自行处理。

答案 1 :(得分:0)

通过这种方式,它起作用了:

...声明

private int currentPosition = 0;
public ArrayList<Bitmap> ItemsBitmap;
public Bitmap bitmap;

public Bits items;

初​​始化:

ItemsBitmap = new ArrayList<>();

    for(int i = 0; i < L.size(); i++){
        Bits item = L.get(i);
        ItemsBitmap.add(BitmapFactory.decodeFile(item.getbImage()));

    }
    for (int x = 0; x < L.size(); x++){
        Bits item = L.get(x);
        nameBit.setText(item.getbText());
    }
    showImages();

方法:

 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showImages() {
    new CountDownTimer(2000, 1000) {
        boolean continuar = true; // Si quieres que se detenga, vuelve continuar false en algun momento
        int number = 0;
        public void onTick(long millisUntilFinished) {
            if(number % 2 != 0) {
                if(number == 101)
                    number = -1; // esto hace que la bandera no crezca mucho
                imageBit.setVisibility(View.VISIBLE);
                nameBit.setVisibility(View.GONE);
            }
            else {
                bitmap = ItemsBitmap.get(currentPosition);
                imageBit.setImageBitmap(bitmap);
                items = L.get(currentPosition);
                nameBit.setText(items.getbText());
                currentPosition++;
                if (ItemsBitmap.size() == currentPosition) {
                    currentPosition = 0; // Esto hace que se vuelva a repetir la lista de Bitmaps
                }
                imageBit.setVisibility(View.GONE);
                if (L.size() == currentPosition){
                    currentPosition = 0;
                }
                nameBit.setVisibility(View.VISIBLE);
            }
            number++;
        }
        public void onFinish() {

            if (continuar) {
                this.start();
            }
        }
    }.start();
}

感谢您的帮助!!