This article shares the relevant code for Android displaying Gif format images through Movie for your reference. The specific content is as follows
public class CommonGifView extends View { private Resources mResources; private Movie mMovie; private long startTime = 0; private float widthRatio; private float heightRatio; public CommonGifView(Context context) { this(context, null); } public CommonGifView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CommonGifView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mResources = (); TypedArray ta = (attrs, .custom_gif_view); int src_id = (.custom_gif_view_gif_src, -1); setGifViewBg(src_id); (); } /** * Set gif format image background for View * @description: * @author ldm * @date 2016-2-18 9:21:16 AM */ private void setGifViewBg(int src_id) { if (src_id == -1) { return; } // Get the input stream of the corresponding resource file InputStream is = (src_id); mMovie = (is);// Decode the input stream as a Movie object requestLayout(); } /* * This method is for use in Activity */ public void setGifStream(InputStream is) { mMovie = (is); requestLayout(); } @Override protected void onDraw(Canvas canvas) { (canvas); long now = (); if (startTime == 0) { // If the first frame is to record the start time startTime = now; } if (mMovie != null) {// If the return value is not equal to null, it means that this is a GIF image int duration = ();// The duration of the animation is removed if (duration == 0) { duration = 1000; } int currentTime = (int) ((now - startTime) % duration);// Calculate which frame to display (currentTime); // (canvas, getWidth() - (), getHeight() - ()); float scale = (widthRatio, heightRatio); (scale, scale); (canvas, 0, 0); invalidate(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mMovie != null) {// If the return value is not equal to null, it means that this is a GIF image int w = ();//width int h = ();//high if (w <= 0) { w = 1; } if (h <= 0) { h = 1; } int left = getPaddingLeft(); int right = getPaddingRight(); int top = getPaddingTop(); int bottom = getPaddingBottom(); int widthSize, heightSize; w += left + right; h += top + bottom; w = (w, getSuggestedMinimumWidth()); h = (h, getSuggestedMinimumHeight()); widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);//Return the size value you want based on the size and MeasureSpec you provide heightSize = resolveSizeAndState(h, heightMeasureSpec, 0); widthRatio = (float) widthSize / w; heightRatio = (float) heightSize / h; setMeasuredDimension(widthSize, heightSize); } else { (widthMeasureSpec, heightMeasureSpec); } } }
Custom attribute res/values/file:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="custom_gif_view"> <attr name="gif_src" format="reference"></attr> </declare-styleable> </resources>
Use in Activity:
public class MainActivity extends Activity { private CommonGifView view; private InputStream is; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); view = (CommonGifView) findViewById(.gif_test); try { is = getAssets().open(""); (is); } catch (IOException e) { (); } } }
The above is all about this article, I hope it will be helpful to everyone's learning.