由于做的項目,要有個動畫的等待效果,第一時間想到的就是Gif(懶,省事),但是試了好多據說能播放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"><WebViewandroid:id="@+id/webview"android:layout_width="match_parent"android:layout_height="match_parent"></WebView></RelativeLayout>
3、代碼中使用
@Overrideprotected 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);}好了,現在就可以播放了,感覺能夠適配任何機型。
以上所述是小編給大家介紹的Android中播放Gif動畫取巧的辦法,希望對大家有所幫助!
新聞熱點
疑難解答