Android Canvas:绘制过大的位图

新手上路,请多包涵

我正在运行 Ubuntu 16.04。在 Android Studio 上,当我尝试在模拟器中运行我的应用程序时,我收到以下错误:

致命异常:主进程: _此处的项目名称_,PID:2528 java.lang.RuntimeException:画布:试图绘制太大(216090000字节)位图。在 android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260) 在 android.graphics.Canvas.drawBitmap(Canvas.java:1415) 在 android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:528) 在 android。 widget.ImageView.onDraw(ImageView.java:1316) 在 android.view.View.draw(View.java:17185) 在 android.view.View.updateDisplayListIfDirty(View.java:16167) 在 android.view.View.draw (View.java:16951) 在 android.view.View.ViewGroup.drawChild(ViewGroup.java:3727) 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) 在 android.view.View.updateDisplayListIfDirty(View.java: 16162) 在 android.view.View.draw(View.java:16951) 在 android.view.ViewGroup.drawChild(ViewGroup.java:3727) 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) 在

ETC…

我确实必须通过一些箍来让我的模拟器工作但是,需要创建一个符号链接,以便我可以在 AMD 上运行模拟器。不确定这是否是问题的一部分。对于我的一生,我无法弄清楚为什么它会继续这样做。在我的小组中,还有其他人可以在相同的模拟手机和 SDK 上很好地模拟该项目。

原文由 Francisco 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 934
2 个回答

将(高分辨率) drawable 中的图像移动到 drawable-xxhdpi 。但是在应用程序开发中,您不需要使用大图像。它会增加您的 APK 文件大小。

原文由 Md Imran Choudhury 发布,翻译遵循 CC BY-SA 4.0 许可协议

Picasso 的解决方案是添加转换以调整图像大小。

 class ResizeTransformation(private val maxSize: Int) : Transformation {
    override fun transform(source: Bitmap?): Bitmap? {
        var result:Bitmap? = null

        if (source != null) {
            var width = source.width
            var height = source.height

            val bitmapRatio = width.toFloat() / height.toFloat()

            if (bitmapRatio > 1) {
                width = maxSize;
                height = (width / bitmapRatio).toInt()
            } else {
                height = maxSize;
                width = (height * bitmapRatio).toInt()
            }

            result = Bitmap.createScaledBitmap(source, width, height, true)
            source.recycle()

        }

        return result
    }

    override fun key() = "resize()"
}

利用:

 Picasso.get()
        .load(url)
        .transform(ResizeTransformation(2400)) //FHD+ resolution
        .into(view)

原文由 Вадим Гарань 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题