Qt动画开发:阴影动画,头像闪烁,窗口淡化,控件移动,曲线动画,旋转动画,连击Combo动画等

效果图

在这里插入图片描述
实现原理
QPropertyAnimation和QGraphicsEffect的综合使用

核心代码

//旋转
m_vAnimation = new QVariantAnimation(this);
if (m_vAnimation) {
	m_vAnimation->setDuration(2000);
	m_vAnimation->setStartValue(0);
	m_vAnimation->setEndValue(360);
	m_vAnimation->setLoopCount(-1);
	connect(m_vAnimation, &QVariantAnimation::valueChanged, this, &rotationLabel::sltMatrixChanged);
}

void rotationLabel::sltMatrixChanged(QVariant value)
{
	// 不断修改旋转角度,并重绘
	m_matrixNum = value.toInt();
	update();
}

void rotationLabel::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);

	// 抗锯齿
	painter.setRenderHints(QPainter::Antialiasing, true);
	// 图片平滑处理
	painter.setRenderHints(QPainter::SmoothPixmapTransform, true);
	//加载图片资源
	QPixmap disc(m_imagePath);
	auto ateX = this->width() / 2;
	auto ateY = this->height() / 2;
	//设定旋转中心点
	painter.translate(ateX, ateY);
	//旋转的角度
	painter.rotate(m_matrixNum);
	//恢复中心点
	painter.translate(-ateX, -ateY);
	//画图操作
	painter.drawPixmap(0, 0, this->width(), this->height(), disc);
}

//阴影
m_vShadowEffectAnimation = new QVariantAnimation(this);
if (m_vShadowEffectAnimation) {
	m_vShadowEffectAnimation->setDuration(2000);
	m_vShadowEffectAnimation->setKeyValueAt(0, 0);
	m_vShadowEffectAnimation->setKeyValueAt(0.5, 100);
	m_vShadowEffectAnimation->setKeyValueAt(1, 0);
	m_vShadowEffectAnimation->setLoopCount(-1);
	connect(m_vShadowEffectAnimation, &QVariantAnimation::valueChanged, this, &animationTest::sltShadowEffectRaduisChanged);
}
//设置边框阴影
m_shadowEffect = new QGraphicsDropShadowEffect(this);
if (m_shadowEffect) {
	m_shadowEffect->setColor(QColor(0, 0, 139));
	m_shadowEffect->setOffset(0, 0);
	m_shadowEffect->setBlurRadius(0);
	ui.label_shadowEffect->setGraphicsEffect(m_shadowEffect);
}
void animationTest::sltShadowEffectRaduisChanged(QVariant value)
{
	auto radius = value.toInt();
	if (m_shadowEffect) {
		m_shadowEffect->setBlurRadius(radius);
	}
}

//头像闪烁
m_opacityEffect = new QGraphicsOpacityEffect(this);
if (m_opacityEffect) {
	ui.label_shadowEffect_2->setGraphicsEffect(m_opacityEffect);
	m_opacityEffect->setOpacity(1);
}
m_vOpacityEffectAnimation = new QVariantAnimation(this);
if (m_vOpacityEffectAnimation) {
	m_vOpacityEffectAnimation->setDuration(400);
	m_vOpacityEffectAnimation->setKeyValueAt(0, 1.0);
	m_vOpacityEffectAnimation->setKeyValueAt(0.5, 0.0);
	m_vOpacityEffectAnimation->setKeyValueAt(1, 1.0);
	m_vOpacityEffectAnimation->setLoopCount(-1);
	connect(m_vOpacityEffectAnimation, &QVariantAnimation::valueChanged, this, [=](QVariant value) {
		if (m_opacityEffect) {
			m_opacityEffect->setOpacity(value.toDouble());
		}
	});
}

源码传送门

  • 22
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离歌漠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值