28 Star 44 Fork 24

WAY / WayGif

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

##WayGif

Android端通过jni解析显示gif图片,更高效,更速度。 原项目来自Github: :blush: android-gif-drawable

##简单使用

###在layout布局XML文件中: 跟使用普通的 ImageView 一样使用 GifImageView (或者 GifImageButton):

<com.way.gif.GifImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/src_anim"
    android:background="@drawable/bg_anim"
    />

如果 android:src 或者 android:background 使用的是GIF文件,运行程序后,将会自动播放。

GifTextView 允许使用gif文件作为背景.

<com.way.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"
    />

###在Java代码中: GifImageView, GifImageButton and GifTextView 可以直接调用 setImageResource(int resId) and setBackgroundResource(int resId) 设置GIF文件图片。

GifDrawable 也能在代码中直接实例化:

		//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 会在 GifDrawable 不再使用时自动关闭,因此你不用特意去关闭它,当然也可以调用 recycle()

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 given position (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

#####Using MediaPlayerControl 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 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)

####Advanced

测试截图

Screenshot 1

空文件

简介

Android端通过jni解析显示gif图片,更高效,更速度。 展开 收起
Android
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Android
1
https://gitee.com/way/WayGif.git
git@gitee.com:way/WayGif.git
way
WayGif
WayGif
master

搜索帮助

14c37bed 8189591 565d56ea 8189591