android中使用imageview显示Gif图片

本文详细介绍如何在Android中使用Movie类实现GIF动画显示,包括自定义GIFView组件,通过XML配置资源,以及在Activity中初始化和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

http://blog.youkuaiyun.com/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

1234567891011121314151617181920212223242526272829303132333435

         
package com . wvr . widget ;
 
import java.io.InputStream ;
import com.wvr.example.R ;
import android.content.Context ;
import android.graphics.Movie ;
import android.util.AttributeSet ;
import android.view.View ;
 
public class GIFView extends View {
   
    private Movie mMovie ;
 
    public GIFView ( Context context ) {
        super ( context );
        initializeView ();
    }
 
    public GIFView ( Context context , AttributeSet attrs ) {
        super ( context , attrs );
        initializeView ();
    }
 
    public GIFView ( Context context , AttributeSet attrs , int defStyle ) {
        super ( context , attrs , defStyle );
        initializeView ();
    }
 
    private void initializeView () {
        //R.drawable.loader - our animated GIF
        InputStream is = getContext (). getResources (). openRawResource ( R . drawable . loader );
        mMovie = Movie . decodeStream ( is );
    }
 
}
view raw GIFView2.java hosted with ❤ by  GitHub

OK, now Movie object is initialized and we just need to draw it. We will use onDraw(Canvas) method for this purpose, but before that, we must let Movie object know what exact part of GIF file it should draw. To do this we need a separate variable movieStart of long type, which we assign SystemClock.uptimeMillis() value. In this case we will know when the movie was started and can calculate how much time has passed.

123456789101112131415

         
@Override
    protected void onDraw ( Canvas canvas ) {
        canvas . drawColor ( Color . TRANSPARENT );
        super . onDraw ( canvas );
        long now = android . os . SystemClock . uptimeMillis ();
        if ( movieStart == 0 ) {
            movieStart = now ;
        }
        if ( movie != null ) {
            int relTime = ( int ) (( now - movieStart ) % movie . duration ());
            movie . setTime ( relTime );
            movie . draw ( canvas , getWidth () - movie . width (), getHeight () - movie . height ());
            this . invalidate ();
        }
    }
view raw onDraw.java hosted with ❤ by  GitHub

All we need now is initialize GIFView in our Activity class

12345678

         
public class AnimatedGIFActivity extends Activity {
    @Override
    public void onCreate ( Bundle savedInstanceState ) {
        super . onCreate ( savedInstanceState );
        GIFView gifView = new GIFView ( this );
        setContentView ( gifView );
    }
}
view raw onCreate.java hosted with ❤ by  GitHub

Now let’s add the possibility to pass GIF id as a parameter

12345678910111213141516171819

         
private int gifId ;
 
    public void setGIFResource ( int resId ) {
        this . gifId = resId ;
        initializeView ();
    }
 
    public int getGIFResource () {
        return this . gifId ;
    }
 
    private void initializeView () {
        if ( gifId != 0 ) {
            InputStream is = getContext (). getResources (). openRawResource ( gifId );
            movie = Movie . decodeStream ( is );
            movieStart = 0 ;
            this . invalidate ();
        }
    }
view raw GIF_Param.java hosted with ❤ by  GitHub

Here is how the Activity class should look:

123456

         
public void onCreate ( Bundle savedInstanceState ) {
        super . onCreate ( savedInstanceState );
        GIFView gifView = new GIFView ( this );
        gifView . setGIFResource ( R . drawable . loader );
        setContentView ( gifView );
    }
view raw onCreate.java hosted with ❤ by  GitHub

Another good practice is using components in XML markup. So, let’s also do this. For this purpose I’ll create fileattrs.xml (res/values/attrs.xml) with the following structure:

12345

         
<resources>
    <declare-styleable name= "GIFView" >
        <attr name= "src" format= "reference" />
    </declare-styleable>
</resources>
view raw attrs.xml hosted with ❤ by  GitHub

Now we just need to read XML-attributes in our GIFView class:

12345678910

         
private void setAttrs ( AttributeSet attrs ) {
        if ( attrs != null ) {
            TypedArray a = getContext (). obtainStyledAttributes ( attrs , R . styleable . GIFView , 0 , 0 );
            String gifSource = a . getString ( R . styleable . GIFView_src );
            //little workaround here. Who knows better approach on how to easily get resource id - please share
            String sourceName = Uri . parse ( gifSource ). getLastPathSegment (). replace ( ".gif" , "" );
            setGIFResource ( getResources (). getIdentifier ( sourceName , "drawable" , getContext (). getPackageName ()));
            a . recycle ();
        }
    }
view raw setAttrs.java hosted with ❤ by  GitHub

After this we are able to use GIFView in XML markup the following way:

main.xml

123456789101112

         
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
    xmlns:components= "http://schemas.android.com/apk/res/com.wvr.example"
    android:layout_width= "fill_parent"
    android:layout_height= "fill_parent"
    android:orientation= "vertical" >
 
    <com.wvr.widget.GIFView
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        components:src= "@drawable/loader" />
</LinearLayout>
view raw main.xml hosted with ❤ by  GitHub

Our Activity class looks the following way:

1234567

         
public class AnimatedGIFActivity extends Activity {
    @Override
    public void onCreate ( Bundle savedInstanceState ) {
        super . onCreate ( savedInstanceState );
        setContentView ( R . layout . main );
    }
}
view raw AnimatedGIFActivity.java hosted with ❤ by  GitHub

That’s it. Have you ever used such approach or maybe you have some better solution – please let us know.


           

给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
这里写图片描述
汽车数据集:2025年3K+记录 真实电汽车数据:特斯拉、宝马、日产车型,含2025年电池规格和销售数据 关于数据集 电汽车数据集 这个合成数据集包含许多品牌和年份的电汽车和插电式车型的记录,捕捉技术规格、性能、定价、制造来源、销售和安全相关属性。每一行代表由vehicle_ID标识的唯一车辆列表。 关键特性 覆盖范围:全球制造商和车型组合,包括纯电汽车和插电式混合力汽车。 范围:电池化学成分、容量、续航里程、充电标准和速度、价格、产地、自主水平、排放、安全等级、销售和保修。 时间跨度:模型跨度多年(包括传统和即将推出的)。 数据质量说明: 某些行可能缺少某些字段(空白)。 几个分类字段包含不同的、特定于供应商的值(例如,Charging_Type、Battery_Type)。 各列中的单位混合在一起;注意kWh、km、hr、USD、g/km和额定值。 列 列类型描述示例 Vehicle_ID整数每个车辆记录的唯一标识符。1 制造商分类汽车品牌或OEM。特斯拉 型号类别特定型号名称/变体。型号Y 与记录关联的年份整数模型。2024 电池_类型分类使用的电池化学/技术。磷酸铁锂 Battery_Capacity_kWh浮充电池标称容量,单位为千瓦时。75.0 Range_km整数表示充满电后的行驶里程(公里)。505 充电类型主要充电接口或功能。CCS、NACS、CHAdeMO、DCFC、V2G、V2H、V2L Charge_Time_hr浮充电的大致时间(小时),上下文因充电方法而异。7.5 价格_USD浮参考车辆价格(美元).85000.00 颜色类别主要外观颜色或饰面。午夜黑 制造国_制造类别车辆制造/组装的国家。美国 Autonomous_Level浮点自化能力级别(例如0-5),可能包括子级别的小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值