Android中播放Gif動畫取巧的辦法
由于做的項目,要有個動畫的等待效果,第一時間想到的就是Gif(懶,省事),但是試了好多據(jù)說能播放Gif的控件,也寫過,但是放到魅族手機上就是不能播放,所有就想了個招,既然Gif能在瀏覽器上播放,那android 的 WebView 也能播放,寫了個Demo,果然能播放。
1、將gif的文件放到android的資源文件夾里面
2、寫個html,將android的gif源放到WebView里面去加載
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> </RelativeLayout>
3、代碼中使用
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView) findViewById(R.id.webview); webView.loadDataWithBaseURL(null, "<HTML><body bgcolor='#f3f3f3'><div align=center><IMG src='file:///android_asset/load_wait_1_gif.gif'/></div></body></html>", "text/html", "UTF-8",null); }
好了,現(xiàn)在就可以播放了,感覺能夠適配任何機型。
以上所述是小編給大家介紹的Android中播放Gif動畫取巧的辦法,希望對大家有所幫助!
相關(guān)文章
Android編程應(yīng)用風(fēng)格和主題詳解
這篇文章主要介紹了Android編程應(yīng)用風(fēng)格和主題,較為詳細(xì)的分析了Android應(yīng)用風(fēng)格和主題的概念、功能、使用方法與注意事項,需要的朋友可以參考下2016-10-10詳解Android Studio正式簽名進(jìn)行調(diào)試的實現(xiàn)步驟
這篇文章主要介紹了詳解Android Studio正式簽名進(jìn)行調(diào)試的實現(xiàn)步驟的相關(guān)資料,需要的朋友可以參考下2017-07-07Android內(nèi)嵌Unity并實現(xiàn)互相跳轉(zhuǎn)的實例代碼
這篇文章主要介紹了Android內(nèi)嵌Unity并實現(xiàn)互相跳轉(zhuǎn)的實例代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11