gifview
主页: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控件。如:
view
sourceprint?
1.<com.ant.liao.GifView
android:id="@+id/gif2"
2.android:layout_height="wrap_content" android:layout_width="wrap_content"
3.android:paddingTop="4px" android:paddingLeft="14px" android:enabled="false" />
3- 在代码中配置常用属性:
view
sourceprint?
01.//
从xml中得到GifView的句柄
02.gf1
= (GifView) findViewById(R.id.gif1);
03.//
设置Gif图片源
04.gf1.setGifImage(R.drawable.gif1);
05.//
添加监听器
06.gf1.setOnClickListener(this);
07.//
设置显示的大小,拉伸或者压缩
08.gf1.setShowDimension(300, 300);
09.//
设置加载方式:先加载后显示、边加载边显示、只显示第一帧再显示
10.gf1.setGifImageType(GifImageType.COVER);
GifView的Jar包共有四个类:
GifAction.java:
观察者类,监视GIF是否加载成功
view
sourceprint?
01.package com.ant.liao;
02.public interface GifAction
{
03./**
04.*gif解码观察者
05.*
@hide
06.*
@param parseStatus 解码是否成功,成功会为true
07.*
@param frameIndex 当前解码的第几帧,当全部解码成功后,这里为-1
08.*/
09.public void parseOk(boolean parseStatus,int frameIndex);
}
GifFrame.java
里面三个成员:当前图片、延时、下张Frame的链接。
view
sourceprint?
01.package com.ant.liao;
02.import android.graphics.Bitmap;
03.public class GifFrame
{
04./**
05.*
构造函数
06.*
@param im 图片
07.*
@param del 延时
08.*/
09.public GifFrame(Bitmap
im, int del)
{
10.image
= im;
11.delay
= del;
12.}
13.public GifFrame(String
name,int del){
14.imageName
= name;
15.delay
= del;
16.}
17./*图片/
18.public Bitmap
image;
19./*延时/
20.public int delay;
21./*当图片存成文件时的文件名/
22.public String
imageName = null;
23./*下一帧/
24.public GifFrame
nextFrame = null;
25.}
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-gif-drawable
android-gif-drawable
Views 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
Latest release downloads
Setup
Gradle (Android Studio)
Insert the following dependency to build.gradle file of your project.
view
sourceprint?
1.dependencies
{
2.compile 'pl.droidsonroids.gif:android-gif-drawable:1.0.+'
3.}
Note that Maven central repository should be defined eg. in top-level build.gradle like this:
view
sourceprint?
01.buildscript
{
02.repositories
{
03.mavenCentral()
04.}
05.}
06.allprojects
{
07.repositories
{
08.mavenCentral()
09.}
10.}
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:
view
sourceprint?
1.<dependency>
2.<groupId>pl.droidsonroids.gif</groupId>
3.<artifactId>android-gif-drawable</artifactId>
4.<version>insert
latest version here</version>
5.<type>aar</type>
6.</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:
view
sourceprint?
1.<pl.droidsonroids.gif.GifImageView
2.android:layout_width="match_parent"
3.android:layout_height="match_parent"
4.android:src="@drawable/src_anim"
5.android:background="@drawable/bg_anim"
6./>
If drawables declared by android:src and/or android:background are GIF files then they will be automatically recognized asGifDrawables 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.
view
sourceprint?
1.<pl.droidsonroids.gif.GifTextView
2.android:layout_width="match_parent"
3.android:layout_height="match_parent"
4.android:drawableTop="@drawable/left_anim"
5.android:drawableStart="@drawable/left_anim"
6.android:background="@drawable/bg_anim"
7./>
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:
view
sourceprint?
01.//asset
file
02.GifDrawable
gifFromAssets = new GifDrawable(
getAssets(), "anim.gif" );
04.//resource
(drawable or raw)
05.GifDrawable
gifFromResource = new GifDrawable(
getResources(), R.drawable.anim );
07.//byte
array
08.byte[]
rawGifBytes = ...
09.GifDrawable
gifFromBytes = new GifDrawable(
rawGifBytes );
11.//FileDescriptor
12.FileDescriptor
fd = new RandomAccessFile( "/path/anim.gif", "r" ).getFD();
13.GifDrawable
gifFromFd = new GifDrawable(
fd );
15.//file
path
16.GifDrawable
gifFromPath = new GifDrawable( "/path/anim.gif" );
18.//file
19.File
gifFile = new File(getFilesDir(),"anim.gif");
20.GifDrawable
gifFromFile = new GifDrawable(gifFile);
22.//AssetFileDescriptor
23.AssetFileDescriptor
afd = getAssets().openFd( "anim.gif" );
24.GifDrawable
gifFromAfd = new GifDrawable(
afd );
26.//InputStream
(it must support marking)
27.InputStream
sourceIs = ...
28.BufferedInputStream
bis = new BufferedInputStream(
sourceIs, GIF_LENGTH );
29.GifDrawable
gifFromStream = new GifDrawable(
bis );
31.//direct
ByteBuffer
32.ByteBuffer
rawGifBytes = ...
33.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 threadstart() - starts the animation, can be called from any threadisRunning() - returns
whether animation is currently running or notreset() - rewinds the animation, does not restart stopped onesetSpeed(float factor) - sets new animation speed factor, eg. passing 2.0f will double the animation speedseekTo(int position) - seeks animation (within
current loop) to givenposition (in milliseconds) Only seeking forward is supportedgetDuration() - returns
duration of one loop of the animationgetCurrentPosition() - 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:
view
sourceprint?
01.@Override
02.protected void onCreate
( Bundle savedInstanceState )
03.{
04.super.onCreate(
savedInstanceState );
05.GifImageButton
gib = new GifImageButton( this );
06.setContentView(
gib );
07.gib.setImageResource(
R.drawable.sample );
08.final MediaController
mc = new MediaController( this );
09.mc.setMediaPlayer(
( GifDrawable ) gib.getDrawable() );
10.mc.setAnchorView(
gib );
11.gib.setOnClickListener( new OnClickListener()
12.{
13.@Override
14.public void onClick
( View v )
15.{
16.mc.show();
17.}
18.}
);
19.}
Retrieving GIF metadata
getLoopCount() - returns a loop count as defined in NETSCAPE 2.0 extensiongetNumberOfFrames() - 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 framegetAllocationByteCount() - returns size (in bytes) of the allocated memory used to store pixels of given GifDrawablegetInputSourceByteCount()
- returns length (in bytes) of the backing input datatoString() - returns human readable information about image size and number of frames (intended for debugging purpose)
Advanced
recycle() - provided to speed up freeing memory (like in android.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现象出现。