在Android上以横向模式渲染问题

问题描述 投票:-1回答:1

我使用View.setRotationY()根据设备的方向旋转我的视图。在肖像画中,一切都很好。但是当设备旋转时,布局的背景会变形。

码:

if(under == RecyclerView.NO_POSITION){
        aboveBinding.vContainerLeft.rotationY = 0F
        aboveBinding.vContainerRight.rotationY = 0F
    }else if(under == above - 1){
        val half = width / 2
        val degrees = 90 * ((width - offset).toFloat() / half)
        aboveBinding.vContainerLeft.pivotX = aboveBinding.vContainerLeft.width.toFloat()
        aboveBinding.vContainerLeft.rotationY = degrees
        aboveBinding.vContainerRight.rotationY = 0F
    }else{
        val half = width / 2
        val degrees = -90 * (offset.toFloat() / half)
        aboveBinding.vContainerRight.pivotX = 0F
        aboveBinding.vContainerLeft.rotationY = 0F
        aboveBinding.vContainerRight.rotationY = degrees
    }

当我使用View.setRotationY()方法设置-60度和-90度之间的角度时,我希望背景看起来像thisangle in 0 ~ -60。但是,事实证明这就像这个angle in -60 ~ -90

同样,当我将角度设置在75度和90度之间时,我希望背景看起来像这个angle in 0 ~ 75。但是,事实证明这就像这个angle in 75 ~ 90

我该如何纠正这个?

android android-layout orientation landscape rotatetransform
1个回答
0
投票

我从GitHub - FlipViewPager得到了一个想法。它使用“Camera”+“Matrix”代替“setRotationY()”,如:

canvas.save()
    mCamera.save()
    mCamera.rotateY(degreeRange * degRadio)
    mCamera.getMatrix(mMatrix)
    mMatrix.preScale(0.25F, 0.25F)
    mMatrix.postScale(4F, 4F)

    mMatrix.preTranslate(preDx, preDy)
    mMatrix.postTranslate(-preDx, -preDy)

    canvas.concat(mMatrix)

我将代码“mMatrix.preScale(0.25F,0.25F)mMatrix.postScale(4F,4F)”添加到我的程序中,它运行良好,没有渲染错误

© www.soinside.com 2019 - 2024. All rights reserved.