QML中的图像圆角

Tam*_*lei 34 qml qt5 qtquick2

令我惊讶的是,该Image组件没有radius属性.我尝试通过将图像放在圆形中来模拟圆角Rectangle,但它不会剪切角落.

Rectangle {
    anchors.right: rectContentBg.left
    anchors.top: rectContentBg.top
    anchors.margins: 8

    radius: 8

    width: 64
    height: 64

    Image {
        id: imgAuthor

        opacity: 1
        smooth: false

        anchors.fill: parent

        source: "qrc:/res/sample_avatar.jpg"
    }
}
Run Code Online (Sandbox Code Playgroud)

如何正确创建圆角图像?

BaC*_*Zzo 45

由于该QtGraphicalEffects模块,Qt 5中存在内置的官方解决方案,我很惊讶地发现没有人提供这样简单的解决方案.

其他影响之一OpacityMask是为此目的而被利用的类型.这个想法是ImageRectangle一个正确设置的来掩盖源radius.这是使用分层的最简单的例子:

Image {
    id: img
    property bool rounded: true
    property bool adapt: true

    layer.enabled: rounded
    layer.effect: OpacityMask {
        maskSource: Item {
            width: img.width
            height: img.height
            Rectangle {
                anchors.centerIn: parent
                width: img.adapt ? img.width : Math.min(img.width, img.height)
                height: img.adapt ? img.height : width
                radius: Math.min(width, height)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这个最小代码为方形图像产生了很好的结果,但它也通过adapt变量考虑了非方形图像.false无论图像大小如何,通过将标志设置为生成的蒙版始终为圆形.这是可能的,因为使用外部Item填充源并允许真实掩模(内部Rectangle)的尺寸.你可以明显地摆脱外部Item,如果你只是瞄准填充源的面具,无论其纵横比.

这是一个可爱的猫图像,方形格式(),非方格式adapt: true(),最后是非方格式和adapt: false():

在此输入图像描述

这个解决方案的实现细节与另一个很好的答案中基于着色器的答案的实现细节非常相似(cfr.OpacityMask可以在这里找到QML源代码- SourceProxy只需返回一个格式良好的结果ShaderEffectSource来提供效果).

如果你不想依赖QtGraphicalEffects模块(好吧,OpacityMask.qml实际存在),你可以用着色器重新实现效果.除了已经提供的解决方案的另一种方法是使用step,smoothstepfwidth功能.这是代码:

import QtQuick 2.5

Image {
    id: image

    property bool rounded: true
    property bool adapt: true

    layer.enabled: rounded
    layer.effect: ShaderEffect {
        property real adjustX: image.adapt ? Math.max(width / height, 1) : 1
        property real adjustY: image.adapt ? Math.max(1 / (width / height), 1) : 1

        fragmentShader: "
        #ifdef GL_ES
            precision lowp float;
        #endif // GL_ES
        varying highp vec2 qt_TexCoord0;
        uniform highp float qt_Opacity;
        uniform lowp sampler2D source;
        uniform lowp float adjustX;
        uniform lowp float adjustY;

        void main(void) {
            lowp float x, y;
            x = (qt_TexCoord0.x - 0.5) * adjustX;
            y = (qt_TexCoord0.y - 0.5) * adjustY;
            float delta = adjustX != 1.0 ? fwidth(y) / 2.0 : fwidth(x) / 2.0;
            gl_FragColor = texture2D(source, qt_TexCoord0).rgba
                * step(x * x + y * y, 0.25)
                * smoothstep((x * x + y * y) , 0.25 + delta, 0.25)
                * qt_Opacity;
        }"
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

与第一种方法类似,rounded并且adapt添加属性以控制效果的视觉外观,如上所述.

  • @Phrogz效果填充源,即使我将`adapt`设置为`true`我也不能在第一个系列的第三张图片中得到结果.如果你有第一张图片中的方形图片,那么外部的"项目"就没用了. (2认同)

Tho*_*jer 7

如果您的背景是纯色或者您从未移动图像,那么制作圆角的快速方法是将您Image与另一个(或使用a BorderImage)重叠,仅绘制角落.

如果这不是一个选项,但您使用的是OpenGL,那么另一种方法是通过像素着色器将遮罩应用于图像.有关在Qt 4之上工作的插件,请参见http://blog.qt.digia.com/blog/2011/05/03/qml-shadereffectitem-on-qgraphicsview/.

最后,还可以编写一个QDeclarativeImageProvider预处理图像以使角落圆角的图像.


小智 6

这段代码可以帮到你

Rectangle {
    width: 200
    height: 200

    color: "transparent"

    //this Rectangle is needed to keep the source image's fillMode
    Rectangle {
        id: imageSource

        anchors.fill: parent
        Image {
            anchors.fill: parent
            source: "your_image_file_path"

            fillMode: Image.PreserveAspectCrop
        }
        visible: false

        layer.enabled: true
    }

    Rectangle {
        id: maskLayer
        anchors.fill: parent
        radius: parent.width / 2

        color: "red"

        border.color: "black"

        layer.enabled: true
        layer.samplerName: "maskSource"
        layer.effect: ShaderEffect {

            property var colorSource: imageSource
            fragmentShader: "
                uniform lowp sampler2D colorSource;
                uniform lowp sampler2D maskSource;
                uniform lowp float qt_Opacity;
                varying highp vec2 qt_TexCoord0;
                void main() {
                    gl_FragColor =
                        texture2D(colorSource, qt_TexCoord0)
                        * texture2D(maskSource, qt_TexCoord0).a
                        * qt_Opacity;
                }
            "
        }

    }

    // only draw border line
    Rectangle {
        anchors.fill: parent

        radius: parent.width / 2

        border.color: "black"
        border.width: 2

        color: "transparent"
    }
}
Run Code Online (Sandbox Code Playgroud)


Max*_*rno 6

虽然接受的答案和来自@fury 的答案对我来说同样有效(Qt 5.9.3),但它们在应用于光栅图像时都在角落留下了一些像差(没有那些带有 SVG 的图像)。在所有情况下对我最有效的是将 应用于OpacityMask周围的项目,例如原始帖子中的矩形。

Rectangle {
    id: root;
    anchors.right: rectContentBg.left
    anchors.top: rectContentBg.top
    anchors.margins: 8

    radius: 8

    width: 64
    height: 64

    // apply rounded corners mask
    layer.enabled: true
    layer.effect: OpacityMask {
        maskSource: Rectangle {
            x: root.x; y: root.y
            width: root.width
            height: root.height
            radius: root.radius
        }
    }

    Image {
        id: imgAuthor
        opacity: 1
        smooth: false
        anchors.fill: parent
        source: "qrc:/res/sample_avatar.jpg"
    }
}
Run Code Online (Sandbox Code Playgroud)


cha*_*lup 5

QML目前仅支持矩形剪切,但您可能需要查看qt-components项目中的DeclarativeMaskedImage:

http://qt.gitorious.org/qt-components/qt-components/blobs/master/src/symbian/sdeclarativemaskedimage.h

  • 他们真的应该做到这一点,而不是他们现在正在做的花哨的3D东西:/ (17认同)

Hel*_*t S 5

如果您有单色背景,则可以使用顶部圆角矩形的边框进行绘制.

Image{
    id:img
}
Rectangle { // rounded corners for img
    anchors.fill: img
    color: "transparent"
    border.color: "blue" // color of background
    border.width: 4
    radius: 4
}
Run Code Online (Sandbox Code Playgroud)


fur*_*ury 5

我知道我参加聚会有点晚了,但我是通过谷歌搜索到这里的,所以我想我会帮助后代:) QtGraphicalEffects OpacityMask 应该更简单地做到这一点(我在图层效果方法上遇到了问题)

Image {
    id: imgAuthor

    width: 64
    height: 64

    source: "qrc:/res/sample_avatar.jpg"

    visible: false // this is needed or the corners of the image will be visible underneath the opacity mask
}

OpacityMask {
    anchors.fill: imgAuthor
    source: imgAuthor
    maskSource: Rectangle {
        width: imgAuthor.width
        height: imgAuthor.height
        radius: 8
        visible: false // this also needs to be invisible or it will cover up the image
    }
}
Run Code Online (Sandbox Code Playgroud)