android中使用imageview显示Gif图片

本文介绍了在Android中如何让ImageView显示GIF动画,提供了两种方法:使用WebView和Movie类。重点讲述了使用Movie类的方法,包括创建GIFView组件、初始化Movie对象、在onDraw方法中绘制GIF以及在XML布局中使用GIFView。
摘要由CSDN通过智能技术生成
               

http://blog.csdn.net/riyuegonghe/article/details/9283479

http://weavora.com/blog/2012/02/07/android-how-to-use-animated-gif/

To date Android platform supports a sufficiently large number of media formats. See documentation here. Among the declared graphic formats you’ll find the one we are interested – GIF. Generally speaking this format does not cause any difficulties until you’ll try to use a GIF file with animation. In this case, the standard class ImageViewdisplays GIF file as a usual static image. Doing search you can find forums stating that Android platform doesn’t fully support GIF, that animated GIF files cannot be used. Nevertheless you can still solve this problem and I’ll show you how.

There are at least two ways:

It is difficult to say which solution is better, however there is one important point. Although WebView is a part of any Android SDK, the ability to play animated GIF files only appeared from version Android 2.2 (API level 8). So if you plan support for vide range of devices, then second option is likely more acceptable for you. I’ll further focus on it.

It is not quite fair to say that class Movie is well documented. Otherwise, this article probably would be useless :-) So, let’s try to create a separate component that can play animated GIF files using class Movie.

OK, creating a new class GIFView, inherited from View:

1234567891011121314151617181920
 
         
package com . wvr . widget ;
import android.content.Context ;
import android.util.AttributeSet ;
import android.view.View ;
 
public class GIFView extends View {
 
    public GIFView ( Context context ) {
        super ( context );
    }
 
    public GIFView ( Context context , AttributeSet attrs ) {
        super ( context , attrs );
    }
 
    public GIFView ( Context context , AttributeSet attrs , int defStyle ) {
        super ( context , attrs , defStyle );
    }
 
}
view raw GIFView.java hosted with ❤ by  GitHub

Now let’s add new variable of type Movie to our class and initialize it

12345678910111213141516171819202122232425262728293031
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值