在自己的包中添加MyGifView.java(直接复制,粘贴),读取gif资源在MyGifView中第20行读取:

MyGifView.java:

 package com.zzw.testgifuseimageview;

 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 { private long movieStart;
private Movie movie; // 重写该构造方法
public MyGifView(Context context, AttributeSet attributeSet) {
super(context, attributeSet); // 以文件流(InputStream)读取进gif图片资源
movie = Movie.decodeStream(getResources().openRawResource(R.drawable.test));
} @Override
protected void onDraw(Canvas canvas) { long curTime = android.os.SystemClock.uptimeMillis(); // 第一次播放
if (movieStart == 0) {
movieStart = curTime;
}
if (movie != null) {
int duraction = movie.duration();
int relTime = (int) ((curTime - movieStart) % duraction);
movie.setTime(relTime);
movie.draw(canvas, 0, 0);
// 强制重绘
invalidate();
} super.onDraw(canvas);
}
}

把要放gif图片的ImageView的名字改为  包名.MyGifView

<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="com.zzw.testgifuseimageview.MainActivity" > <com.zzw.testgifuseimageview.MyGifView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp" /> </RelativeLayout>

MainActivity.java:

 package com.zzw.testgifuseimageview;

 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);
}
}

让ImageView可以使用gif的方法的更多相关文章

  1. Android零基础入门第22节:ImageView的属性和方法大全

    原文:Android零基础入门第22节:ImageView的属性和方法大全 通过前面几期的学习,TextView控件及其子控件基本学习完成,可以在Android屏幕上显示一些文字或者按钮,那么从本期开 ...

  2. [Xcode 实际操作]六、媒体与动画-(9)使用CATransaction Push制作入场动画

    目录:[Swift]Xcode实际操作 本文将演示如何制作入场动画. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class View ...

  3. [Xcode 实际操作]六、媒体与动画-(8)使用CATransaction Reveal制作渐显动画

    目录:[Swift]Xcode实际操作 本文将演示如何制作渐显动画. 图片的不透明度逐渐发生了变化,从而产生作渐显动画的效果. 在项目导航区,打开视图控制器的代码文件[ViewController.s ...

  4. Android 自定义View修炼-实现自定义圆形、圆角和椭圆ImageView(使用Xfermode图形渲染方法)

    一:简介: 在上一篇<Android实现圆形.圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)>博文中,采用BitmapShader方法实现自定义的圆形.圆角等自定 ...

  5. IOS中图片拉伸技巧与方法总结(转载)

    以下内容转载自:http://my.oschina.net/u/2340880/blog/403996 IOS中图片拉伸技巧与方法总结 一.了解几个图像拉伸的函数和方法 1.直接拉伸法 简单暴力,却是 ...

  6. 转:探讨android更新UI的几种方法

    本文转自:http://www.cnblogs.com/wenjiang/p/3180324.html 作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因 ...

  7. 【转】探讨android更新UI的几种方法----不错

    原文网址:http://www.cnblogs.com/wenjiang/p/3180324.html 作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因 ...

  8. 探讨android更新UI的几种方法

    作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因为现实是残酷的,就算一开始的时间和精力非常充足,也会随着项目的推进而逐步消磨殆尽.我们会发现,自己越来越 ...

  9. NSThread 在主线操作的三个方法

    - (void)createNSThread444{ UIImage *image = [UIImage imageNamed:@"图片名字"]; /** 1 performSel ...

随机推荐

  1. Phong光照以及其他

        在说光照之前,有必要先弄清法线的变换,假设Mworld 将物体的定点一一变换到世界空间,如果我们对法线实施同样的变化,,以为能将法线变换到世界空间,但世界上变换之后的法线不再与表面垂直,就像下 ...

  2. 2.1.4 扫描器X-Scan查本机隐患

    X-Scan是由安全焦点开发的一个功能强大的扫描工具.它采用多线程方式对指定IP地址段(或单机)进行安全漏洞检测,支持插件功能. 1.用X-Scan查看本机IP地址 利用X-Scan扫描器来查看本机的 ...

  3. 【开源项目8】Android开源项目分类汇总【畜生级别】

    欢迎大家推荐好的Android开源项目,可直接Commit或在 收集&提交页 中告诉我,欢迎Star.Fork :) 微博:Trinea    主页:www.trinea.cn    邮箱:t ...

  4. 安装 vsftpd

    (1)安装vsftpdsudo apt-get install vsftpd (2)配置sudo vi /etc/vsftpd.conf #anonymous_enable=YESlocal_enab ...

  5. Amazon全场满$35减$5 (需Facebook)

    在Amazon.com上建一个Wish List(创建Wish List后须点击“Manage your lists”,在“Privacy”那一栏选择“Public”或“Shared”,再回到这个链接 ...

  6. css 所有选择器

    选择器 例子 例子描述 CSS .class .intro 选择 class="intro" 的所有元素. 1 #id #firstname 选择 id="firstna ...

  7. 100-The 3n + 1 problem

    本文档下载 题目: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...

  8. 写多个物件css3循环动画案例原理

    div { background-color: #67CF22; height: 100%; width: 6px; display: inline-block; -webkit-animation: ...

  9. hdu3336

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. CSS3笔记

    CSS/CSS3在线手册:http://www.css119.com/book/css/   CSS3实现水平垂直居中:http://bbs.html5cn.org/thread-87300-1-1. ...