How to use gif in Android?

I’m a beginner in the world of Android. Today I decide to use gif in my app using ImageView. So, I code as usual & image does show but good lord there is no movement! Then I search over the google & the easiest way are given below :

Advertisements
  1. Go to build.gradle (Module: app) & write
repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

 2. sync the gradle file

 3. write on behalf of any button or activity

ImageView imageView = findViewById(R.id.imageView);

/* load file from internet */
Glide.with(this)
        .load("https://media.giphy.com/media/98uBZTzlXMhkk/giphy.gif")
        .into(imageView);

/* load file from drawable folder */
Glide.with(this)
        .load(R.drawable.giffilename)
        .into(imageView);

Example:

boolean fan1state=false;
public void fan1(View v)
    {
        if(fan1state) {
            ImageView imgFp= (ImageView) findViewById(R.id.fan1ico);
            imgFp.setImageResource(R.drawable.fanoff);
        }
        else {
            ImageView imgFp= (ImageView) findViewById(R.id.fan1ico);

            Glide.with(this)
                    .load(R.drawable.fanon)
                    .into(imgFp);
        }
        // Invert the state of the boolean. (This will enter the other case next time.)
        fan1state = !fan1state;
    }

Share for your friends it may help!

Leave a Reply

Your email address will not be published. Required fields are marked *