0

I'm trying to display an animated GIF picture in Widget, and I have URL for the picture. I know I can use WebView in activity with webView.loadUrl("http://my_url_here.gif"), but WebView is not supported in Android Widget.

I've already had URL for the picture, and I'm finding some ways not using third party libraries.

Does anyone know how to display it in Widget?

Huaying
  • 151
  • 2
  • 11
  • Before anybody else posts another unworkable answer, please note that the OP is talking about an App Widget, which uses [`RemoteViews`](http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout). – Mike M. May 09 '16 at 09:38

4 Answers4

2

Where is no way to show GIF animation on RemoteViews. You can use only widgets that describes in official documentation

HeyAlex
  • 1,666
  • 1
  • 13
  • 31
2

A bit late to the party, but I will post this for future references who want to get a widget animated.

I think this is probably what you want to achieve: https://media.giphy.com/media/Rb7FAFdrgHr51V6zjy/giphy.gif

Fundamentally, GIF files "allow images or frames to be combined, creating basic animations", any GIF is a collection of images with continuous updates/refreshes to achieve the animated effect.

Hence, even though widgets on Android are RemoteViews that only support basic Text and Image, but if you can downsample a GIF into a series of PNGs or Bitmaps, you can achieve the animation effect by creating an async background task that updates the image bitmap or src of the Widget's RemoteView.

There are great answers on SO for converting Gifs into a collection of PNGs:

public class GifPlayer extends AsyncTask<String, Void, List<Bitmap>> {

    @Override
    protected List<Bitmap> doInBackground(String... params) {
        try {
            // load Gif images
            InputStream in = new java.net.URL(url).openStream();
            //... use Glide to convert Gifs into PNGs and save it in your local file system.
            List<Bitmap> gifs = ...
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<Bitmap> pngArray) {
        while(true) {
            for(Bitmap png : pngArray){
               views.setImageViewBitmap(R.id.new_widget_image, png);
               WidgetManager.updateAppWidget(WidgetID, views);
               Thread.sleep(500); // higher lower this number depends on how fast you want to animate
            }
        }

Not to mention, Android Widgets have a small memory limit, so the above code works, but it will run out of memory for larger GIFs.

In general, RemoteViews.setImageViewBitmap(viewId, bitmap) should only be used for Widgets that don't update frequently. For frequent updates of Widget such as animating a GIF, always use setImageViewResource(viewId, resId) instead (save frames of a GIF as PNG files identified by consecutive IDs such as gif_1.png, gif_2.png, gif_3.png...)

and replace the above code by:

//...
@Override
    protected void onPostExecute(List<Integer> pngArray) {
        while(true) {
            for(int id : pngArray){
               views.setImageViewResource(R.id.new_widget_image, id);
               WidgetManager.updateAppWidget(WidgetID, views);
               Thread.sleep(500); // higher lower this number depends on how fast you want to animate
            }
        }
gazcn007
  • 141
  • 2
  • 5
-1

You can use a load of different image loading libraries for this, but I recommend Fresco. They have great built-in GIF support detailed on this page which looks like the following in code:

Uri uri = Uri.parse("http://my_url_here.gif");
DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setUri(uri)
    .setAutoPlayAnimations(true)
    . // other setters
    .build();
mSimpleDraweeView.setController(controller);

update

I never really understand where this need to not use libraries stems from, but you could just manually copy across all the code classes to your app then! The code in these libraries is battle tested and will work much more reliably than most code you decide to write alone.

That being said, there's a useful gist I've used in the past that may be what you're looking for.

roarster
  • 4,058
  • 2
  • 25
  • 38
  • Thanks, but I want to know if there are ways without using third party library – Huaying May 09 '16 at 09:24
  • @HuayingHuang I've updated the answer with a gist I've used in the past - could you explain why you can't use libraries though? I'm curious. – roarster May 09 '16 at 09:36
-1

You can use the gifView library :https://github.com/koral--/android-gif-drawable

use this is your .xml:-

<pl.droidsonroids.gif.GifImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/src_anim"
    />

where src_anim is your loader gif file