动态图片 Movie android-gif-drawable GifView
Movie 类
简单的利用Movie播放GIF图的控件
setContentView(new CustomGifView(this, R.drawable.ic_launcher)); //显示普通图片
setContentView(new CustomGifView(this, R.drawable.gif1)); //显示gif动图
/**
* 自定义可以循环播放gif动画的View,可以像使用其他控件一样使用
* @author 白乾涛
*/
public class CustomGifView extends View {
private Movie mMovie;
private long mMovieStart;
private int resId;
public CustomGifView(Context context, int resId) {
super(context);
this.resId = resId;
setLayerType(View.LAYER_TYPE_SOFTWARE, null);//必须关闭硬件加速
mMovie = Movie.decodeStream(getResources().openRawResource(resId));//创建Movie对象
}
public void onDraw(Canvas canvas) {
long now = SystemClock.uptimeMillis();//系统当前时刻
//第一次播放
if (mMovieStart == 0) mMovieStart = now;//动画开始的时间
if (mMovie != null) {
int dur = mMovie.duration();//动画持续的时间,也就是完成一次动画的时间
if (dur == 0) dur = 1000;
int relTime = (int) ((now - mMovieStart) % dur);//注意这是取余操作,这才能算出当前这次重复播放的第一帧的时间
mMovie.setTime(relTime);//设置相对本次播放第一帧时间,根据这个时间来决定显示第几帧
mMovie.draw(canvas, 0, 0);
invalidate();
} else {//如果指定的资源不是gif图片,也就是说是普通的图片,则需要手动绘制此图片
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), resId), getMatrix(), new Paint());
}
}
}
Movie 类的 API
- static Movie decodeByteArray(byte[] data, int offset, int length)
- static Movie decodeFile(String pathName)
- static Movie decodeStream(InputStream is)
- void draw(Canvas canvas, float x, float y, Paint paint)
- void draw(Canvas canvas, float x, float y)
- int duration()
- int height()
- boolean isOpaque()
- boolean setTime(int relativeMilliseconds)
- int width()
开源库 android-gif-drawable
- Android 2.3+ (API level 9+)
- for GifTextureView Android 4.0+ (API level 14+) and hardware-accelerated rendering渲染
- for GifTexImage2D OpenGL ES 2.0+
- Android NDK needed to compile native sources
配置build.gradle
dependencies {
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.6'
}
Note that Maven central repository should be defined eg. in top-level build.gradle like this:
buildscript {
repositories {
mavenCentral()
}
}
allprojects {
repositories {
mavenCentral()
}
}
XML中使用
<pl.droidsonroids.gif.GifImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gif1"
android:src="@drawable/gif2"/>
<pl.droidsonroids.gif.GifTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gif1"
android:drawableTop="@drawable/gif2"/>
代码中构造GifDrawable
void constructGifDrawable() throws IOException {
//asset file
GifDrawable gifFromAssets = new GifDrawable(getAssets(), "gif1.gif");
//resource (drawable or raw)
GifDrawable gifFromResource = new GifDrawable(getResources(), R.drawable.gif1);
//Uri
ContentResolver contentResolver = null; //can be null for file:// Uris
GifDrawable gifFromUri = new GifDrawable(contentResolver, null);//gifUri
//byte array
byte[] rawGifBytes = null;
GifDrawable gifFromBytes = new GifDrawable(rawGifBytes);
//FileDescriptor
FileDescriptor fd = new RandomAccessFile("/path/anim.gif", "r").getFD();
GifDrawable gifFromFd = new GifDrawable(fd);
//file path
GifDrawable gifFromPath = new GifDrawable("/path/anim.gif");
//file
File gifFile = new File(getFilesDir(), "gif1.gif");
GifDrawable gifFromFile = new GifDrawable(gifFile);
//AssetFileDescriptor
AssetFileDescriptor afd = getAssets().openFd("gif1.gif");
GifDrawable gifFromAfd = new GifDrawable(afd);
//InputStream (it must support marking)
InputStream sourceIs = null;
BufferedInputStream bis = new BufferedInputStream(sourceIs, 1024);//GIF_LENGTH
GifDrawable gifFromStream = new GifDrawable(bis);
//direct ByteBuffer
ByteBuffer rawGifByteBuffer = null;
GifDrawable gifFromByteBuffer = new GifDrawable(rawGifByteBuffer);
}
动画过程控制 Animation control
- stop() - stops the animation, can be called from any thread
- start() - starts the animation, can be called from any thread
- isRunning() - returns whether animation is currently running or not
- reset() - rewinds the animation, does not restart stopped one
- setSpeed(float factor) - sets new animation speed factor, eg. passing 2.0f will double the animation speed
- seekTo(int position) - seeks animation (within current loop) to given position (in milliseconds)
- getDuration() - returns duration of one loop of the animation
- getCurrentPosition() - returns elapsed time from the beginning of a current loop of animation
使用 MediaPlayerControl
@BindView(R.id.gif) GifImageView gifView;
android.widget.MediaController mc;
mc = new MediaController(this);
mc.setMediaPlayer((pl.droidsonroids.gif.GifDrawable) gifView.getDrawable());
mc.setAnchorView(gifView);
mc.show();
获取GIF元数据
- getLoopCount() - returns a loop count as defined in NETSCAPE 2.0 extension
- getNumberOfFrames() - returns number of frames (at least 1)
- getComment() - returns comment text (null if GIF has no comment)
- getFrameByteCount() - returns minimum number of bytes that can be used to store pixels of the single frame
- getAllocationByteCount() - returns size (in bytes) of the allocated memory used to store pixels of given GifDrawable
- getInputSourceByteCount() - returns length (in bytes) of the backing input data
- toString() - returns human readable information about image size and number of frames (intended for debugging purpose)
一个GifDrawable用在多个View上
MultiCallback multiCallback = new MultiCallback();
imageView.setImageDrawable(gifDrawable);
multiCallback.addView(imageView);
anotherImageView.setImageDrawable(gifDrawable);
multiCallback.addView(anotherImageView);
gifDrawable.setCallback(multiCallback);
高级API
开源库 Cutta/GifView
Library for playing gifs on Android
Simple android view to display gifs efficiently高效的. You can start, pause and stop gifView.
Inspired by灵感来自 sbakhtiarov/gif-movie-view

AS中使用
// Top-level build file where you can add configuration options common to all sub-projects/modules.
repositories {
maven {
url "https://jitpack.io"
}
}
On app's build.gradle
compile 'com.github.Cutta:GifView:1.1'
xmlns:custom="http://schemas.android.com/apk/res-auto"
<com.cunoraz.gifview.library.GifView
android:id="@+id/gif"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
custom:gif="@drawable/gif1"/>
GifView gifView = (GifView) view.findViewById(R.id.gif);
gifView.pause(); //默认为自动播放,可以手动设置 custom:paused="true"
Eclipse中使用
<declare-styleable name="GifView">
<attr name="gif" format="reference" />
<attr name="paused" format="boolean" />
</declare-styleable>
<com.bqt.GifView
android:id="@+id/gif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:gif="@drawable/gif" />
附件列表
动态图片 Movie android-gif-drawable GifView的更多相关文章
- iOS 播放GIF动态图片!!!!!
////////////////////////////////////////////////////////////////////////////////////////// //// Vie ...
- Android—基于GifView显示gif动态图片
android中显示gif动态图片用到了开源框架GifView 1.拷GifView.jar到自己的项目中. 2.将自己的gif图片拷贝到drawable文件夹 3.在xml文件中设置基本属性: &l ...
- Android中显示gif动态图片
在android中显示一个静态图片比如png jpg等等都很方便,但是如果要显示一个gif 动态图片就需要进行一些处理. 本文是采用自定义view 然后进行重新onDraw方法来实现 首先自定义Vie ...
- Android之动态图片
在Android中,比起静态图片来动态图片会更加生动更加酷炫,因为这种视觉效果,你应该会发现我们手机中大多数应用软件的导航页面也都是采用动态图片来展示.动态图片的格式有gif.png格式等等. 我们就 ...
- 动态图片 gif
简介 android不推荐使用gif图片,一般都是png的,对于gif的图片解析比较消耗资源,但是对于一些动态gif图片的播放,如果比较小的话还是可以的,要是大的话,建议还是把gif图片转换成一帧一帧 ...
- 布局之按钮的图片分辨率--Android Studio
在布局页面,想把取消按钮和确认钮大小一致,刚开始想法是错的,不用在控制层设置,也不用在布局层压缩图片,有两个方法法: 1.直接用美图秀秀“尺寸”功能,修改成另一按钮一样的分辨率. 2.设置按钮相同高度 ...
- Android使用Drawable资源之使用ClipDrawable资源 实现进入条
以前我自定义的进度条(就是咱们现在工程中用的)是从android的源码中扒出来的一个XML,然后把里面的图片给替换了.一直不知道它的具体原理是什么. 今天得空研究了一下,发现它的原理其实就是用的and ...
- Android之drawable state各个属性详解
android:drawable 放一个drawable资源android:state_pressed 是否按下,如一个按钮触摸或者点击.android:state_focused 是否取得焦点,比如 ...
- Android动画Drawable Animation
Drawable Animation是逐帧动画,那么使用它之前必须先定义好各个帧.我们可以通过代码定义,也可以使用xml文件定义,一般使用后者.如下: <?xml version="1 ...
随机推荐
- Keras/tensorflow出现‘Loaded runtime CuDNN library: 7.0.5 but source was compiled with: 7.1.14’错误的解决办法
从tensorflow1.10 升级到1.12版本后,对依赖的CuDNN不兼容产生的问题.鉴于一直使用的是Keras,未使用新版本tensorflow的功能,故果断回退到旧版本. 方法为:pip3 i ...
- Linux-数据库4
存储引擎 什么是存储引擎? mysql中建的库是文件夹,建的表是文件.文件有不同的类型,数据库中的表也有不同的类型,表的类型不同,会对应mysql不同的存取机制,表类型又称为存储引擎. 存储引擎说白了 ...
- 【BZOJ 1415】 1415: [Noi2005]聪聪和可可 (bfs+记忆化搜索+期望)
1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1640 Solved: 962 Description I ...
- Ubuntu安装redis和redis-php扩展
通过apt-get安装的redis使用方法 sudo apt-get install redis-server sudo apt-get install php-redis vim /etc/redi ...
- QT学习笔记4:QT中GraphicsView编程
一.QGraphicsScene 1.QGraphicsScene QGraphicsScene继承自QObject,是一个管理图元的容器,与QGraphicsView合用可以在2D屏幕上显示如线.三 ...
- 线性表之单链表C++实现
线性表之单链表 一.头文件:LinkedList.h //单链表是用一组任意的存储单元存放线性表的元素,这组单元可以是连续的也可以是不连续的,甚至可以是零散分布在内存中的任意位置. //单链表头文件 ...
- 【Dijkstra】POJ1062-昂贵的聘礼
由于物品编号从1开始,我们假定0也是一个物品作为起点,它到其它物品的距离就是各个物品的原始价值.开始时,如果两种物品主人的等级限制M在规定范围以内,且j能用i替换,则将优惠价格视作从i到j的一条权值为 ...
- 【差分约束系统/SPFA】POJ3169-Layout
[题目大意] n头牛从小到大排,它们之间某些距离不能大于一个值,某些距离不能小于一个值,求第一头牛和第N头牛之间距离的最大值. [思路] 由题意可以得到以下不等式d[AL]+DL≥d[BL]:d[BD ...
- 【POJ】1840:Eqs【哈希表】
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18299 Accepted: 8933 Description ...
- IE11 全新的F12开发者工具
我讨厌debug,相信也没多少开发者会喜欢.但是当代码出错之后肯定是要找出问题出在哪里的.不过网页开发的时候遇到 BUG 是一件再正常不过的事情了,我们不能保证自己的代码万无一失,于是使用浏览器的 ...