[英]How to make gif images to fit WebView on Android
我正在使用webview顯示gif圖像,但是問題是,webview窗口不適合gif的寬度,在gif的右邊總是有一些空白。
現在我必須手動設置寬度並嘗試保持gif的寬度,但是奇怪的是,高度始終適合gif
int w = 115;//image.getWidth();
LayoutParams lp = holder.wv.getLayoutParams();
lp.width= (int) (w * context.getResources().getDisplayMetrics().density);
holder.wv.setLayoutParams(lp);
holder.wv.loadUrl("file:///android_asset/" + text.substring(1, text.length()-1) + ".gif");
holder.wv.setVisibility(View.VISIBLE);
xml布局中的Webview
<WebView
android:id="@+id/wv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2sp"
android:gravity="center_vertical|center_horizontal"
android:visibility="gone"/>
gif沒問題,它們沒有結尾的空格
這是設備上的外觀
這是原始的gif
我認為最好的方法是用HTML包裝gif並使用CSS縮放它:
String gif_url = "file:///android_asset/" + text.substring(1, text.length()-1) + ".gif";
String html = "<html><body><img style=\"width: 100%\" src=\"" + gif_url + "\"></body></html>";
holder.wv.loadData(html);
我不得不手動編輯gif大小以使其適合窗口,這不是一個聰明的方法,但這是唯一的解決方案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.