Skip to content

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帧动画的停止模式,包括停在第一帧、停在当前帧、停在最后一帧

描述:动画控制接口可以对动画进行控制,包括控制动画帧间隔、动画循环次数、动画播放与停止状态

示例代码:

cpp
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设置的透明度

描述:动画属性接口可以设置或获取动画帧的属性值,包括透明度、动画帧尺寸、动画帧数量及动画停止状态、动画帧纹理;

示例代码:

cpp
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动图文件路径

描述: 通过资源加载接口可以显示指定动图文件

示例代码:

cpp
void NAnimationImageSample()
{
    if (auto gifAct = NActorManager::GetActor("AnimationImage_1"))
    {
        if (auto gifComp = gifAct->GetComponent<NAnimationImage>())
        {
            gifComp->LoadFromFile("Assets/Textures/defaultGif.gif");
        }
    }
}