【问题标题】:How to do an opacity gradient in an image?如何在图像中进行不透明度渐变?
【发布时间】:2019-01-21 15:34:33
【问题描述】:

我已经实现了一个类似于 Itunes 的封面流程。但是,即使在研究和阅读文档之后,我仍然无法解决一件事...... 我有以下结果,但我只想显示一半的反射。

我发现了以下 StackOverflow 帖子:How do I make a gradient opacity in an image - QML,这与我想要实现的非常相似。除了我的背景是渐变色之外,我无法选择白色(或作为解决方案提供的解决方案代码中最后一个 GradientStop 元素的任何纯色)。

到目前为止,这是我的代码:

Image {
    id: rectDelegate
    anchors.fill: parent
    source: images[index % images.length]
}
ShaderEffectSource {
    id: reflection
    sourceItem: rectDelegate

    y: sourceItem.height
    width: sourceItem.width
    height: sourceItem.height

    transform: [
        Rotation {
            origin.x: reflection.width / 2
            origin.y: reflection.height / 2
            axis.x: 1
            axis.y: 0
            axis.z: 0
            angle: 180
        }
    ]
    visible: reflection_visible
}


Rectangle {
    anchors.fill: reflection

    gradient: Gradient {
        GradientStop {
            position: 0.0
            color: "#55ffffff"
        }
        GradientStop {
            // This determines the point at which the reflection fades out.
            position: 1
            color: "#ffffff"
        }
    }
    visible: reflection_visible
}

我尝试将 ShaderEffectSource 元素嵌入到高度为反射高度一半的透明矩形内,并将 ShaderEffectSource 剪辑在其中,但不会出现反射:/

代码如下:

    Rectangle {
    anchors.top: rectDelegate.bottom
    color: "transparent"
    width: rectDelegate.width
    height: rectDelegate.height - 300
    visible: reflection_visible
    clip: true
    border.color: "yellow"
    ShaderEffectSource {
        id: reflection
        sourceItem: rectDelegate

        y: sourceItem.height
        width: sourceItem.width
        height: sourceItem.height

        transform: [
            Rotation {
                origin.x: reflection.width / 2
                origin.y: reflection.height / 2
                axis.x: 1
                axis.y: 0
                axis.z: 0
                angle: 180
            }
        ]
        visible: reflection_visible
    }
}

欢迎任何想法 :) 我试图使我的帖子尽可能清晰,但如果有什么需要理解的地方,请询问:)

【问题讨论】:

  • 理解这些东西我有点分心,你可以放置输入图像和另一个你想要的输出图像。 :-)

标签: qt qml qtquick2


【解决方案1】:

阅读这篇文章: How do I make a gradient opacity in an image? - QML 或者你可以使用 Canvas 类型来绘制透明渐变的图像,但正确的方法是第一种。

【讨论】:

  • 我已经按照我的帖子中提到的那样检查了这篇帖子,但我仍然坚持这样一个事实,即为了让反射淡入我的背景,我需要为我的渐变获得“第二种”颜色。问题是颜色“透明”并没有给出好的结果,任何其他颜色都不对:/
猜你喜欢
  • 2016-10-28
  • 2018-07-06
  • 2011-02-07
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
相关资源
最近更新 更多