【Android】使用android-gif-drawable包加载GIF动图
程序员文章站
2022-06-02 22:22:02
【导包】
首先需要导入android-gif-drawable包,请参考:【android】实用教程:导入android-gif-drawable包,不用在github下载(android stud...
【导包】
首先需要导入android-gif-drawable包,请参考:【android】实用教程:导入android-gif-drawable包,不用在github下载(android studio 3.1.2)
【使用】
一、在layout中添加gifimageview控件,该控件既可以加载gif动态图,也可以加载jpg、png静态图。不需要设置src属性。
二、在java文件中,给gifimageview设置src属性,加载gif动态图。
1、获取gifimageview控件。
gifimageview gifimageview = findviewbyid(r.id.image);
2、实例化gifdrawable对象,共有11种方法。
(1)resouces文件
:
/** * creates drawable from resource. * * @param res resources to read from * @param id resource id (raw or drawable) * @throws notfoundexception if the given id does not exist. * @throws ioexception when opening failed * @throws nullpointerexception if res is null */ public gifdrawable(@nonnull resources res, @rawres @drawableres int id) throws notfoundexception, ioexception { this(res.openrawresourcefd(id)); final float densityscale = gifviewutils.getdensityscale(res, id); mscaledheight = (int) (mnativeinfohandle.getheight() * densityscale); mscaledwidth = (int) (mnativeinfohandle.getwidth() * densityscale); }
示例:
// resources file gifdrawable giffromassets = new gifdrawable(getresources(), r.mipmap.timg);
(2)assets文件
源码:
/** * creates drawable from asset. * * @param assets assetmanager to read from * @param assetname name of the asset * @throws ioexception when opening failed * @throws nullpointerexception if assets or assetname is null */ public gifdrawable(@nonnull assetmanager assets, @nonnull string assetname) throws ioexception { this(assets.openfd(assetname)); }
示例:
// assets file gifdrawable giffromassets = new gifdrawable(getassets(), "timg.gif");
(3)文件路径
源码:
/** * constructs drawable from given file path. * only metadata is read, no graphic data is decoded here. * in practice can be called from main thread. however it will violate * {@link strictmode} policy if disk reads detection is enabled. * * @param filepath path to the gif file * @throws ioexception when opening failed * @throws nullpointerexception if filepath is null */ public gifdrawable(@nonnull string filepath) throws ioexception { this(new gifinfohandle(filepath), null, null, true); }
示例:
// path to the gif file gifdrawable giffrompath = new gifdrawable("/path/timg.gif");
(4)file文件
源码:
/** * equivalent to {@code} gifdrawable(file.getpath())} * * @param file the gif file * @throws ioexception when opening failed * @throws nullpointerexception if file is null */ public gifdrawable(@nonnull file file) throws ioexception { this(file.getpath()); }
示例:
// the gif file file giffile = new file(getfilesdir(), "timg.gif"); gifdrawable giffromfile = new gifdrawable(giffile);
(5)输入流stream to read from
源码:
/** * creates drawable from inputstream. * inputstream must support marking, illegalargumentexception will be thrown otherwise. * * @param stream stream to read from * @throws ioexception when opening failed * @throws illegalargumentexception if stream does not support marking * @throws nullpointerexception if stream is null */ public gifdrawable(@nonnull inputstream stream) throws ioexception { this(new gifinfohandle(stream), null, null, true); }
(6)assetfiledescriptor
源码:
/** * creates drawable from assetfiledescriptor. * convenience wrapper for {@link gifdrawable#gifdrawable(filedescriptor)} * * @param afd source * @throws nullpointerexception if afd is null * @throws ioexception when opening failed */ public gifdrawable(@nonnull assetfiledescriptor afd) throws ioexception { this(new gifinfohandle(afd), null, null, true); }
示例:
// creates drawable from assetfiledescriptor assetfiledescriptor assetfiledescriptor = getassets().openfd("timg.gif"); gifdrawable giffromafd = new gifdrawable(assetfiledescriptor);
(7)filedescriptor
源码:
/** * creates drawable from filedescriptor * * @param fd source * @throws ioexception when opening failed * @throws nullpointerexception if fd is null */ public gifdrawable(@nonnull filedescriptor fd) throws ioexception { this(new gifinfohandle(fd), null, null, true); }
示例:
// creates drawable from filedescriptor filedescriptor filedescriptor = new randomaccessfile("/path/timg.gif", "r").getfd(); gifdrawable giffromfd = new gifdrawable(filedescriptor);
(8)raw gif bytes
源码:
* creates drawable from byte array. * it can be larger than size of the gif data. bytes beyond gif terminator are not accessed. * * @param bytes raw gif bytes * @throws ioexception if bytes does not contain valid gif data * @throws nullpointerexception if bytes are null */ public gifdrawable(@nonnull byte[] bytes) throws ioexception { this(new gifinfohandle(bytes), null, null, true); }
(9)bytebuffer
源码:
/** * creates drawable from {@link bytebuffer}. only direct buffers are supported. * buffer can be larger than size of the gif data. bytes beyond gif terminator are not accessed. * * @param buffer buffer containing gif data * @throws ioexception if buffer does not contain valid gif data or is indirect * @throws nullpointerexception if buffer is null */ public gifdrawable(@nonnull bytebuffer buffer) throws ioexception { this(new gifinfohandle(buffer), null, null, true); }
(10)contentresolver
源码:
/** * creates drawable from {@link android.net.uri} which is resolved using {@code resolver}. * {@link android.content.contentresolver#openassetfiledescriptor(android.net.uri, string)} * is used to open an uri. * * @param uri gif uri, cannot be null. * @param resolver resolver used to query {@code uri}, can be null for file:// scheme uris * @throws ioexception if resolution fails or destination is not a gif. */ public gifdrawable(@nullable contentresolver resolver, @nonnull uri uri) throws ioexception { this(gifinfohandle.openuri(resolver, uri), null, null, true); }
(11)inputsource
源码:
/** * creates drawable from {@link inputsource}. * * @param inputsource the {@link inputsource} concrete subclass used to construct {@link gifdrawable}. * @param olddrawable the old drawable that will be reused to save the memory. can be null. * @param executor the executor for rendering tasks. can be null. * @param isrenderingtriggeredondraw true if rendering of the next frame is scheduled after drawing current one, false otherwise. * @param options options controlling various gif parameters. * @throws ioexception if input source is invalid. */ protected gifdrawable(@nonnull inputsource inputsource, @nullable gifdrawable olddrawable, @nullable scheduledthreadpoolexecutor executor, boolean isrenderingtriggeredondraw, @nonnull gifoptions options) throws ioexception { this(inputsource.createhandlewith(options), olddrawable, executor, isrenderingtriggeredondraw); }
3、设置gifimageview控件的src。
gifimageview.setimagedrawable(gifdrawable);
【注意】
gifimageview控件可以加载静态图,步骤如下:
drawable drawable = getdrawable(r.mipmap.ic_launcher); gifimageview.setimagedrawable(drawable);gifdrawable类的对象读取静态图可能会报错。
上一篇: 自定义时间选择控件(仿ios滚动效果)
下一篇: oracle用户创建及权限设置命令教程