简体   繁体   English

如何使gif图像适合Android上的WebView

[英]How to make gif images to fit WebView on Android

I'm using webview to display gif images, but the problem is, the webview windows doesn't fit the width of gif, there's always some blank space to the right of the gif. 我正在使用webview显示gif图像,但是问题是,webview窗口不适合gif的宽度,在gif的右边总是有一些空白。

Right now I have to manually set the width and try to keep the width of my gifs, but it's strange that the height always fit the gifs 现在我必须手动设置宽度并尝试保持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);

Webview in xml layout 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"/>

There is nothing wrong with the gifs, they don't have the trailing white space gif没问题,它们没有结尾的空格

Here is what it looks like on the device 这是设备上的外观

在此处输入图片说明

here is the original gif 这是原始的gif

在此处输入图片说明

I think the best way is to wrap gif in HTML and scale it using CSS: 我认为最好的方法是用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM