Search code examples
androidbackgroundviewflipper

Name of current View from ViewFlipper


I have a ViewFlipper with many images. I add images to ViewFlipper like this :

bitmap = BitmapFactory.decodeFile(files[i].getPath());
img = new ImageView(this);
img.setImageBitmap(bitmap);
img.setScaleType(ImageView.ScaleType.FIT_XY);
vf.addView(img, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
img = null;

into a for loop. When animation starts I want to know what image (image's name) will be displayed because for some images I must stopflipping the ViewFlipper. I tried this :

animFlipInNext.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                System.out.println("AnimStart- LeftIn" + " Will be displayed "
                        + vf.getDisplayedChild());
                final File[] imageName = images.listFiles(); //images that I want for viewflipper to stop.
                for (int i = 0; i < images.length(); i++) {
                    bitmap = BitmapFactory.decodeFile(imageName[i].getPath());
                    Drawable background = new BitmapDrawable(bitmap);
                    if (vf.getCurrentView().getBackground().equals(background)) {
                        System.out.println("begin  "
                                + files[i].getName());
                    } else
                        System.out.println("NUNUNU");

                }
            }

but nothing happens. I don't get any message. Where is my mistake? Or, how to do this?


Solution

  • A couple of issues I've pick out:

    A view flipper will automatically cycle through your View's at a set interval. I'm not sure if your listener will fire on every "flip" or it will only fire when you first call startFlipping(). You can test whether it's called every flip, or only when the flipping begins by entering a Log.v() as the first line and checking your output.

    You also have a logical error here which is tripping you up:

    vf.getCurrentView().getBackground().equals(background)
    

    In this line of code your not checking if the background object has the same source, your asking if it is exactly the same object. Which you know it isn't because you only just created the new background object in the line before.

    However, you should be able to achieve what you want like this. All Views have a "One size fits all" attribute which you can use for any reason you want. Consider this code:

    bitmap = BitmapFactory.decodeFile(files[i].getPath());
    img = new ImageView(this);
    img.setImageBitmap(bitmap);
    *img.setTag(files[i].getPath())*
    img.setScaleType(ImageView.ScaleType.FIT_XY);
    vf.addView(img, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
    img = null;
    

    You can now call:

    if (vf.getCurrentView().getTag().toString().equalsIgnoreCase(mPathImLookingFor)) { 
        // Do something
    }