如何在android中的ImageView背景中加载GIF

时间:2016-12-22 07:20:09

标签: android

这是我从URL加载gif的代码。我正在使用Glide Library。此代码无效,空白活动显示

    httpClient.addNetworkInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {

            Request originalRequest = chain.request();

            // Add authorization header with updated authorization value to intercepted request
            Request authorisedRequest = originalRequest.newBuilder()
                    .header("Authorization", AccountAuthenticator.getAccessTokenWithTokenType(mContext))
                    .build();
            return chain.proceed(authorisedRequest);
        }
    });

6 个答案:

答案 0 :(得分:2)

尝试使用此

 Glide.with(context)
.load(imageUrl)
.asGif()
.placeholder(R.drawable.loading2)
.crossFade()
.into(imageView);

答案 1 :(得分:1)

Android不支持GIF。作为解决方法,Android提供动画列表/ AnimationDrawable。您需要将GIF转换为单独的帧[.png文件]。您可以使用GIMP进行转换

原始GIF图片

这个GIF可以分解为其框架:

-Background 100ms
- 框架2 100ms
- 框架3 100ms



所以在

GIMP中的GIF帧

将它们保存为frame01.png,frame02.png等,然后创建动画列表XML文件,例如progress_animation.xml

<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/frame01" android:duration="50" />
<item android:drawable="@drawable/frame02" android:duration="50" />
<item android:drawable="@drawable/frame03" android:duration="50" />
....
<item android:drawable="@drawable/frameN" android:duration="50" />

要开始动画,您需要将图像转换为AnimationDrawable,并在其上调用start()

AnimationDrawable progressAnimation = (AnimationDrawable) yourImageView.getBackground();
progressAnimation.start();

答案 2 :(得分:1)

答案 3 :(得分:0)

您可以使用WebView加载GIF图像。实施非常简单,就像一个魅力。创建html页面。这是代码:

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

创建资产文件夹并在资产中创建html文件夹。在html文件夹中复制Gif图像和网页。在xml中添加WebView。

MainActivity

webView = (WebView)findViewById(R.id.webView);
webView.setBackgroundColor(Color.TRANSPARENT); //for gif without background
webView.loadUrl("file:///android_asset/html/HTML_PAGE_NAME.html");

答案 4 :(得分:0)

您也可以使用Picasso

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
            <section id="wrap1">
                <div class="section-text1">
                    <p>Lorem ipsum"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit</p>
                </div>
            </section>
            <section id="wrap2">
                <div class="section-text2">
                    <p>Lorem ipsum"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit</p>
                </div>
            </section>
</div>

答案 5 :(得分:0)

Android现在直接支持从P OS gif,并且易于加载。
Java代码(从文件加载):

ImageDecoder.Source source = ImageDecoder.createSource(new File(fileName));
AnimatedImageDrawable drawable = (AnimatedImageDrawable) ImageDecoder.decodeDrawable(source);
imageView.setImageDrawable(drawable);
drawable.start();

科特林代码(从资产加载):

val source = ImageDecoder.createSource(assets, assetFileName)
val drawable = ImageDecoder.decodeDrawable(source)

imageView.setImageDrawable(drawable)
if (drawable is AnimatedImageDrawable) {
  drawable.start()
}