How to play GIF in android

后端 未结 8 822
死守一世寂寞
死守一世寂寞 2020-11-30 05:08

Hello stackoverflow I\'m trying to develop an android application to play my own GIF, here is the code snippet

MainActivity.java

8条回答
  •  独厮守ぢ
    2020-11-30 05:12

    Source Code https://drive.google.com/open?id=0BzBKpZ4nzNzUZy1BVlZSbExvYUU

    ** android:hardwareAccelerated="false" in Manifest File**

    package com.keshav.gifimageexampleworking;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    public class MainActivity extends AppCompatActivity
    {
        private GifImageView gifImageView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            gifImageView = (GifImageView) findViewById(R.id.GifImageView);
    
            gifImageView.setGifImageResource(R.drawable.success1);
    
        }
    
     @Override
     protected void onResume() 
     {
        super.onResume();
    
        //refresh long-time task in background thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //dummy delay for 2 second
                    Thread.sleep(8000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                //update ui on UI thread
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        gifImageView.setGifImageResource(R.drawable.success);
                    }
                });
    
            }
        }).start();
       }
    }
    
    
    package com.keshav.gifimageexampleworking;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Movie;
    import android.net.Uri;
    import android.os.SystemClock;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;
    
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    
    
    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, 10, 10);
                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");
            }
        }
    }
    

提交回复
热议问题