首页 > 解决方案 > Android横向模式下的渲染问题

问题描述

我使用 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 度之间的角度时,我希望背景看起来像0 ~ -60 中的这个角度。但是,结果是这个角度在 -60 ~ -90

同样,当我将角度设置在 75 度和 90 度之间时,我希望背景看起来像0 ~ 75 之间的这个角度。但是,原来这个角度在 75 ~ 90

我该如何纠正这个..?

标签: androidandroid-layoutorientationlandscaperotatetransform

解决方案


我从 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)”添加到我的程序中,它运行良好,没有渲染错误


推荐阅读