Android shows pictures in Gif format through Movie

  • 2021-07-01 08:09:12
  • OfStack

This article example for everyone to share Android through Movie display Gif format pictures related code, 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 = context.getResources();
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_gif_view);
    int src_id = ta.getResourceId(R.styleable.custom_gif_view_gif_src, -1);
    setGifViewBg(src_id);
    ta.recycle();
  }

  /**
   *  For View Settings gif Format picture background 
   * @description : 
   * @author ldm
   * @date 2016-2-18  Morning 9:21:16
   */
  private void setGifViewBg(int src_id) {
    if (src_id == -1) { return; }
    //  Get the input stream of the corresponding resource file 
    InputStream is = mResources.openRawResource(src_id);
    mMovie = Movie.decodeStream(is);//  Decodes the input stream to Movie Object 
    requestLayout();
  }

  /*
   *  This method provides Activity Use in 
   */
  public void setGifStream(InputStream is) {
    mMovie = Movie.decodeStream(is);
    requestLayout();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    long now = SystemClock.uptimeMillis();
    if (startTime == 0) { //  If the 1 Frame, record start time 
      startTime = now;
    }
    if (mMovie != null) {//  If the return value is not equal to null That means this is 1 A GIF Picture 
      int duration = mMovie.duration();//  Take out the duration of animation 
      if (duration == 0) {
        duration = 1000;
      }
      int currentTime = (int) ((now - startTime) % duration);//  Calculate which frames to display 
      mMovie.setTime(currentTime);
      // mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
      float scale = Math.min(widthRatio, heightRatio);
      canvas.scale(scale, scale);
      mMovie.draw(canvas, 0, 0);
      invalidate();
    }
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mMovie != null) {//  If the return value is not equal to null That means this is 1 A GIF Picture 
      int w = mMovie.width();// Width 
      int h = mMovie.height();// Height 
      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 = Math.max(w, getSuggestedMinimumWidth());
      h = Math.max(h, getSuggestedMinimumHeight());
      widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);// According to the size and size you provide MeasureSpec Returns the size value you want 
      heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);
      widthRatio = (float) widthSize / w;
      heightRatio = (float) heightSize / h;
      setMeasuredDimension(widthSize, heightSize);
    }
    else {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
  }
}

Custom properties res/values/attrs. xml 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>

Used in Activity:


public class MainActivity extends Activity {
  private CommonGifView view;
  private InputStream is;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    view = (CommonGifView) findViewById(R.id.gif_test);
    try {
      is = getAssets().open("test01.gif");
      view.setGifStream(is);
    }
    catch (IOException e) {
      e.printStackTrace();
    }

  }
}

The above is the whole content of this paper, hoping to help everyone's study.


Related articles: