如何在 android 中在线使用 gif 图像更改 ImageView 小部件?

How can I change an ImageView widget using a gif image online in android?

我想从网页获取一些图像(.gif 格式),然后在我的应用程序中更改 ImageView 小部件。

我在Whosebug上找到了如下代码:

public static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

然后,在 onPostExecute 方法中,我添加了以下不起作用的行(尽管它没有给出任何可见的错误)。

((ImageView) findViewById(R.id.imgWeather)).setImageDrawable(LoadImageFromWebOperations(pic_image)); //pic_image is the url which ends in .gif

试试这个,

 private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

并将其命名为,

  new DownloadImageTask("//(ImageView) findViewById(R.id.imgWeather)")
                    .execute("//pic_image is the url which ends in .gif");