几天来,我一直在从动态壁纸绘制到硬件加速画布上。
当壁纸启动时,它处于纵向模式,最初看起来一切正常:HW 画布:直立
当我旋转手机时,屏幕的左半部分包含图像,屏幕的右半部分为黑色: HW 画布:旋转
然后,当我旋转回原来的方向时,它变得更奇怪了。 HW 帆布:回到直立
在非加速画布上绘图可以正常工作。只需将lockHardwareCanvas
下面的代码更改为lockCanvas
. 软件画布:直立-软件画布:旋转
示例代码:
package reproduceproblem
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Rect
import android.service.wallpaper.WallpaperService
import android.util.DisplayMetrics
import android.view.SurfaceHolder
import android.view.WindowManager
class MyWallpaperService: WallpaperService() {
override fun onCreateEngine(): Engine = WallpaperEngine()
inner class WallpaperEngine : WallpaperService.Engine() {
override fun onSurfaceChanged(
holder: SurfaceHolder,
format: Int,
width: Int,
height: Int
) {
super.onSurfaceChanged(holder, format, width, height)
val bmp: Bitmap = BitmapFactory.decodeResource(applicationContext.resources, R.drawable.webb)
val rect = Rect( 0, 0, width, height )
val canvas: Canvas = holder.lockHardwareCanvas()
canvas.drawBitmap(bmp, null, rect, null)
holder.unlockCanvasAndPost(canvas)
}
}
}