漂亮!用Qt Quick实现Coverflow效果

本文介绍如何使用Qt Quick和QML实现一个简单的Coverflow效果,无需复杂数学知识,仅通过布局和动画即可。作者分享了源代码,并提供了一个包含五个状态的元素设计,展示了如何通过Repeater组件生成多个Rectangle对象,并通过Behavior实现平滑的动画过渡。
摘要由CSDN通过智能技术生成

趁着放假又拾起了很久不动的Qt了,本想在假期写点什么程序,但是到现在一点想法也没有,也就算了。今天写这个Coverflow效果纯属无聊的Play,没有太多技术含量,重要的是没用到数学和几何知识。程序用了Qt 5.2 + QtQuick 2.0架构,纯qml语言,有兴趣的童鞋可以试着完善它,我已经将源代码提交到Github上了。

 

好了我们切入主题:

为了写这个效果,我非常仔细的研究了Finder中的Coverflow效果,发现它是这样的:


我认为写出Finder中的那种效果应该还需要一定的数学知识,毕竟它是实时变化的,也就是画面的布局是随窗口大小而动态调整的。于是我设计了一个一屏只有三页的Coverflow效果:


还是非常的小清新嘛- -

 

来讲一下实现原理吧。

准备工作:

首先需要创建一个Rectangle元素,在里面放一个Text元素:

Text {
                color: "#ffffff"
                font.family: "ArialBlack"
                font.pointSize: 60
 
                anchors.centerIn: parent
 
                text: index
            }


 

为了方便定位,我使用了anchors属性:

anchors.centerIn:parent

 

然后一个比较重要的属性transform我给它赋了RotationTranslate两种变换,我们一会要用到它:

transform: [
                Rotation {
                    id: rotate; angle: 0;origin.y: 125; origin.x: 125;  axis { x:0; y: 1; z: 0 }
                    Behavior on angle {
                        NumberAnimation {easing.overshoot: 1; easing.type: Easing.OutBack; duration: 600 }
                    }
                },
                Translate {
                    id: trans; x: 0
                    Behavior on x {
                        NumberAnimation {easing.overshoot: 1; easing.type: Easing.OutBack; duration: 600;  }
                 
qt 滑屏翻页效果C++程序 #ifndef PICTUREFLOW_H #define PICTUREFLOW_H #include class PictureFlowPrivate; /*! Class PictureFlow implements an image show widget with animation effect like Apple's CoverFlow (in iTunes and iPod). Images are arranged in form of slides, one main slide is shown at the center with few slides on the left and right sides of the center slide. When the next or previous slide is brought to the front, the whole slides flow to the right or the right with smooth animation effect; until the new slide is finally placed at the center. */ class PictureFlow : public QWidget { Q_OBJECT Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor) Q_PROPERTY(QSize slideSize READ slideSize WRITE setSlideSize) Q_PROPERTY(int slideCount READ slideCount) Q_PROPERTY(int centerIndex READ centerIndex WRITE setCenterIndex) public: enum ReflectionEffect { NoReflection, PlainReflection, BlurredReflection }; /*! Creates a new PictureFlow widget. */ PictureFlow(QWidget* parent = 0); /*! Destroys the widget. */ ~PictureFlow(); /*! Returns the background color. */ QColor backgroundColor() const; /*! Sets the background color. By default it is black. */ void setBackgroundColor(const QColor& c); /*! Returns the dimension of each slide (in pixels). */ QSize slideSize() const; /*! Sets the dimension of each slide (in pixels). */ void setSlideSize(QSize size); /*! Returns the total number of slides. */ int slideCount() const; /*! Returns QImage of specified slide. */ QImage slide(int index) const; /*! Returns the index of slide currently shown in the middle of the viewport. */ int centerIndex() const; /*! Returns the effect applied to the reflection. */ ReflectionEffect reflectionEffect() const; /*! Sets the effect applied to the reflection. The default is PlainReflection. */ void setReflectionEffect(ReflectionEffect effect); public slots: /*! Adds a new slide. */ void addSlide(const QImage& image); /*! Adds a new slide. */ void addSlide(const QPixmap& pixmap); /*! Sets an image for specified slide. If the slide already exists, it will be replaced. */ void setSlide(int index, const QImage& image); /*! Sets a pixmap for specified slide. If the slide already exists, it will be replaced. */ void setSlide(int index, const QPixmap& pixmap); /*! Sets slide to be shown in the middle of the viewport. No animation effect will be produced, unlike using showSlide. */ void setCenterIndex(int index); /*! Clears all slides. */ void clear(); /*! Shows previous slide using animation effect. */ void showPrevious(); /*! Shows next slide using animation effect. */ void showNext(); /*! Go to specified slide using animation effect. */ void showSlide(int index); /*! Rerender the widget. Normally this function will be automatically invoked whenever necessary, e.g. during the transition animation. */ void render(); /*! Schedules a rendering update. Unlike render(), this function does not cause immediate rendering. */ void triggerRender(); signals: void centerIndexChanged(int index); protected: void paintEvent(QPaintEvent *event); void keyPressEvent(QKeyEvent* event); void mousePressEvent(QMouseEvent* event); void resizeEvent(QResizeEvent* event); private slots: void updateAnimation(); private: PictureFlowPrivate* d; }; #endif // PICTUREFLOW_H
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值