我正在使用此library作为显示“ gif”文件的指南.当在drawableit中正确使用保存的gif时,gif会这样显示
<pl.droIDsonroIDs.gif.GifImageVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/imgVIEw1" androID:src="@drawable/giffile" />
但是当删除src并尝试像这样在Activity中初始化GifImageVIEw时
GifImageVIEw gifFromfile = (GifImageVIEw) findVIEwByID(R.ID.imgVIEw1);gifFromfile.setimageBitmap(Utils.getBitmAPImagefromStorage(context, imageHome));
其中,getBitmAPImagefromfromStorage获取路径,imageHome获取文件名,gif文件不会像图像那样仅显示其内容.据说如果给定的drawable不是GIF,则提到的VIEws就像普通的ImageVIEw和Imagebutton一样工作.但是我提供的文件是gif文件.我想知道我是否正确使用了它.
另外文件可以是png或gif,因此我需要支持两者.
解决方法:
从XML
最简单的方法是像普通ImageVIEw一样使用GifImageVIEw(或GifImagebutton):
<pl.droIDsonroIDs.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文件,则它们将被自动识别为Gifdrawables并进行动画处理.如果给定的drawable不是GIF,则提到的视图就像普通的ImageVIEw和Imagebutton一样工作.
GifTextVIEw允许您将GIF用作复合画图和背景.
<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" />
从Java代码
GifImageVIEw,GifImagebutton和GifTextVIEw也具有用于实现的setter的钩子.因此,可以通过调用setimageResource(int resID)和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 );
如果不再需要Gifdrawable,则在Finalizer中将自动关闭inputStreams,因此您无需显式关闭它们.调用recycle()也将关闭基础输入源.
请注意,所有输入源都必须具有后退到开头的功能.由于随后的帧是按需从源中解码的,因此需要正确播放动画GIF(可重复动画).
参考:https://github.com/koral–/android-gif-drawable
总结以上是内存溢出为你收集整理的android-如何显示GIF文件全部内容,希望文章能够帮你解决android-如何显示GIF文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)