【问题标题】:Canvas drawBitmap not displays bitmap画布drawBitmap不显示位图
【发布时间】:2019-11-13 10:21:23
【问题描述】:

我有一个自定义视图,在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 图片 60 KB

如果我删除canvas.drawRect(dstRect,paint),则只显示蓝屏

【问题讨论】:

    标签: android canvas bitmap


    【解决方案1】:

    试试下面的

    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);
    

    【讨论】:

    • 这对我没有帮助,因为我需要用 dstRect 区域缩放位图
    • @JemoMgebrishvili 试试我更新的答案。我已将您的 dstRect 高度宽度传递给画布,并将您的位图绘制到 dstRect
    • 但如果它是新的画布对象,它将如何绘制任何东西?据我所知,要绘制一些东西应该有一个原始画布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2013-03-15
    • 2014-03-16
    • 2014-08-16
    • 2014-12-16
    • 1970-01-01
    相关资源
    最近更新 更多