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);

如果你覺得本文對你有幫助那就點個贊吧!
在這裏插入圖片描述

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