QML自定义类似谷歌按钮的点击波浪按钮

  • 效果

 

  • 摘要

       一个类谷歌按钮点击的波浪效果,其实Material风格里面已经带了这个效果但是我使用的qt5.7是不支持的,所以我自己尝试做了一个这个效果,这样也算是提供一个实现的思路,也不仅限于按钮上的使用。

  •  代码

 

import QtQuick 2.0

Rectangle{
    property string btnText: "click"//文本
    id:par
    border.width: 0.5//按钮边框
    border.color:mouse.containsMouse?"lightblue":"lightgray"
    height: 30
    width: 70
    color: "transparent"
    clip: true
    radius: 4
    Text {//按钮文字
        id: btnText
        text: qsTr("设置")
        anchors.centerIn: parent
        font.pointSize: 12
        font.family:"微软雅黑"
        color: "lightgray"
    }
    MouseArea{//数据区域
        id:mouse
        anchors.fill: parent
        onPressed: {//点击
            child.startx=mouse.x//鼠标点击点作为圆心
            child.starty=mouse.y
            showAni.from=5//动态的起始大小
            showAni.to=carculate(mouse.x,mouse.y)*2//计算一下圆心与最远角的距离
            showAni.start()//启用动画
        }
        onReleased: {
            child.visible=false//隐藏动态显示对象
        }
    }
    Rectangle{
        property int startx:0
        property int starty: 0
        id:child
        opacity:0.5
        color: "lightblue"
        x:startx-height/2//根据圆心计算出该显示对象的起始坐标
        y:starty-height/2
        width: height//宽度等于高度
        radius:height/2//显示对象为一个圆
        visible: false
        NumberAnimation on height {//高度控制的动画
            id:showAni
            running: false
            duration: 400//动画周期
            easing.type:Easing.Bezier//贝塞尔曲线
            easing.bezierCurve: [ 0.40, 0.05, 0.22, 0.97, 1, 1 ]
            onStarted: {
                child.visible=true//动画开始前显示该对象
            }
        }
    }
    function carculate(x,y)//圆心距离最远角的距离计算函数(根据点击点的不同位置确认最远点,分四种情况)
    {
        var i=0
        if(x<par.width/2){
            if(y<par.height/2){
                i= Math.sqrt(Math.pow(par.width-x,2)+Math.pow(par.height-y,2))
              // console.log("00000:",i)
                return i
            }else{
                i= Math.sqrt(Math.pow(par.width-x,2)+Math.pow(y,2))
               // console.log("22222:",i)
                return i
            }
        }else{
            if(y<par.height/2){
                i=Math.sqrt(Math.pow(x,2)+Math.pow(par.height-y,2))
                console.log("111111:",i)
                return i
            }else{
                i=Math.sqrt(Math.pow(x,2)+Math.pow(y,2))
              //  console.log("33333:",i)
                return i
            }
        }
    }
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值