Android 显示gif动画
2016-06-01 21:57
295 查看
我创建了一个自定义ImageView,该视图既可以加载Gif也可以加载普通图片(如jpg,png,bmp)
百度搜索 ”在Android中显示GIF动画“,第一条便是以下链接
http://blog.csdn.net/loongggdroid/article/details/21166563
该作者写的GifView继承自普通的View,比较复杂。我个人认为在实际的工作中用我的这个GifView就足够了。
码数太多,不过虽然不太实用,但那位作者代码中透入的知识点也是比较全面的,适合初学者们多加琢磨和吸收。
package com.chy.view; import java.io.File; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Movie; import android.os.SystemClock; import android.text.TextUtils; import android.util.AttributeSet; import android.widget.ImageView; /** * gif视图 */ public class PowerImageView extends ImageView { private long timeStart = 0; private boolean isStopGif = false; private Movie movie = null; public PowerImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public PowerImageView(Context context, AttributeSet attrs) { super(context, attrs); } public PowerImageView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { if (movie == null || isStopGif || movie.duration() == 0) { super.onDraw(canvas); } else { long timeNow = SystemClock.uptimeMillis(); if (timeStart == 0) { timeStart = timeNow; } if ((timeNow - timeStart) >= movie.duration()) { timeStart = 0; } int timeRel = (int) ((timeNow - timeStart) % movie.duration()); movie.setTime(timeRel); float wScale = movie.width()/(float)getWidth(); float hScale = movie.height()/(float)getHeight(); canvas.scale(wScale, hScale); movie.draw(canvas, 0, 0); invalidate(); } } @Override public void setImageResource(int resId) { movie = null; super.setImageResource(resId); } public void setImage(String path) { movie = null; if(TextUtils.isEmpty(path)) { setImageBitmap(null); } else { setImageBitmap(BitmapFactory.decodeFile(path)); } } public void setGif(String path) { setImageBitmap(null); if (TextUtils.isEmpty(path)) { return; } File file = new File(path); if (!file.exists()) { return; } //movie只能解析gif图 movie = Movie.decodeFile(path); if (movie == null) { setImageBitmap(BitmapFactory.decodeFile(path)); } } public boolean isStopGif() { return isStopGif; } public void setStopGif(boolean isStopGif) { this.isStopGif = isStopGif; } }
百度搜索 ”在Android中显示GIF动画“,第一条便是以下链接
http://blog.csdn.net/loongggdroid/article/details/21166563
该作者写的GifView继承自普通的View,比较复杂。我个人认为在实际的工作中用我的这个GifView就足够了。
码数太多,不过虽然不太实用,但那位作者代码中透入的知识点也是比较全面的,适合初学者们多加琢磨和吸收。
相关文章推荐
- android隐藏信号栏
- Android Service实现闹钟
- Android MVP模式简介
- 疑惑:自定义控件中 TypedArray对象进行为什么要进行recycle()
- Android_Notes
- Android中的各种传感器
- Caused by: android.os.TransactionTooLargeException总结
- Caused by: android.os.TransactionTooLargeException总结
- Caused by: android.os.TransactionTooLargeException总结
- Android中ListView的工作原理
- 最全面的 Android 编码规范指南
- android4.2 WifiDisplay远程显示修改为保存文件
- 使用Android Annotations开发
- AndroidStudio 使用AIDL
- android 权限一览表
- Android 最火的快速开发框架XUtils
- Android依赖注入Dagger的使用和源码解析(上篇)
- Android5.1中surface和CpuConsumer下生产者和消费者间的处理框架简述
- AndroidManifest配置之uses-feature
- 怎样用好Fragment,避免掉坑(一)