Display Animated GIF

后端 未结 30 2030
无人及你
无人及你 2020-11-22 02:11

I want to display animated GIF images in my aplication. As I found out the hard way Android doesn\'t support animated GIF natively.

However it can display animations

相关标签:
30条回答
  • 2020-11-22 02:52

    Ways to show animated GIF on Android:

    • Movie class. As mentioned above, it's fairly buggy.
    • WebView. It's very simple to use and usually works. But sometimes it starts to misbehave, and it's always on some obscure devices you don't have. Plus, you can’t use multiple instances in any kind of list views, because it does things to your memory. Still, you might consider it as a primary approach.
    • Custom code to decode gifs into bitmaps and show them as Drawable or ImageView. I'll mention two libraries:

    https://github.com/koral--/android-gif-drawable - decoder is implemented in C, so it's very efficient.

    https://code.google.com/p/giffiledecoder - decoder is implemented in Java, so it's easier to work with. Still reasonably efficient, even with large files.

    You'll also find many libraries based on GifDecoder class. That's also a Java-based decoder, but it works by loading the entire file into memory, so it's only applicable to small files.

    0 讨论(0)
  • 2020-11-22 02:52

    There are two options to load animated gifs into our Android apps

    1)Using Glide to load the gif into an ImageView.

        String urlGif = "https://cdn.dribbble.com/users/263558/screenshots/1337078/dvsd.gif";
        //add Glide implementation into the build.gradle file.
        ImageView imageView = (ImageView)findViewById(R.id.imageView);
        Uri uri = Uri.parse(urlGif);
        Glide.with(getApplicationContext()).load(uri).into(imageView);
    

    2) Using an html to load the gif into a WebView

    Create the html with the address to the .gif file:

    <html style="margin: 0;">
    <body style="margin: 0;">
    <img src="https://..../myimage.gif" style="width: 100%; height: 100%" />
    </body>
    </html>
    

    store this file into the assets directory:

    The load this html into the WebView of your application:

        WebView webView =  (WebView)findViewById(R.id.webView);
        webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("file:///android_asset/html/webpage_gif.html");
    

    Heres is a complete example of this two options.

    0 讨论(0)
  • 2020-11-22 02:54

    Glide 4.6

    1. To Load gif

    GlideApp.with(context)
                .load(R.raw.gif) // or url
                .into(imageview);
    

    2. To get the file object

    GlideApp.with(context)
                    .asGif()
                    .load(R.raw.gif) //or url
                    .into(new SimpleTarget<GifDrawable>() {
                        @Override
                        public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) {
    
                            resource.start();
                          //resource.setLoopCount(1);
                            imageView.setImageDrawable(resource);
                        }
                    });
    
    0 讨论(0)
  • 2020-11-22 02:56

    i found a very easy way, with a nice and simple working example here

    display animated widget

    Before getting it working there are some chages to do do in the code

    IN THE FOLLOWING

        @Override
        public void onCreate(Bundle savedInstanceState){    
            super.onCreate(savedInstanceStated);   
            setContentView(new MYGIFView());
        }    
    }
    

    just replace

    setContentView(new MYGIFView());
    

    in

    setContentView(new MYGIFView(this));
    

    AND IN

    public GIFView(Context context) {
        super(context);
    

    Provide your own gif animation file

        is = context.getResources().openRawResource(R.drawable.earth);
        movie = Movie.decodeStream(is);
    }
    

    REPLACE THE FIRST LINE IN

    public MYGIFView(Context context) {
    

    according to the name of the class...

    after done this little changes it should work as for me...

    hope this help

    0 讨论(0)
  • 2020-11-22 02:56

    Easiest way - Can be consider the below code

    We can take advantage of Imageview setImageResource , refer below code for the same.

    The below code can be used to show the image like gif incase if you have the multiple split image of gif. Just split the gif into individual png from a online tool and put image in the drawable like the below order

    image_1.png, image_2.png, etc.

    Have the handler to change the image dynamically.

    int imagePosition = 1;
        Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                public void run() {
                    updateImage();
                }
            };
    
    
    
    
        public void updateImage() {
    
                    appInstance.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", appInstance.getPackageName());
                            gifImageViewDummy.setImageResource(resId);
                            imagePosition++;
        //Consider you have 30 image for the anim
                            if (imagePosition == 30) {
    //this make animation play only once
                                handler.removeCallbacks(runnable);
    
                            } else {
        //You can define your own time based on the animation
                                handler.postDelayed(runnable, 50);
                            }
    
    //to make animation to continue use below code and remove above if else
    // if (imagePosition == 30)
    //imagePosition = 1;
    // handler.postDelayed(runnable, 50);
    // 
                        }
                    });
                  }
    
    0 讨论(0)
  • 2020-11-22 02:57

    Use fresco. Here's how to do it:

    http://frescolib.org/docs/animations.html

    Here's the repo with the sample:

    https://github.com/facebook/fresco/tree/master/samples/animation

    Beware fresco does not support wrap content!

    0 讨论(0)
提交回复
热议问题