如何处理Android帆布中的屏幕方向?
我正在与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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览了几个博客,因此答案终于在Android开发人员文档上找到了答案 - 在这里
我的代码片段看起来像:
这样,当设备旋转时,您可以处理屏幕方向。
创建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 :
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.