Android通过Movie展示Gif格式图片

 更新时间:2016年04月06日 14:06:34   作者:BetterLaterThanNever  
这篇文章主要介绍了Android通过Movie展示Gif格式图片的相关资料,需要的朋友可以参考下

本文实例为大家分享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();
    }

  }
}

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

相关文章

  • Flutter Reusable Lottie Animations技巧

    Flutter Reusable Lottie Animations技巧

    这篇文章主要为大家介绍了Flutter Reusable Lottie Animations技巧,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • 代码分析Android消息机制

    代码分析Android消息机制

    本文通过代码实例详细分析了Android消息机制的相关知识点,对此有需要的朋友可以参考学习下。
    2018-03-03
  • Android实现横向滑动卡片效果

    Android实现横向滑动卡片效果

    这篇文章主要为大家详细介绍了Android实现横向滑动卡片效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android实现退出时关闭所有Activity的方法

    Android实现退出时关闭所有Activity的方法

    这篇文章主要介绍了Android实现退出时关闭所有Activity的方法,主要通过自定义类CloseActivityClass实现这一功能,需要的朋友可以参考下
    2014-09-09
  • 使用RecyclerView实现水平列表

    使用RecyclerView实现水平列表

    这篇文章主要为大家详细介绍了使用RecyclerView实现水平列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • Android实现阿里云oss上传流程解析

    Android实现阿里云oss上传流程解析

    这篇文章主要介绍了Android实现阿里云oss上传流程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Android项目实战之Glide 高斯模糊效果的实例代码

    Android项目实战之Glide 高斯模糊效果的实例代码

    这篇文章主要介绍了Android项目实战之Glide 高斯模糊效果的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06
  • Android与Vue的交互的方法示例

    Android与Vue的交互的方法示例

    这篇文章主要介绍了Android与Vue的交互的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Android与H5交互产生Script Error踩坑解决

    Android与H5交互产生Script Error踩坑解决

    这篇文章主要为大家介绍了Android与H5交互产生Script Error问题解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Android仿今日头条顶部导航栏效果的实例代码

    Android仿今日头条顶部导航栏效果的实例代码

    这篇文章主要介绍了Android之仿今日头条顶部导航栏效果的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
    2018-05-05

最新评论