這一篇寫使用webview加載gif動畫,
貼代碼:
xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/buttons"
android:layout_marginTop="20dp"
android:gravity="center">
<WebView
android:id="@+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
這是布局中的某一個控件
----------------------------------------------------------
activity中調用:
mWebView = (WebView) findViewById(R.id.webView1);
String gifFilePath = "file:///android_asset/1.gif";//首先將一張gif格式的動圖放置在assets中
mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
String data = "<HTML><Div align=\"center\" margin=\"0px\"><IMG src=\"" + gifFilePath + "\" margin=\"0px\"/></Div>";//設置圖片位於webview的中間位置
mWebView.loadDataWithBaseURL(gifFilePath, data, "text/html", "utf-8", null);