首页 > 解决方案 > 将 ImageView 转换为具有本机分辨率的位图

问题描述

我有以下问题:我imageView的转换为 a bitmap,将位图保存到我的画廊时,我可以清楚地看到位图的分辨率/格式与 imageView 相同。但我想获得未裁剪的分辨率。

是否可以将 imageView 转换为具有本机分辨率的位图?

我想将 imageView 转换为位图,而不是通过图像源(URL)创建新的位图,因为我需要实现一个新的异步...

当前转换代码:

BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();

澄清:
网站上的图像具有分辨率800*800px,而我的 imageView 具有分辨率300*300dp(更小),我想要源的 res..这可能吗?

标签: androidbitmap

解决方案


只需使用 Glide 来加载带有自定义目标的图像。

Glide.with(context) //or view
    .asBitmap()
    .load(imagePath)
    .into(object : CustomTarget<Bitmap>(){
        override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
            // do whatever you want with the 'resource' Bitmap
        }
        override fun onLoadCleared(placeholder: Drawable?) {
            // this is called when imageView is cleared on lifecycle so you can recycle your bitmap
        }
    })

推荐阅读