0

I need to show an image for 1 second, at the end of that time make it invisible for 1 second, and at the end of the time again to show again, because I walk a list and no longer it displays me the following pictures.

But my code only shows me the image by 1 second, but the following images doesn't show them me.

This my code:

@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

            }
}
Exbaby
  • 75
  • 1
  • 2
  • 10

2 Answers2

1

You can do it by using Handler. Just take a flag isShowing. Below is the rough idea, you can alter it as per your need.

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

You just have to call showHideImage() once at the start, rest will be handled by itself.

Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
0

In this way it worked:

Declarations...

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

public Bits items;

Initialize:

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

Method:

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

thanks for your helps!!

Exbaby
  • 75
  • 1
  • 2
  • 10