转于:http://blog.csdn.net/up1up2up3/article/details/22682549
---------------------------------------------------------------------------------------
Android开源项目:GifView——Android显示GIF动画
作者:ant.cy.liao
主页:http://code.google.com/p/gifview/
下载:http://code.google.com/p/gifview/downloads/list
简介:android中现在没有直接显示gif的view,只能通过mediaplay来显示,且还常常不能正常显示出来,为此写了这个gifview,其用法和imageview一样
使用方法:
1-把GifView.jar加入你的项目。
2-在xml中配置GifView的基本属性,GifView继承自View类,和Button、ImageView一样是一个UI控件。如:
- <com.ant.liao.GifView android:id="@+id/gif2"
- android:layout_height="wrap_content" android:layout_width="wrap_content"
- android:paddingTop="4px" android:paddingLeft="14px" android:enabled="false" />
3-在代码中配置常用属性:
- // 从xml中得到GifView的句柄
- gf1 = (GifView) findViewById(R.id.gif1);
- // 设置Gif图片源
- gf1.setGifImage(R.drawable.gif1);
- // 添加监听器
- gf1.setOnClickListener(this);
- // 设置显示的大小,拉伸或者压缩
- gf1.setShowDimension(300, 300);
- // 设置加载方式:先加载后显示、边加载边显示、只显示第一帧再显示
- gf1.setGifImageType(GifImageType.COVER);
GifView的Jar包共有四个类:
GifAction.java:
观察者类,监视GIF是否加载成功
- package com.ant.liao;
- public interface GifAction {
- /**
- *gif解码观察者
- * @hide
- * @param parseStatus 解码是否成功,成功会为true
- * @param frameIndex 当前解码的第几帧,当全部解码成功后,这里为-1
- */
- public void parseOk(boolean parseStatus,int frameIndex); }
GifFrame.java
里面三个成员:当前图片、延时、下张Frame的链接。
- package com.ant.liao;
- import android.graphics.Bitmap;
- public class GifFrame {
- /**
- * 构造函数
- * @param im 图片
- * @param del 延时
- */
- public GifFrame(Bitmap im, int del) {
- image = im;
- delay = del;
- }
- public GifFrame(String name,int del){
- imageName = name;
- delay = del;
- }
- /**图片*/
- public Bitmap image;
- /**延时*/
- public int delay;
- /**当图片存成文件时的文件名*/
- public String imageName = null;
- /**下一帧*/
- public GifFrame nextFrame = null;
- }
GifDecoder.java
解码线程类
http://code.google.com/p/gifview/source/browse/trunk/src/com/ant/liao/GifDecoder.java
GifView.java
主类,包括常用方法,如GifView构造方法、设置图片源、延迟、绘制等。
http://code.google.com/p/gifview/source/browse/trunk/src/com/ant/liao/GifView.java
Android开源项目:android-gif-drawable——Android显示GIF动画
android-gif-drawable
View
s and Drawable
for animated GIFs in Android.
项目地址:https://github.com/koral--/android-gif-drawable
Overview
Bundled GIFLib via JNI is used to render frames. This way should be more efficient thanWebView
or Movie
classes.
Animation starts automatically and run only if View
with attached GifDrawable
is visible.
Download
Setup
Gradle (Android Studio)
Insert the following dependency to build.gradle
file of your project.
dependencies {
compile 'pl.droidsonroids.gif:android-gif-drawable:1.0.+'
}
Note that Maven central repository should be defined eg. in top-level build.gradle
like this:
buildscript {
repositories {
mavenCentral()
}
}
allprojects {
repositories {
mavenCentral()
}
}
Maven dependency
SDK with API level 19 is needed. If you don't have it in your local repository, downloadmaven-android-sdk-deployer and install SDK level 19:mvn install -P 4.4
(from maven-android-sdk-deployer directory). Then add dependency inpom.xml
of your project:
<dependency>
<groupId>pl.droidsonroids.gif</groupId>
<artifactId>android-gif-drawable</artifactId>
<version>insert latest version here</version>
<type>aar</type>
</dependency>
Requirements
- Android 1.6+ (API level 4+)
Building from source
- Android NDK needed to compile native sources
Usage
From XML
The simplest way is to use GifImageView
(or GifImageButton
) like a normalImageView
:
<pl.droidsonroids.gif.GifImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/src_anim"
android:background="@drawable/bg_anim"
/>
If drawables declared by android:src
and/or android:background
are GIF files then they will be automatically recognized asGifDrawable
s and animated. If given drawable is not a GIF then mentioned Views work like plainImageView
and ImageButton
.
GifTextView
allows you to use GIFs as compound drawables and background.
<pl.droidsonroids.gif.GifTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableTop="@drawable/left_anim"
android:drawableStart="@drawable/left_anim"
android:background="@drawable/bg_anim"
/>
From Java code
GifImageView
, GifImageButton
and GifTextView
have also hooks for setters implemented. So animated GIFs can be set by callingsetImageResource(int resId)
and setBackgroundResource(int resId)
GifDrawable
can be constructed directly from various sources:
//asset file
GifDrawable gifFromAssets = new GifDrawable( getAssets(), "anim.gif" );
//resource (drawable or raw)
GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );
//byte array
byte[] rawGifBytes = ...
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(),"anim.gif");
GifDrawable gifFromFile = new GifDrawable(gifFile);
//AssetFileDescriptor
AssetFileDescriptor afd = getAssets().openFd( "anim.gif" );
GifDrawable gifFromAfd = new GifDrawable( afd );
//InputStream (it must support marking)
InputStream sourceIs = ...
BufferedInputStream bis = new BufferedInputStream( sourceIs, GIF_LENGTH );
GifDrawable gifFromStream = new GifDrawable( bis );
//direct ByteBuffer
ByteBuffer rawGifBytes = ...
GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );
InputStreams are closed automatically in finalizer if GifDrawable is no longer needed so you don't need to explicitly close them. Callingrecycle()
will also close underlaying input source.
Note that all input sources need to have ability to rewind to the begining. It is required to correctly play animated GIFs (where animation is repeatable) since subsequent frames are decoded on demand from source.
Animation control
GifDrawable
implements an Animatable
and MediaPlayerControl
so you can use its methods and more:
-
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 givenposition
(in milliseconds) Only seeking forward is supported -
getDuration()
- returns duration of one loop of the animation -
getCurrentPosition()
- returns elapsed time from the beginning of a current loop of animation
UsingMediaPlayerControl
Standard controls for a MediaPlayer (like in VideoView) can be used to control GIF animation and show its current progress.
Just set GifDrawable
as MediaPlayer on your MediaController like this:
@Override
protected void onCreate ( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
GifImageButton gib = new GifImageButton( this );
setContentView( gib );
gib.setImageResource( R.drawable.sample );
final MediaController mc = new MediaController( this );
mc.setMediaPlayer( ( GifDrawable ) gib.getDrawable() );
mc.setAnchorView( gib );
gib.setOnClickListener( new OnClickListener()
{
@Override
public void onClick ( View v )
{
mc.show();
}
} );
}
Retrieving GIF metadata
-
getLoopCount()
- returns a loop count as defined inNETSCAPE 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)
Advanced
-
recycle()
- provided to speed up freeing memory (like inandroid.graphics.Bitmap
). -
getError()
- returns last error details
References
This library uses code from GIFLIB 5.0.5 and SKIA.
License
MIT License
See LICENSE file.
PS:
GifView:已知bug: 如果图档过大,会出现OOM
if the gif image is too large,maybe OOM.
为了解决图档太大时的OOM,我想把gif解析时的图片先存入到文件中,在显示时直接从文件中读入,但这样的话,显示的效果不好。
而android-gif-drawable并没有此问题,底层解码使用C实现,极大的提高了解码效率,同时很大程度上避免了OOM现象出现。
相关推荐
Glide的toBytes() 和transcode() 两个方法可以用来获取、解码和变换背景图片,并且transcode() 方法还能够改变图片的样式动画的支持: 新增支持图片的淡入淡出动画效果(调用crossFade()方法)和查看动画的属性的...
1、Android显示GIF动画 GifView GifView 是一个为了解决android中现在没有直接显示gif的view,只能通过mediaplay来显示这个问题的项目,其用法和 ImageView一样,支持gif图片 使用方法:1-把GifView.jar加入你的项目...
Java EJB中有、无状态SessionBean的两个例子 两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;在有状态SessionBean中,用...
两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;在有状态SessionBean中,用累加器,以对话状态存储起来,创建EJB对象,并将...
Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。 Beta 版 的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 ...
GIF 动画的解码 :通过调用Glide.with(context).load(“图片路径“)方法,GIF动画图片可以自动显示为动画效果。如果想有更多的控制,还可以使用Glide.with(context).load(“图片路径“).asBitmap()方法加载静态图片...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...
Android里开源库比较好的就是GifView了(项目地址:http://code.google.com/p/gifview/), 但是个人感觉还是有一些不足的地方,就是每个GifView都需要开启一个线程,界面上gif比较多时,卡顿比较严重。所以就作了...
GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,...