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 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:
@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
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 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現象出現。
米糊軟件開發室:http://shop62437931.taobao.com/?spm=0.0.0.0