当前位置: 移动技术网 > 移动技术>移动开发>Android > Android通过Movie展示Gif格式图片

Android通过Movie展示Gif格式图片

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享android通过movie展示gif格式图片的相关代码,供大家参考,具体内容如下

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();
  }

  /**
   * 为view设置gif格式图片背景
   * @description:
   * @author ldm
   * @date 2016-2-18 上午9:21:16
   */
  private void setgifviewbg(int src_id) {
    if (src_id == -1) { return; }
    // 获取对应资源文件的输入流
    inputstream is = mresources.openrawresource(src_id);
    mmovie = movie.decodestream(is);// 解码输入流为movie对象
    requestlayout();
  }

  /*
   * 这个方法供activity中使用
   */
  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) { // 如果第一帧,记录起始时间
      starttime = now;
    }
    if (mmovie != null) {// 如果返回值不等于null,就说明这是一个gif图片
      int duration = mmovie.duration();// 取出动画的时长
      if (duration == 0) {
        duration = 1000;
      }
      int currenttime = (int) ((now - starttime) % duration);// 算出需要显示第几帧
      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) {// 如果返回值不等于null,就说明这是一个gif图片
      int w = mmovie.width();//宽度
      int h = mmovie.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);//根据你提供的大小和measurespec,返回你想要的大小值
      heightsize = resolvesizeandstate(h, heightmeasurespec, 0);
      widthratio = (float) widthsize / w;
      heightratio = (float) heightsize / h;
      setmeasureddimension(widthsize, heightsize);
    }
    else {
      super.onmeasure(widthmeasurespec, heightmeasurespec);
    }
  }
}

自定义属性res/values/attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <declare-styleable name="custom_gif_view">
    <attr name="gif_src" format="reference"></attr>
  </declare-styleable>

</resources>

在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();
    }

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网