Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 顯示 GIF 動圖的實作方法

編輯:關於 Android 程式設計

Android 原生並不支援 GIF 格式的圖片。由於我希望程式剛啟動時能顯示一個小人跑步的動畫,來表示「正在載入」,而這個跑步動畫正好是一張 GIF 圖,也就是想在程式啟動的瞬間就載入這張 GIF。

我在網上找了一些方法,但不確定是因為我用的 Android 版本較高(Android 4.4)還是其他因素,就是顯示不出來。最後想出了一個辦法成功載入——把 GIF 放進 WebView 裡讓它顯示。

下面是相關程式碼。

activity_prepare_fullscreen.xml 檔:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F3F3F3" > <WebView android:id="@+id/runWebView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <TextView android:id="@+id/fullscreen_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/runWebView" android:layout_centerHorizontal="true" android:gravity="center" android:keepScreenOn="true" android:text="正在加載請等待..." android:textColor="#33b5e5" android:textSize="30sp" android:text /> </RelativeLayout> 

PrepareFullscreenActivity.java 檔,這裡只列出主要的兩句:

private WebView runWebView=null;

runWebView = (WebView) findViewById(R.id.runWebView);

下一句就是把檔案放進去,檔案放在 assets 目錄下:

runWebView.loadDataWithBaseURL(null,"<HTML><body bgcolor='#f3f3f3'><div align=center><IMG src='file:///android_asset/run.gif'/></div></body></html>", "text/html", "UTF-8",null);

希望透過本文能幫大家解決這類問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved