GIF Image display using WebView

后端 未结 8 799
北荒
北荒 2020-12-08 23:11

Can someone please provide a code to display GIF images in a webview ( I\'m already able to display the same using frame animation of png images) Now I want a way to display

相关标签:
8条回答
  • 2020-12-08 23:55

    Gifs are supported in web view.

    write a html file as following :

    <html>
    <body bgcolor="white">
        <table width="100%" height="100%">
            <tr>
                <td align="center" valign="center">
                    <font color="gray">Some text you display</font>
                    <br/>
                    <br/>
                    <br/>
                    <br/>
                    <br/>
                    <img src="yourGIF.gif">
                </td>
            </tr>
        </table>
    </body>
    

    and store it in "assets" folder of you application also store your gif in the same folder. And do following to show it:

    webview.loadUrl("file:///android_asset/your_html.html");
    
    0 讨论(0)
  • 2020-12-08 23:55

    Create a package named “Utils” under src folder and create a class named “GifImageView”

    public class GifImageView extends View {
        private InputStream mInputStream;
        private Movie mMovie;
        private int mWidth, mHeight;
        private long mStart;
        private Context mContext;
        public GifImageView(Context context) {
            super(context);
            this.mContext = context;
        }
        public GifImageView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
        public GifImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            this.mContext = context;
            if (attrs.getAttributeName(1).equals("background")) {
                int id = Integer.parseInt(attrs.getAttributeValue(1).substring(1));
                setGifImageResource(id);
            }
        }
        private void init() {
            setFocusable(true);
            mMovie = Movie.decodeStream(mInputStream);
            mWidth = mMovie.width();
            mHeight = mMovie.height();
            requestLayout();
        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(mWidth, mHeight);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            long now = SystemClock.uptimeMillis();
            if (mStart == 0) {
                mStart = now;
            }
            if (mMovie != null) {
                int duration = mMovie.duration();
                if (duration == 0) {
                    duration = 1000;
                }
                int relTime = (int) ((now - mStart) % duration);
                mMovie.setTime(relTime);
                mMovie.draw(canvas, 0, 0);
                invalidate();
            }
        }
        public void setGifImageResource(int id) {
            mInputStream = mContext.getResources().openRawResource(id);
            init();
        }
        public void setGifImageUri(Uri uri) {
            try {
                mInputStream = mContext.getContentResolver().openInputStream(uri);
                init();
            } catch (FileNotFoundException e) {
                Log.e("GIfImageView", "File not found");
            }
        }
    }
    

    Now define GifImageView in MainActivity File: src/activity/MainActivity.class

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            GifImageView gifImageView = (GifImageView) findViewById(R.id.GifImageView);
            gifImageView.setGifImageResource(R.drawable.smartphone_drib);
        }
    }
    

    Now UI Part File: res/layout/activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:gravity="center"
        android:background="#111E39"
        tools:context=".Activity.MainActivity">
        <com.android.animatedgif.Utils.GifImageView
            android:id="@+id/GifImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    
    </RelativeLayout>
    

    Resource : How to display gif imageview in android

    0 讨论(0)
提交回复
热议问题