• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

WayGif: Android端通过jni解析显示gif图片,更高效,更速度。

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

WayGif

开源软件地址:

https://gitee.com/way/WayGif

开源软件介绍:

##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 controlGifDrawable 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 MediaPlayerControlStandard 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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap