Show Image 1 second and then Remove it 1 second and then return it to show setVisibility

4

I need to show an image for 1 second since I spend 1 second to hide it for 1 second, and then show the next image in the list for 1 second and then hide it for 1 second until I scan all the images in the list.

My problem is that my code only shows me the first image and the next one does not show them, it leaves them invisible and goes through the whole list, but only shows the first one.

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


                   }
               },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() {
                        imageBit.setVisibility(View.VISIBLE);
                        showNextImage();
                    }
                }, 1000); // in millis, 1000 for one second delay

            }
}
    
asked by Exbaby 21.04.2017 в 08:26
source

3 answers

1

First try to create a list of Bitmaps called ItemsBitmap , declare currentPosition global and create a variable to store the bitmap global too:

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

Now initialize the list with the bitmaps before calling the showImages method:

ItemsBitmap = new ArrayList<>();

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

Here's the method:

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showImages() {
        bitmap = ItemsBitmap.get(currentPosition);
        imageBit.setImageBitmap(bitmap);

        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);
                }
                else {
                    bitmap = ItemsBitmap.get(currentPosition);
                    imageBit.setImageBitmap(bitmap);
                    currentPosition++;
                    if (ItemsBitmap.size() == currentPosition) {
                        currentPosition = 0; // Esto hace que se vuelva a repetir la lista de Bitmaps
                    }
                    imageBit.setVisibility(View.GONE);
                }
                number++;
            }
            public void onFinish() {

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

I tried it and it works perfect, let me know if it works for you.

    
answered by 21.04.2017 / 09:22
source
1

Because you do not simply create two methods that make it invisible and another that makes it visible, that is, when the activity starts, the image will appear for a second and this will be controlled by a Handler () and after this time the Handler () will launch the invisible method that will also include a Handler () inside that will determine when to activate the visible method. And to move from one image to another I offer you a method that stores the routes of the files of a directory in a List and can access them by its position increasing the value 0 that is the first to the last value Something simple I think. Method:

private List <String> getSD()
{
 List <String> it = new ArrayList <String>();
String files1;
 File f = new File ("ruta");
 File[] files = f.listFiles ();

 for (int i = 0; i <files.length; i++)
 {
    File  file = files[i];


    Log.d("Count",file.getPath());
    it.add (file.getAbsolutePath());
 }
 return it;
}

This returns the List and you get it and use it like this:

   int valor=0;     
private void cambio(){
List <String> ImageList;

ImageList=getSD();
if(valor==ImageList.size()){
//no hay mas
}else{
valor++;
String ruta=ImageList.get(valor).toString();
imageBit.setImageBitmap(BitmapFactory.decodeFile(new File(ruta));
//Usted agregue el resto.
}
}

I hope it helps.

    
answered by 21.04.2017 в 19:26
1

This way it is working:

Thanks for your help:

  

Andrespengineer

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();
}
    
answered by 21.04.2017 в 20:29