MainActivity如下:

package cc.testgif2;

import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述:
* 利用自定义View控件显示GIF动画
* 详细代码参见GIFView
*
* 参考资料:
* http://blog.csdn.net/dawanganban/article/details/9816083
* Thank you very much
*/
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

GIFView如下:

package cc.testgif2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View; public class GIFView extends View {
private Movie mMovie;
private long startTime;
private int gifDuration;
private boolean isBeginPlay=true;
public GIFView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} public GIFView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public GIFView(Context context) {
super(context);
init();
} private void init(){
mMovie = Movie.decodeStream(getResources().openRawResource(R.drawable.gif));
gifDuration= mMovie.duration();
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 从开机到现在的毫秒(不包括手机睡眠的时间在内)
long currrentTime = android.os.SystemClock.uptimeMillis();
// 第一次播放
if (isBeginPlay) {
startTime = currrentTime;
isBeginPlay = false;
} int playTime = (int) ((currrentTime - startTime) % gifDuration);
mMovie.setTime(playTime);
mMovie.draw(canvas, 0, 0);
// 重绘
invalidate();
} }

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"
> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerHorizontal="true"/> <cc.testgif2.GIFView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/> </RelativeLayout>

Android显示GIF动画完整示例(二)的更多相关文章

  1. Android显示GIF动画完整示例(一)

    MainActivity如下: package cc.testgif; import com.ant.liao.GifView; import com.ant.liao.GifView.GifImag ...

  2. Android显示GIF动画 GifView

    android中显示gif动画原生态一般支持的不是很好,故找了一个开源的项目,现简单介绍如下: GifView 是一个为了解决android中现在没有直接显示gif的view,只能通过mediapla ...

  3. Android中的AlertDialog使用示例二(普通选项对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  4. Android显示GIF动画(转载)

    GifView 是一个为了解决android中现在没有直接显示gif的view,只能通过mediaplay来显示这个问题的项目,其用法和 ImageView一样,支持gif图片 使用方法: 1-把Gi ...

  5. Android开源项目:GifView——Android显示GIF动画

    下载:http://code.google.com/p/gifview/downloads/list 简介:android中现在没有直接显示gif的view,只能通过mediaplay来显示,且还常常 ...

  6. Android窗口管理服务WindowManagerService显示窗口动画的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8611754 在前一文中,我们分析了Activi ...

  7. Android之Animation动画各属性的参数意思(二)

    现在就来讲讲Animation里这四个标签的属性. 一.这四个标签alpha.scale.translate.rotate共有的属性为: android:duration        动画持续时间, ...

  8. 【Android】21.4 图片动画缩放示例

    分类:C#.Android.VS2015: 创建日期:2016-03-21 一.简介 该例子演示如何动画缩放图片,实现类似"点击看大图"的效果. 二.示例 1.运行截图    2. ...

  9. Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表

    Android BLE与终端通信(二)--Android Bluetooth基础搜索蓝牙设备显示列表 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟, ...

随机推荐

  1. input输入框只能输入数字的功能

    Html代码 收藏代码 <input type="text" style="ime-mode:disabled;" onpaste="retur ...

  2. JavaSE_ API常用对象 总目录(11~14)

    JavaSE学习总结第11天_开发工具 & API常用对象111.01 常见开发工具介绍11.02 Eclipse和MyEclipse的概述11.03 Eclipse的下载安装及卸载11.04 ...

  3. hive 分区操作记录

    创建分区: alter table table_name add partition (dt='20150423') location '/data/text/20150423';

  4. Git使用方法记录(一)

    记录下git的基本使用方法,这里是以ubuntu14.04为例. 1,使用前的初始设置 git config –global user.name “FirstName LastName” git co ...

  5. Ubuntu 12.04中文输入法的安装(转)

    Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu的中文系统中自带了中文输入法,通过Ctrl+S ...

  6. 关于Qt信号与槽机制的传递方向性研究(结论其实是错误的,但是可以看看分析过程)

    最近由于项目的需求,一直在研究Qt.信号与槽机制是Qt的一大特色,该机制允许两者间传递参数,依次来实现对象间的通信.这个参数会分别存在于信号的参数列表和槽函数的参数列表中.需要注意的是,若将槽函数绑定 ...

  7. 命令查询分离原则Command-query separation principle

    在UML和模式应用一书中,发送给Die的roll消息之后跟随着第二个消息getFaceValue用于提取其新的faceValue,特别是:roll()方法是void的,没有返回值,例如: public ...

  8. 关于COOKIE使用过程为NULL

    关于COOKIE使用过程中的一个小问题在程序中要用到COOKIE,网站website/login/login.aspx传值userID到 website/web/tab/web.aspx中的FRAME ...

  9. hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...

  10. Flex 内置验证器—验证用户输入

    今晚对于Flex中的Validator类(所有验证器的父类)测试一下 ---->其中常用的验证类有StringValidator,NumberValidator,DateValidator 测试 ...