Put Your GIF image in /raw folder and put the below code in your project
public class CustomGifView extends ImageView {
    Movie movie;
    InputStream inputStream;
    private long mMovieStart;
    public CustomGifView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public CustomGifView(Context context) {
        super(context);
    }
    public CustomGifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
        inputStream = context.getResources()
                .openRawResource(R.raw.yourgifimage);
        byte[] array = streamToBytes(inputStream);
        movie = Movie.decodeByteArray(array, 0, array.length);
    }
    private byte[] streamToBytes(InputStream is) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                1024);
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = is.read(buffer)) >= 0) {
                byteArrayOutputStream.write(buffer, 0, len);
                return byteArrayOutputStream.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        long now = SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }
        if (movie != null) {
            int dur = movie.duration();
            if (dur == 0) {
                dur = 3000;
            }
            int relTime = (int) ((now - mMovieStart) % dur);
            movie.setTime(relTime);
            movie.draw(canvas, getWidth() - 200, getHeight() - 200);
            invalidate();
        }
    }
}
Now, in your xml code you can use this CustomGifView like this:
        <CustomGifView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
You have succesfully placed GIF image in your app! 
                       
                    
0 Comment(s)