我有一个自定义的视图,在onDraw()中,我试图绘制特定的位图,这是从可绘制的资源创建的。我正在调用canvas.drawBitmap(bitmap,null, dstRect, null),它必须将这个位图绘制到指定的dstRect区域中,但它不显示任何内容。如果我调用canvas.drawRect(dstRect,paint),它会毫无问题地画出矩形,它看起来是这样的
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas.drawColor(Color.BLUE)
canvas.drawRect(dstRect,paint)
canvas.drawBitmap(bitmap, null, dstRect, null)
}

我也检查过,位图是正确创建的。源位图为385x389 PNG图像60KB
如果我删除canvas.drawRect(dstRect,paint),那么只会显示蓝屏
发布于 2019-11-13 18:34:46
请尝试以下操作
Bitmap bitmap = BitmapFactory.decodeResource(
mResources,
R.drawable.your_bitmap
);
// Initialize a new Bitmap to hold the source bitmap
Bitmap dstBitmap = Bitmap.createBitmap(
dstRect.getRight() - dstRect.getLeft, // Width
dstRect.getBottom() - dstRect.getTop(), // Height
Bitmap.Config.ARGB_8888 // Config
);
// Initialize a new Canvas instance
Canvas canvas = new Canvas(dstBitmap);
canvas.drawColor(Color.BLUE);
canvas.drawBitmap(bitmap, // Bitmap
0, // Left offset
0, // Top offset
null // Paint);https://stackoverflow.com/questions/58834905
复制相似问题