【问题标题】:How to make gif images to fit WebView on Android如何使 gif 图像适合 Android 上的 WebView
【发布时间】:2015-12-16 03:13:01
【问题描述】:

我用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 没有任何问题,它们没有尾随空格

这是设备上的样子

这是原图

【问题讨论】:

    标签: android image webview gif


    【解决方案1】:

    我认为最好的方法是将 gif 包装在 HTML 中并使用 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);
    

    【讨论】:

      【解决方案2】:

      我必须手动编辑 gif 大小以使其适合窗口,这不是一个聪明的方法,但这是唯一的解决方案

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-19
        • 1970-01-01
        • 2012-11-10
        • 2011-06-17
        • 1970-01-01
        相关资源
        最近更新 更多