Android BitmapFactory.decodeResource读取原始图片装载成原始宽高Bitmap,Kotlin

Android BitmapFactory.decodeResource读取原始图片装载成原始宽高Bitmap,Kotlin

Kotlin 复制代码
    fun getOriginalBitmap(resId: Int): Bitmap {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true //只解析原始图片的宽高,不decode原始文件装载到内存的Bitmap。
        BitmapFactory.decodeResource(resources, resId, options)

        //这一阶段,最关键的是获取原始图片的真实宽高
        val srcBmpWidth = options.outWidth
        val srcBmpHeight = options.outHeight

        val d = ContextCompat.getDrawable(applicationContext, R.mipmap.p1)

        //根据原始图片的宽高创建一个空的Bitmap
        val bitmap = Bitmap.createBitmap(srcBmpWidth, srcBmpHeight, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        d?.setBounds(0, 0, srcBmpWidth, srcBmpHeight)
        d?.draw(canvas) //至此,bitmap即为原始图片。

        return bitmap
    }

Android Drawable 转化成 Bitmap-CSDN博客文章浏览阅读1.8k次。/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmaphttps://blog.csdn.net/zhangphil/article/details/43767535

相关推荐
Jomurphys29 分钟前
Kotlin - 类型别名 typealias
android·kotlin
Haha_bj38 分钟前
Flutter ——flutter_screenutil 屏幕适配
android·ios
zh_xuan1 小时前
kotlin lazy委托异常时执行流程
开发语言·kotlin
Haha_bj1 小时前
Flutter ——device_info_plus详解
android·flutter·ios
前端小伙计1 小时前
Android/Flutter 项目统一构建配置最佳实践
android·flutter
Mr_sun.3 小时前
Day09——入退管理-入住-2
android·java·开发语言
ujainu4 小时前
告别杂乱!Flutter + OpenHarmony 鸿蒙记事本的标签与分类管理(三)
android·flutter·openharmony
常利兵4 小时前
Android内存泄漏:成因剖析与高效排查实战指南
android
·云扬·4 小时前
MySQL 8.0 Redo Log 归档与禁用实战指南
android·数据库·mysql
野生技术架构师4 小时前
SQL语句性能优化分析及解决方案
android·sql·性能优化