NAnimationImage
NAnimationImage组件时UI系统中的基础动图组件,用于显示gif、webp格式的动图,支持动图属性获取与设置、动图动画的播放与暂停等操作。
| 接口 | 说明 |
|---|---|
| SetIntervalMs | 设置动画帧间隔时间,单位毫秒 |
| GetIntervalMs | 获取动画帧间隔时间,单位毫秒 |
| GetOrignalIntervalMs | 获取原始动画帧间隔时间,单位毫秒 |
| SetLoopCount | 设置循环次数 |
| GetLoopCount | 获取当前设置的循环次数 |
| GetLastLoopTimes | 获取上次循环的次数 |
| GetFrameCount | 获取动画帧数 |
| SetIsStopAtLastFrame | 设置动画是否在最后一帧停止播放 |
| GetIsStopAtLastFrame | 获取动画是否在最后一帧停止播放 |
| Stop | 停止动画播放 |
| Play | 开始播放动画 |
| LoadFromMemoryData | 从内存数据中加载动图 |
| LoadFromFile | 从文件加载动图 |
| GetFrameWidth | 获取动画帧的宽度 |
| GetFrameHeight | 获取动画帧的高度 |
| GetFrameTexture | 获取动画纹理 |
| SetAlpha | 设置透明度 |
| GetAlpha | 获取透明度 |
动画控制接口
void SetIntervalMs(int intervalMs);
int GetIntervalMs();
int GetOrignalIntervalMs();
void SetLoopCount(int count);
int GetLoopCount();
int GetLastLoopTimes();
void Stop(EAnimImageFrame stopMode);
void Stop();
void Play();
参数:
| 参数 | 说明 |
|---|---|
| intervalMs | 帧间隔时间,单位毫秒 |
| count | 帧动画循环次数 |
| stopMode | 帧动画的停止模式,包括停在第一帧、停在当前帧、停在最后一帧 |
描述:动画控制接口可以对动画进行控制,包括控制动画帧间隔、动画循环次数、动画播放与停止状态
示例代码:
void NAnimationImageSample()
{
if (auto gifAct = NActorManager::GetActor("AnimationImage_1"))
{
if (auto gifComp = gifAct->GetComponent<NAnimationImage>())
{
gifComp->SetIntervalMs(100);
auto curIntervalMs = gifComp->GetIntervalMs();
auto originalIntervalMs = gifComp->GetOrignalIntervalMs();
gifComp->SetLoopCount(10);
auto curLoopCount = gifComp->GetLoopCount();
gifComp->Stop(EAnimImageFrame::AnimImageFrameLast);
}
}
}动画属性接口
size_t GetFrameCount();
void SetIsStopAtLastFrame(bool b);
bool GetIsStopAtLastFrame();
uint32 GetFrameWidth();
uint32 GetFrameHeight();
NTexturePtr GetFrameTexture();
void SetAlpha(float alpha);
float GetAlpha();
参数:
| 参数 | 说明 |
|---|---|
| b | 是否停止在最后一帧 |
| alpha | 设置的透明度 |
描述:动画属性接口可以设置或获取动画帧的属性值,包括透明度、动画帧尺寸、动画帧数量及动画停止状态、动画帧纹理;
示例代码:
void NewScript1::NAnimationImageSample()
{
if (auto gifAct = NActorManager::GetActor("AnimationImage_1"))
{
if (auto gifComp = gifAct->GetComponent<NAnimationImage>())
{
auto frameCount = gifComp->GetFrameCount();
auto width = gifComp->GetFrameWidth();
auto height = gifComp->GetFrameHeight();
auto tex = gifComp->GetFrameTexture();
gifComp->SetIsStopAtLastFrame(true);
bool flag = gifComp->GetIsStopAtLastFrame();
gifComp->SetAlpha(0.5f);
auto alpha = gifComp->GetAlpha();
}
}
}资源加载接口
void LoadFromMemoryData(EAnimateImageType type, NResourceMemoryDataPtr res, RESOURCE_CALLBACK callback = nullptr, ImageLoadConfig config = {});
void LoadFromFile(const std::string filePath, RESOURCE_CALLBACK callback = nullptr, ImageLoadConfig config = {});
参数:
| 参数 | 说明 |
|---|---|
| type | 动图类型,支持gif和webp |
| res | 存储动图数据的指针 |
| callback | 回调函数 |
| config | 动图加载配置 |
| filePath | 动图文件路径 |
描述: 通过资源加载接口可以显示指定动图文件
示例代码:
void NAnimationImageSample()
{
if (auto gifAct = NActorManager::GetActor("AnimationImage_1"))
{
if (auto gifComp = gifAct->GetComponent<NAnimationImage>())
{
gifComp->LoadFromFile("Assets/Textures/defaultGif.gif");
}
}
}
