13 Star 2 Fork 11

OpenHarmony-SIG / ohos_gif-drawable

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

ohos-gif-drawable

简介

本项目是OpenHarmony系统的一款GIF图像渲染库,基于Canvas进行绘制,主要能力如下:

  • 支持播放GIF图片。
  • 支持控制GIF播放/暂停。
  • 支持重置GIF播放动画。
  • 支持调节GIF播放速率。
  • 支持监听GIF所有帧显示完成后的回调。
  • 支持设置显示大小。
  • 支持7种不同的展示类型。
  • 支持设置显示区域背景颜色。

下载安装

ohpm install @ohos/gif-drawable

使用说明

1.使用taskpool替代worker,无需额外工程配置

2.在需要展示的位置使用GIFComponent自定义组件

import { GIFComponent, ResourceLoader } from '@ohos/gif-drawable'
// gif绘制组件用户属性设置
@State model:GIFComponent.ControllerOptions = new GIFComponent.ControllerOptions();
// 是否自动播放
@State gifAutoPlay:boolean = true;
// 重置GIF播放,每次取反都能生效
@State gifReset:boolean = true;

// 在ARKUI的其他容器组件中添加该组件 如果需要绑定滚动组件内部可见/不可见状态 gif自动暂停开始播放能力,只需要加上bindScroller字段即可。
GIFComponent({model:$model, autoPlay:$gifAutoPlay, resetGif: $gifReset})

3.配置 GIFComponent.ControllerOptions达到更多的效果

// 关闭动画			
this.gifAutoPlay = false;
// 销毁上一次资源
this.model.destroy();
// 新创建一个modelx,用于配置用户参数
let modelx = new GIFComponent.ControllerOptions()
modelx
  // 配置回调动画结束监听,和耗时监听	
  .setLoopFinish((loopTime) => {
   this.gifLoopCount++;
   this.loopHint = '当前gif循环了' + this.gifLoopCount + '次,耗时=' + loopTime + 'ms'
   })
  // 设置图像和组件的适配类型
  .setScaleType(this.scaleType)
  // 设置播放速率
  .setSpeedFactor(this.speedFactor)
// 加载网络图片,getContext(this)中的this指向page页面或者组件都可以
ResourceLoader.downloadDataWithContext(getContext(this), {
   url: 'https://pic.ibaotu.com/gif/18/17/16/51u888piCtqj.gif!fwpaa70/fw/700'
   }, (sucBuffer) => {
    // 网络资源sucBuffer返回后处理
   modelx.loadBuffer(sucBuffer, () => {
   
   console.log('网络加载解析成功回调绘制!')
    // 开启自动播放   
   this.gifAutoPlay = true;
    // 给组件数据赋新的用户配置参数,达到后续gif动画效果   
   this.model = modelx;
   })
}, (err) => {
   // 用户根据返回的错误信息,进行业务处理(展示一张失败占位图、再次加载一次、加载其他图片等)
})

备注:由于屏幕密度关系,gif图片平移缩放后,不能沾满边缘的整个物理像素,在做像素混合效果时,会有在边缘像素形成边框效果,在web场景和源库软解码也存在相同效果,建议使用硬解码。

接口说明

GIFComponent 自定义组件

方法名 入参 接口描述
GIFComponent({ model: GIFComponent.ControllerOptions, autoPlay: boolean,resetGif:boolean}) model:GIFComponent.ControllerOptions, autoPlay:boolean,resetGif:boolean 自定义GIF渲染组件构造器

GIFComponent.ControllerOptions自定义组件配置

使用方法 入参 接口描述
getFrames() 获取所有解析图像数据帧
setFrames(images?: GIFFrame[]) images?: GIFFrame[] 设置解析图像数据帧集合
getLoopFinish() 获取循环结束最后一帧的回调方法
setLoopFinish(fn:(loopTime?)=>void) fn:(loopTime?)=>void 设置循环结束最后一帧的回调方法
getScaleType() 获取用户设置组件展示类型
setScaleType(scaletype: ScaleType) scaletype: ScaleType 设置组件展示类型
getPositionFinish() 获取帧回调方法
setPositionFinish(position: number, fn: (posTime?) => void) position: number, fn: (posTime?) => void 设置帧位置和回调方法
getSpeedFactor() 获取当前播放速率
setSpeedFactor(speed: number) speed: number 设置播放速率
getSeekTo() 获取当前播放某一静态帧
setSeekTo(gifPosition: number) gifPosition: number 设置显示GIF某一静态帧
loadBuffer(buffer: ArrayBuffer, readyRender: (err?) => void) buffer: ArrayBuffer, readyRender: (err?) => void 加载一个arraybuffer数据,展示GIF

GIFComponent.ScaleType

展示类型 展示类型描述
ScaleType.FIT_START 显示内容缩放至能全部显示,然后将其放置于自定义组件的左上方
ScaleType.FIT_END 显示内容缩放至能全部显示,然后将其放置于自定义组件的右下方
ScaleType.FIT_CENTER 显示内容缩放至能全部显示,然后将其放置于自定义组件的居中位置
ScaleType.CENTER 不进行缩放,然后将其放置于自定义组件的居中位置
ScaleType.CENTER_CROP 取(内容/自定义组件)的宽高比值的小值进行缩放,缩放至自定义组件大小,然后将其放置于自定义组件的居中位置
ScaleType.FIT_XY 将内容宽度和高度拉伸/压缩至自定义组件的大小
ScaleType.CENTER_INSIDE 当内容宽高任意一个大于自定义组件宽高表现为ScaleType.FIT_CENTER,如果内容都小于自定义组件表现为ScaleType.CENTER

GIFFrame 解析帧数据格式

export class GIFFrame {

    // 显示帧 width 宽  height 高 top上边距 left左边距
    dims: { width: number; height: number; top: number; left: number}

    // 当前帧的像素数据指向的颜色数组
    colorTable?: [number, number, number][]

    // 当前帧到下一帧的间隔时长
    delay: number

    // 当前帧绘制要求 0保留 1在上一帧绘制此帧 2恢复画布背景 3.将画布恢复到绘制当前图像之前的先前状态
    disposalType: number

    // Uint8CampedArray颜色转换后的补片信息用于绘制
    patch: Uint8ClampedArray
    
    // 由patch生成的pixelmap图像
    drawPixelMap

    // 当前帧每个像素的颜色表查找索引
    pixels?: number[]

    // 表示透明度的可选颜色索引
    transparentIndex: number
}

约束与限制

在下述版本验证通过:

  • DevEco Studio: 4.1 (4.1.3.414), SDK: API11 (4.1.0.55)
  • DevEco Studio: 4.0 (4.0.3.512), SDK: API10 (4.0.10.9)
  • DevEco Studio: 4.0 Release(4.0.3.413), SDK: API10(4.0.10.3)

目录结构

|---- ohos-gif-drawable
|     |---- entry  # 示例代码文件夹
			|----pages # 页面测试代码
				|----index.ets #测试文件列表
				|----sample1TestCasePage.ets 		#硬解码GIF范例demo
				|----sample2TestCasePage.ets 		#软解码GIF范例demo
				|----networkLoadTestCasePage.ets	#软解码测试网络文件下载
				|----loadResourceAndRawFilePage.ets #软解码测试工程文件加载
				|----loadStringAndBufferPage.ets	#软解码测试app文件路径加载
|     |---- screenshots #截图
|     |---- library  # ohos-gif-drawble库文件夹
|           |---- src  # ohos-gif-drawble gif解析渲染核心代码
				|---- display #组件展示渲染相关
				|---- parse   #gif解析相关
				|---- utils   #工具方法相关
|           |---- index.ets  # ohos-gif-drawble 对外暴露接口
|     |---- README.MD  # 安装使用方法

贡献代码

使用过程中发现任何问题都可以提 issue 给我们,当然,我们也非常欢迎你给我们发 PR

开源协议

本项目基于 Apache License 2.0 ,请自由的享受和参与开源。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

暂无描述 展开 收起
TypeScript 等 4 种语言
Apache-2.0
取消

发行版 (8)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/openharmony-sig/ohos_gif-drawable.git
git@gitee.com:openharmony-sig/ohos_gif-drawable.git
openharmony-sig
ohos_gif-drawable
ohos_gif-drawable
master

搜索帮助