You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用图片序列实现光标动画效果?

实现GDK窗口点击时的光标动画效果

不需要借助CSS,GDK本身就能通过两种方式实现光标动画效果,以下是具体方案:

方法一:使用GDK原生动画光标支持

GDK的Gdk::Cursor支持加载标准动画光标文件(如.ani格式),如果能将你的PNG序列打包成这类动画光标资源,可以直接加载使用:

Glib::RefPtr<Gdk::Window> ref_window = window->get_window();
// 加载动画光标文件
Glib::RefPtr<Gdk::Cursor> animated_cursor = Gdk::Cursor::create(ref_window->get_display(), "path/to/your_animated_cursor.ani");
ref_window->set_cursor(animated_cursor);

方法二:手动通过定时器切换图片序列

如果要基于现有PNG序列实现动画,可通过定时器固定间隔切换光标帧,步骤如下:

  1. 提前预加载所有光标帧
// 存储所有光标帧的容器
std::vector<Glib::RefPtr<Gdk::Pixbuf>> cursor_frames;
Glib::RefPtr<Gdk::Window> ref_window = window->get_window();

// 加载序列图片(示例为4帧,根据实际数量调整)
for (int i = 1; i <= 4; ++i) {
    std::string img_path = "./push_cursor_25fps_" + std::to_string(i) + ".png";
    cursor_frames.push_back(Gdk::Pixbuf::create_from_file(img_path, 20, 20));
}
  1. 绑定窗口点击事件,启动定时器切换帧
int current_frame = 0;
sigc::connection timer_connection;

// 窗口点击回调
bool on_window_clicked(GdkEventButton* event) {
    // 25fps对应每40ms切换一帧(1000ms/25=40ms)
    timer_connection = Glib::signal_timeout().connect(
        sigc::mem_fun(*this, &YourWindowClass::update_cursor_frame), 
        40
    );
    return true;
}

// 更新光标帧的回调
bool update_cursor_frame() {
    current_frame = (current_frame + 1) % cursor_frames.size();
    Glib::RefPtr<Gdk::Cursor> cursor = Gdk::Cursor::create(
        ref_window->get_display(), 
        cursor_frames[current_frame], 
        0, 0
    );
    ref_window->set_cursor(cursor);

    // 如需动画仅播放一次,可在回到初始帧时断开定时器
    // if (current_frame == 0) {
    //     timer_connection.disconnect();
    //     return false;
    // }
    return true;
}
  1. 注意事项
  • 预加载所有帧可避免切换时的文件IO延迟,保证动画流畅;
  • 不需要动画时(如窗口失去焦点),记得调用timer_connection.disconnect()断开定时器。

内容的提问来源于stack exchange,提问作者XoDefender

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 18:39:22