Android通过Movie展示Gif格式图片
作者:BetterLaterThanNever 时间:2023-06-16 19:29:38
本文实例为大家分享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();
}
}
}
标签:Android,Movie,Gif格式图片
0
投稿
猜你喜欢
android中RecyclerView自定义分割线实现
2023-08-03 17:37:47
struts2实现文件上传显示进度条效果
2021-11-19 16:16:21
Android使用插件实现代码混淆
2023-02-25 07:13:10
http协议进阶之Transfer-Encoding和HttpCore实现详解
2023-11-03 14:22:05
c# 解决IIS写Excel的权限问题
2021-08-21 03:23:34
unity绘制一条流动的弧线(贝塞尔线)
2022-09-03 18:15:00
c#的异或运算符介绍
2021-08-09 03:17:51
Java中如何调用cmd压缩文件
2023-12-09 13:37:02
Android中AsyncTask与handler用法实例分析
2023-12-04 00:01:52
Java 图片与byte数组互相转换实例
2023-06-24 03:28:39
在Kotlin开发中如何使用集合详解
2022-03-06 02:22:30
Java通俗易懂系列设计模式之装饰模式
2023-08-07 15:41:28
Android 通过TCP协议上传指定目录文件的方法
2023-11-07 23:34:11
MyBatisPlus 自定义sql语句的实现
2021-11-11 07:51:20
SpringBoot2学习之springboot与spring区别分析
2023-02-22 17:44:28
android从资源文件中读取文件流并显示的方法
2022-05-29 14:11:56
springboot如何开启一个监听线程执行任务
2022-01-09 08:44:48
浅谈android @id和@+id的区别
2021-10-28 06:06:09
Spring RestTemplate的使用与踩坑
2022-01-12 21:43:14
java实现ftp文件上传下载功能
2023-04-13 19:31:30