构造函数:创建一个新的 QPropertyAnimation
对象。
QPropertyAnimation(QObject *target, const QByteArray &propertyName);
当 QPropertyAnimation
对象创建后,你可以设置动画的持续时间、起始值、结束值等,然后启动动画。QPropertyAnimation
将在指定的时间内逐渐改变目标属性的值propertyName,产生动画效果。
// 创建动画对象,控制 QLabel 的位置动画
QPropertyAnimation *moveAnimation = new QPropertyAnimation(ui->label, "geometry");
moveAnimation->setDuration(100);
QRect startRect = ui->label->geometry();
// 设置目标位置为向下移动到 (startRect.x(), startRect.y() + 100)
QRect endRect = QRect(startRect.x(), startRect.y() + 100, startRect.width(), startRect.height()+50);
moveAnimation->setEndValue(endRect);
// 第二段动画:高度减少50,底部位置不变
QPropertyAnimation *animation2 = new QPropertyAnimation(ui->label, "geometry");
animation2->setDuration(300);
animation2->setStartValue(moveAnimation->endValue());
QRect endRect2 = moveAnimation->endValue().toRect();
endRect2.setTop(endRect.top() + 50);
// endRect2.setHeight(endRect.height() - 50);
animation2->setEndValue(endRect2);
// animation2->setEndValue(QRect(startRect.x(), startRect.y()+50, startRect.width(), startRect.height()-50));
// 创建顺序动画组,将两个动画加入组中
QSequentialAnimationGroup *seqGroup = new QSequentialAnimationGroup;
seqGroup->addAnimation(moveAnimation);
seqGroup->addAnimation(animation2);
// 开始执行动画组
seqGroup->start();
描述动画或过渡效果中可能使用的缓动函数(easing functions)。每个函数类型都有不同的数学公式,用于控制对象从一个状态过渡到另一个状态的速度和方式。
在动画和过渡中,使用正确的缓动函数可以让运动看起来更自然、更符合预期,而不是简单的线性移动。这些函数可以使对象在开始和结束时慢下来或加速,或者在中间某些时间段内有变化。下面是一些常见的缓动函数类型及其含义:
基础缓动函数
- Linear: 线性运动,匀速运动。
- Quad: 二次方缓动,包括 InQuad, OutQuad, InOutQuad, OutInQuad。
- Cubic: 三次方缓动,包括 InCubic, OutCubic, InOutCubic, OutInCubic。
- Quart: 四次方缓动,包括 InQuart, OutQuart, InOutQuart, OutInQuart。
- Quint: 五次方缓动,包括 InQuint, OutQuint, InOutQuint, OutInQuint。
其他常见缓动函数
- Sine: 正弦缓动,包括 InSine, OutSine, InOutSine, OutInSine。
- Expo: 指数缓动,包括 InExpo, OutExpo, InOutExpo, OutInExpo。
- Circ: 圆形缓动,包括 InCirc, OutCirc, InOutCirc, OutInCirc。
其他特殊缓动函数
- Elastic: 弹性缓动,包括 InElastic, OutElastic, InOutElastic, OutInElastic。
- Back: 后退缓动,包括 InBack, OutBack, InOutBack, OutInBack。
- Bounce: 弹跳缓动,包括 InBounce, OutBounce, InOutBounce, OutInBounce。
自定义缓动函数
- BezierSpline: 贝塞尔样条缓动。
- TCBSpline: TCB 样条缓动。
- Custom: 自定义缓动函数。
其他曲线类型
- InCurve, OutCurve: 输入和输出曲线。
- SineCurve, CosineCurve: 正弦和余弦曲线。
- NCurveTypes: 可能指代不同种类的曲线,具体取决于上下文。
QTransform
是 Qt 提供的一个类,用于表示二维空间中的仿射变换。仿射变换包括平移、旋转、缩放和错切等几何变换。这些变换可以通过 QTransform
类轻松地应用到图形对象上,例如图像、路径和小部件。
QTransform transform;
平移
- translate:在 x 和 y 方向上平移变换。
transform.translate(dx, dy);
旋转
-
rotate:旋转变换,单位是度。
transform.rotate(angle);
-
rotateRadians:旋转变换,单位是弧度。
transform.rotateRadians(angle);
缩放
- scale:按指定因子进行缩放。
transform.scale(sx, sy);