Android上的QML Canvas,定位问题

我是QML的新手(在Android上),请原谅我,如果这是一件微不足道的事情.我正在尝试使用Canvas对象来绘制一些自定义图形等.但是我不能让它在改变屏幕方向时表现得很好.如果我以纵向方向启动我的应用程序,它看起来不错,直到我切换到横向.如果我开始它的风景也一样.一切都好,直到我去画像.我该如何解决这个问题?这是一个简单的测试,我遇到了麻烦:

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
    visible: true
    visibility: Window.FullScreen

    Rectangle {
        anchors.fill: parent
        color: "red"

        Canvas {
            anchors.fill: parent

            onPaint: {
                var ctx = getContext("2d")

                ctx.fillStyle = "blue"
                ctx.fillRect(0, 0, width, height)

            }
        }
    }
}

这是发生了什么.如果我启动上面的应用程序肖像,它会在屏幕上填充红色背景矩形和顶部的蓝色画布(我实际上可以看到它绘制两者).当我去景观时,这就是我最终的结果.

画布不再填满整个屏幕.它似乎现在是一个完美的正方形,并且右侧剪掉(它没有拉伸.如果有更多内容,你会看到这个).如果我开始横向和去画像,会发生类似的事情:

解决方法:

回答我自己的问题作为可能的解决方案之一.看起来我的原始代码中的“anchors.fill:parent”行默认情况下不会更新相应级别中的width和height属性.以下代码似乎摆脱了这个问题:

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
    id: root
    width: Screen.width
    height: Screen.height
    visible: true
    visibility: Window.FullScreen


    Rectangle {
        anchors.fill: parent
        width: parent.width
        height: parent.height
        color: "red"

        Canvas {
            anchors.fill: parent
            width: parent.width
            height: parent.height

            onPaint: {
                var ctx = getContext("2d")

                ctx.fillStyle = "blue"
                ctx.fillRect(0, 0, width, height)

            }
        }
    }
}
上一篇:在android中给出方位角,俯仰和滚动的相对方向?


下一篇:android 横竖屏旋转Activity生命周期变化(面试)