1分钟就能学会Android如何展示Gif动态图


效果图
在这里插入图片描述

Gif简介

GIF的全称是Graphics Interchange Format,可译为图形交换格式,用于以超文本标志语言(Hypertext Markup Language)方式显示索引彩色图像,在因特网和其他在线服务系统上得到广泛应用。GIF是一种公用的图像文件格式标准,版权归Compu Serve公司所有。 ————百度百科

在如今聊天必有图的情况下,有没有想过我如何在我软件中加上一个动态图呢?

依赖

虽然很多东西Android没有自带,但是很多东西我们加上依赖就能愉快的使用了,比如这个GifImageView

           //GIF
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.15'

XML布局

通过源码可以看到,GifImageView是继承自ImageView的,所以我们会用ImageView,那么用GifImageView也就不难了

这里我们第一个使用 src 属性添加上一个 gif 文件,第二个我们则演示在Java代码中添加

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackGround"
    android:orientation="vertical">

    <pl.droidsonroids.gif.GifImageView
        android:id="@+id/gifImv1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleType="center"
        android:src="@mipmap/elephant" />

    <pl.droidsonroids.gif.GifImageView
        android:id="@+id/gifImv2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleType="center" />

</LinearLayout>

Java

可以看到和ImageView的使用方式是一模一样的

mGifImv2.setImageResource(R.mipmap.xiaohei);

如果你觉得本文对你有帮助那就点个赞吧!
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章