I have a custom view and inside onDraw()
I'm trying to draw specific bitmap which is created from drawable resource. I'm calling canvas.drawBitmap(bitmap,null, dstRect, null)
which must draw this bitmap into the specified dstRect
region but it doesn't display anything. If I call canvas.drawRect(dstRect,paint)
it draws rectangle with no problem and it is how it looks like
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas.drawColor(Color.BLUE)
canvas.drawRect(dstRect,paint)
canvas.drawBitmap(bitmap, null, dstRect, null)
}
I have also checked and bitmap is created correctly. Source bitmap is 385x389 PNG image 60 KB
If I remove canvas.drawRect(dstRect,paint)
than only blue screen is showing
Try the following
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.