如何处理Android帆布中的屏幕方向?

发布于 2025-02-06 04:36:23 字数 1606 浏览 1 评论 0原文

我正在与Android中的Canvas合作,而我面临的问题是在屏幕旋转期间。假设我以肖像模式启动应用程序,然后在画布上绘制一些东西,然后在旋转上绘制一些画布的某些部分移出屏幕。请参阅所附的屏幕截图。

我的文件中的代码片段在哪里实现了帆布(如果需要的话,我会提供其他零件,只需通过评论让我知道):

    private lateinit var mBitmap: Bitmap
    private lateinit var mCanvas: Canvas
override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.apply {
            drawColor(0)
            drawBitmap(mBitmap, 0f, 0f, mBitmapPaint)
            drawPath(mPath, mPaint)
        }
    }
    private fun createBitmap(w: Int, h: Int) {
        val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        mBitmap = bitmap
        mCanvas = Canvas(bitmap)
        clear()
    }
 private fun createBitmap() {
        val p = displayDimensions
        val bitmapSize = max(p.x,p.y)
        createBitmap(bitmapSize, bitmapSize)
    }
init {
        mPaint = Paint()
        mPaint.isAntiAlias = true
        mPaint.isDither = true
        mPaint.color = foregroundColor
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeJoin = Paint.Join.ROUND
        mPaint.strokeCap = Paint.Cap.ROUND
        mPaint.strokeWidth = currentStrokeWidth.toFloat()
        createBitmap()
}

I am working with Canvas in Android and the issue I am facing is during the screen rotation. Let's say I start the app in portrait mode and draw something on the canvas, then on rotation some part of the canvas moves out of the screen. See the screenshots attached.

Image in Portrait Mode
Image in Landscape Mode

Code snippets from my file where Canvas is implemented (I will provide other parts if they are required just let me know through comment) :

    private lateinit var mBitmap: Bitmap
    private lateinit var mCanvas: Canvas
override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.apply {
            drawColor(0)
            drawBitmap(mBitmap, 0f, 0f, mBitmapPaint)
            drawPath(mPath, mPaint)
        }
    }
    private fun createBitmap(w: Int, h: Int) {
        val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        mBitmap = bitmap
        mCanvas = Canvas(bitmap)
        clear()
    }
 private fun createBitmap() {
        val p = displayDimensions
        val bitmapSize = max(p.x,p.y)
        createBitmap(bitmapSize, bitmapSize)
    }
init {
        mPaint = Paint()
        mPaint.isAntiAlias = true
        mPaint.isDither = true
        mPaint.color = foregroundColor
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeJoin = Paint.Join.ROUND
        mPaint.strokeCap = Paint.Cap.ROUND
        mPaint.strokeWidth = currentStrokeWidth.toFloat()
        createBitmap()
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡紫姑娘! 2025-02-13 04:36:23

浏览了几个博客,因此答案终于在Android开发人员文档上找到了答案 - 在这里

我的代码片段看起来像:

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        val scaledBitmap: Bitmap = Bitmap.createScaledBitmap(mBitmap, w, h, true)
        }
        mCanvas = Canvas(scaledBitmap)
    }

这样,当设备旋转时,您可以处理屏幕方向。 创建careScaledBitMap()功能会考虑使用先前的绘制位图并根据新的宽度和高度进行缩放。

Going through a couple of blogs and SO answers I finally found the answer on Android developers documentation - here

My code snippet looks like :

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        val scaledBitmap: Bitmap = Bitmap.createScaledBitmap(mBitmap, w, h, true)
        }
        mCanvas = Canvas(scaledBitmap)
    }

This way you can handle the screen orientation when the device rotates. The createScaledBitmap() function takes care of taking the previous drawn Bitmap and scaling it according to new width and height.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文