Qt 功能 序列幀動畫

2021-10-08 02:16:02 字數 3857 閱讀 4126

測試環境:win10、qt creator 4.11.1、5.14.2版本sdk

一、實際效果:

二、實現原理:

1.序列幀圖是將一系列動作放在一張圖中展示,一般每幅圖大小相等(方便程式處理)。

2.根據每一幀大小定時繪製一幅圖。

3.在paintevent使用qpainter的drawpixmap進行繪製。

三、源**

sequenceframeanimation.h

#ifndef sequenceframeanimation_h

#define sequenceframeanimation_h

#include #include #include #include class sequenceframeanimation : public qwidget

; explicit sequenceframeanimation(qwidget *parent = nullptr);

~sequenceframeanimation();

bool setsequenceframeanimation(const qstring& filename, int framewidth, int frameheight, int framerate);

bool setanimationsequence(animationsequence sequence);

bool setstartframeindex(int index = 0);

void setscaled(const qsize& size); //設定縮放大小時,長寬必須是每一幀長寬的整數倍

void setspeedupenabled(bool enabled); //動畫啟動加速過程

bool start(); //開始

void stop(); //結束

signals:

private slots:

void ontimeout();

void onspeedup();

protected:

void paintevent(qpaintevent *event) override;

private:

qpixmap m_sequenceframepixmap;

qsize m_scaledsize;

qrect m_currentframerect;

qtimer m_timer;

qtimer m_speeduptimer;

const int m_maxspeedupcount;

int m_currentspeedupcount;

int m_perframewidth;

int m_perframeheight;

int m_framerate;

int m_frameindex;

int m_row;

int m_col;

animationsequence m_animationsequence;

bool m_loop;

bool m_needspeedup;

private:

int getcurrentspeeduptime();

};#endif // sequenceframeanimation_h

sequenceframeanimation.cpp

#include "sequenceframeanimation.h"

#include #include #include #include #define speedup_interval 10

sequenceframeanimation::sequenceframeanimation(qwidget *parent) : qwidget(parent),

m_maxspeedupcount(10),

m_currentspeedupcount(1),

m_perframewidth(0),

m_perframeheight(0),

m_framerate(0),

m_frameindex(0),

m_animationsequence(begintoend),

m_loop(false),

m_needspeedup(false)

sequenceframeanimation::~sequenceframeanimation()

bool sequenceframeanimation::setsequenceframeanimation(const qstring& filename, int framewidth, int frameheight, int framerate)

return ret;

}bool sequenceframeanimation::setanimationsequence(animationsequence sequence)

return ret;

}bool sequenceframeanimation::setstartframeindex(int index)

else

update();

}return ret;

}void sequenceframeanimation::setscaled(const qsize& size)

void sequenceframeanimation::setloopenabled(bool enabled)

void sequenceframeanimation::setspeedupenabled(bool enabled)

bool sequenceframeanimation::start()

else

resize(m_perframewidth, m_perframeheight);

if(m_needspeedup)

else

update();

}return ret;

}void sequenceframeanimation::stop()

void sequenceframeanimation::ontimeout()

else

}int x = 0;

int y = 0;

if(begintoend == m_animationsequence)

else

m_currentframerect = qrect(x, y, m_perframewidth, m_perframeheight);

update();

}void sequenceframeanimation::onspeedup()

else

else

m_currentframerect = qrect(x, y, m_perframewidth, m_perframeheight);

int time = getcurrentspeeduptime();

m_speeduptimer.setinterval(time);

update();

}}void sequenceframeanimation::paintevent(qpaintevent *event)

int sequenceframeanimation::getcurrentspeeduptime()

四、使用示例

int main(int argc, char *ar**)

序列幀圖:

序列幀動畫

關於影象的例項化 1.imagenamed 系統推薦使用的,但是影象是例項化之後的釋放由系統負責,如果要自己釋放,不能使用imagenamed方法。2.如果要自己釋放,需要使用imagewithcontentsoffile nsstring path nsbundle mainbundle path...

iOS序列幀動畫

方法1 uiimage animatedimagewithimages 動畫陣列duration 持續時間 可以獲取乙個能做動畫的uiimage物件 方法2 self.imageview.animationimages array 裝的陣列 需要做動畫的陣列 self.imageview.anima...

序列幀動畫Shader

效果圖如下圖所示 原理 控制紋理uv。隨時間改變x軸 tiling,來顯示單個火焰,再改變x軸的offset來位移紋理切換其他火焰 shader upgrade note replaced mul unity matrix mvp,with unityobjecttoclippos shader c...