0

i'm using this How to animate .gif images in an android? solution (first answer of Shobhit Puri) to implements in my app some Gif. now i want to add some Listener on them (click listner). every single procedure i tried didn't worked.

the idea is:

  • the gif is made by 3 image, when you click the first image the gif will change in another one, and so on.

Thanks for your attention.

EDIT: actualy i'm trying this way

    findViewById(R.id.ivAnimation).setOnClickListener(new View.OnClickListener(){
         @Override
    public void onClick(View v){
             Log.i(TAG, "onClickGif PrincipalActivity");
         }
    });

on onCreate() of the mainActivity. the Log is well generated but i can't go over this

Community
  • 1
  • 1
  • `View.setOnClickListener` didn't work? – OneCricketeer Feb 17 '16 at 22:23
  • i forgot to say that findViewById(R.id.ivAnimation).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Log.i(TAG, "onClickGif PrincipalActivity"); } }); work, in the log i can read "onClickgif..." but i cant do nothing than append this in the Log – Filippo Adessi Feb 17 '16 at 22:43
  • You need to save the instance of `findViewById(R.id.ivAnimation)` to an ImageView (Or whatever it is in your XML) in order to do anything with the view object – OneCricketeer Feb 17 '16 at 22:57

1 Answers1

0

I'm not sure about gifs, but if you have separate files, you could do something like this.

Warning: Untested :)

final int[] imgs = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3};
int position = 0;

final ImageView gifView = (ImageView) findViewById(R.id.ivAnimation);
gifView.setImageResource(imgs[position]);

gifView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        Log.i(TAG, "onClickGif PrincipalActivity");
        position = (position + 1) % imgs.length; // wrap around to "restart" gif
        gifView.setImageResource(imgs[position]);
    }
});
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • thanks for help. with this code i can see the gif changing the image as always plus a smaller static image that on click change till the third image. – Filippo Adessi Feb 17 '16 at 23:15
  • I don't know what View you are using to display the gif, but I assume it is constantly drawing it in the background and ignoring the image source. You will probably need to edit the source of your custom Gif image view to allow stepping through "frames" – OneCricketeer Feb 17 '16 at 23:17