Monday, July 18, 2022

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

Issue

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.

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

        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

   <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

Here is what it looks like on the device

enter image description here

here is the original gif

enter image description here


Solution

I had to manually edit the gif sizes so it fits the window, not a smart way to do but it was the only solution



Answered By - Casper
Answer Checked By - Candace Johnson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.