我有一个自定义视图,并且在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)}
我还检查了位图是否正确创建。源位图是385x389 PNG图片60 KB
如果我删除canvas.drawRect(dstRect,paint)却只显示蓝屏
尝试以下
Bitmap bitmap = BitmapFactory.decodeResource( mResources,R.drawable.your_bitmap );// Initialize a new Bitmap to hold the source bitmapBitmap dstBitmap = Bitmap.createBitmap( dstRect.getRight() - dstRect.getLeft,// Width dstRect.getBottom() - dstRect.getTop(),// Height Bitmap.Config.ARGB_8888 // Config );// Initialize a new Canvas instanceCanvas canvas = new Canvas(dstBitmap);canvas.drawColor(Color.BLUE);canvas.drawBitmap(bitmap,// Bitmap 0,// Left offset 0,// Top offset null // Paint);