HarmonyOS开发者版主1|
ArkUI (方舟UI框架)
UI
HarmonyOS 5
HarmonyOS ImageSpan会支持Apng格式吗 (API12+)
Text负责显示emoji表情和文字的混排效果,项目中的emoji表情是Apng格式,当在Text中的ImageSpan中显示时无法播放,是否有其他方法来实现播放效果,以及是否在后期会全面支持Apng格式的图片。
1
浏览41 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

在Image组件中可以使用@ohos/apng库来实现,在ImageSpan组件中可以使用ffmpeg库,通过命令行接口,将Apng图片转为gif图片,然后再展示。
代码示例如下:
收起
自动换行
深色代码主题
复制
import { fileUri } from '@kit.CoreFileKit';
import { FFmpeg, FFProgressMessageParser } from '@sj/ffmpeg';
import fs from '@ohos.file.fs';
import { resourceManager } from '@kit.LocalizationKit';
import { apng } from '@ohos/apng';
@Entry
@Component
struct ApngToGif {
@State apngFilePath: string = '';
@State apngUri: string = '';
async aboutToAppear(): Promise<void> {
let fileName = 'stack.apng'
let tempPath = getContext(this).cacheDir + '/' + fileName;
this.mediaGifFileToLocalFile($r('app.media.stack'), tempPath)
this.apngFilePath = tempPath
// 将Apng图片转为gif
this.apngUri = await this.apngToGif(this.apngFilePath)
}
// 将rawfile目录下的apng文件复制到沙箱目录
rawGifFileToLocalFile(fileName: string, localFileUri: string) {
let context = getContext(this)
const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
let buff = resourceMgr.getRawFileContentSync(fileName)
let file = fs.openSync(localFileUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, buff.buffer);
fs.closeSync(file.fd);
}
// 将media目录下的apng文件复制到沙箱目录
mediaGifFileToLocalFile(fileName: Resource, localFileUri: string) {
let context = getContext(this)
const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
let buff = resourceMgr.getMediaContentSync(fileName.id)
let file = fs.openSync(localFileUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, buff.buffer);
fs.closeSync(file.fd);
}
async apngToGif(inPutFile: string) {
const dateStr = (new Date().getTime()).toString()
// 临时文件目录
let outPutFile = getContext(this).cacheDir + `/${'testGif' + dateStr}.gif`;
let palettegenFile = getContext(this).cacheDir + '/palette.png';
let res = fs.accessSync(outPutFile);
if (res) {
fs.unlinkSync(outPutFile)
}
// 用ffmpeg,通过apng生成调色板palette
await FFmpeg.execute(['ffmpeg',
'-i', inPutFile,
'-vf', 'palettegen=reserve_transparent=1',
'-y', palettegenFile], {
logCallback: (logLevel: number, logMessage: string) => console.log(`[${logLevel}]${logMessage}`),
progressCallback: (message: string) => console.log(`[progress]${JSON.stringify(FFProgressMessageParser.parse(message))}`),
}).then(() => {
console.info('FFmpeg Execution succeeded');
}).catch((error: Error) => {
console.error(`FFmpeg Execution failed with error: ${error.message}`);
});
// 用ffmpeg,将apng转为gif
await FFmpeg.execute(['ffmpeg',
'-i', inPutFile,
'-i', palettegenFile,
'-lavfi', 'paletteuse',
'-y', outPutFile], {
logCallback: (logLevel: number, logMessage: string) => console.log(`[${logLevel}]${logMessage}`),
progressCallback: (message: string) => console.log(`[progress]${JSON.stringify(FFProgressMessageParser.parse(message))}`),
}).then(() => {
console.info('FFmpeg Execution succeeded');
}).catch((error: Error) => {
console.error(`FFmpeg Execution failed with error: ${error.message}`);
});
return fileUri.getUriFromPath(outPutFile);
}
build() {
Column() {
Row() {
Column({ space: 10 }) {
apng({
src: $r('app.media.stack'), // 图片资源
apngWidth: 100,
apngHeight: 100,
speedRate: 1 // 动画倍速
})
.width(100)
.height(100)
Text('apng三方库效果')
}
.margin({ right: 15 })
Column({ space: 10 }) {
Image(this.apngUri)
.width(100)
.height(100)
Text('ffmpeg转换效果')
}
.margin({ left: 15 })
}.justifyContent(FlexAlign.SpaceBetween)
Text() {
Span('图文混排')
.decoration({ type: TextDecorationType.None, color: Color.Red }).fontSize(25)
ImageSpan(this.apngUri)
.width(50)
.height(50)
.objectFit(ImageFit.Fill)
.verticalAlign(ImageSpanAlignment.CENTER)
Span('图文混排')
.decoration({ type: TextDecorationType.None, color: Color.Red }).fontSize(25)
}.margin({ top: 20 })
}
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
}
}
展开全部 
1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
相关推荐
换一批
1
HarmonyOS 如何判断一个IP地址为IPV6格式? (API12+)
2
HarmonyOS 如何显示RGB格式的文件?(API12+)
3
HarmonyOS 需要转换图片为非预乘格式 (API12+)
4
HarmonyOS 文字Text加阴影的效果怎么实现(API12+)
5
HarmonyOS 支持用Text代替Image实现图标加载吗?(API12+)
6
HarmonyOS JPEG 2000 格式的图片支持吗?(API12+)
7
HarmonyOS ImageSpan不支持fillColor(API12+)
8
HarmonyOS ImageSpan设置enable为false后,ImageSpan的onClick事件依然有响应 (API12+)
了解社区公约,与您携手共创和谐专业的开发者社区。




































苏公安网备 32011402010933号