Android显示GIF图片

摘要:
今天,当我们研究如何在Android手机上显示GIF动态图片时,我们首先需要在src文件夹下创建一个自定义视图。

今天我们研究一下怎样在Android手机上显示GIF动态图片
首先须要在src文件夹下新建一个自己定义的View。代码例如以下:

</pre><pre name="code" class="java">
</pre><pre name="code" class="java">

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

public class MyGifView extends View {
	
	//表示開始播放gif图片的绝对时间
	private long movieStart = 0;
	//movie对象管理gif图片里面的多个帧
	private Movie movie;

	public MyGifView(Context context, AttributeSet attrs) {
		super(context, attrs);
		movie = Movie.decodeStream(context.getResources().openRawResource(
				R.drawable.horse));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		long currentTime = System.currentTimeMillis();
		// 第一次播放
		if (movieStart == 0) {
			movieStart = currentTime;
		}
		
		//循环播放
		if (movie != null) {
			int duration = movie.duration();
			int relTime = (int) ((currentTime - movieStart) % duration);
			movie.setTime(relTime);
			movie.draw(canvas, 0, 0);
			// 强制重绘
			invalidate();
		}
		
		//假设仅仅想播放一次,仅仅需推断currentTime-movieStart的值大于duration就不重绘就可以

		super.onDraw(canvas);
	}
}

接着写一个Activity,用来显示gif图片:

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

XML布局文件是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.example.gifdemo.MyGifView
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp" />

</RelativeLayout>

效果图例如以下:

Android显示GIF图片第1张


整个演示样例project文件下载链接:

Android显示GIF图片


免责声明:文章转载自《Android显示GIF图片》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇shell正则表达式Vue可伸缩工具栏下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

RecyclerView下拉刷新和上拉加载更多实现

RecyclerView下拉刷新和上拉加载更多实现 转 https://www.jianshu.com/p/4ea7c2d95ecf   在Android开发中,RecyclerView算是使用频率非常广泛的组件了吧,在这里对RecyclerView比较常用的下拉刷新和上拉加载更多的功能实现做个记录,方便以后查看。在这里下拉刷新使用的是官方提供的Swip...

各软件市场清单

前几天因为项目要上线搜索了一下安卓,iOS以及全平台的市场,网上有但不是很全。个人又总结了整理如下,有需要的同学就不用再费劲去查找了。                  安卓市场 序号 名字 网址 1 安卓在线 http://www.androidonline.net 2 安卓园 http:...

Android开发人员不得不收集的代码(不断更新中...)

尺寸相关 dp与px转换 sp与px转换 各种单位转换 在onCreate()即可获取View的宽高 ListView中提前测量View尺寸 手机相关 判断设备是否是手机 获取当前设备的IMIE,需与上面的isPhone一起使用 获取手机状态信息 是否有SD卡 获取MAC地址 获取手机厂商,如Xiaomi 获取手机型号...

Asp.net mvc与PHP的Session共享的实现

最近在做的一个ASP.NET MVC的项目,要用到第三方的php系统,为了实现两个系统的互联互通。决定将两者的session打通共享。让asp.net mvc 和php 都能正常访问和修改Session内容。 在决定实现之前,先搜索了一下院子里有没有相类似的文章,对于跨语言的程序互通,有两种方案: (1) SSO单点登录,其实就是把用户名和密码传给另一个系...

react组件间的传值方法

关于react的几个网站:  http://react.css88.com/ 小书:http://huziketang.mangojuice.top/books/react/ http://www.redux.org.cn/ 组件传值的方法: 1.父子组件间的传值 2.context 3.子组件向父组件传值 4.没有任何嵌套关系的组件之间传值 5.redu...

动态显示轮播图片

我指的动态是时时从数据库中取的数据,在页面上动态显示: 我用到的轮播插件是: carouFredSel  html: 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...