Android从Drawable资源Id直接生成Bitmap,Kotlin

Android从Drawable资源Id直接生成Bitmap,Kotlin

Kotlin 复制代码
        val t1 = System.currentTimeMillis()
        val bmp = getBmpFromDrawId(this, R.mipmap.ic_launcher_round)
        Log.d("fly", "1 ${bmp?.byteCount} h=${bmp?.height} w=${bmp?.width} cost time=${System.currentTimeMillis() - t1}")

        val t2 = System.currentTimeMillis()
        val scaleBmp = Bitmap.createScaledBitmap(bmp!!, 800, 800, true)
        Log.d("fly", "2 ${scaleBmp.byteCount} h=${scaleBmp.height} w=${scaleBmp.width} cost time=${System.currentTimeMillis() - t2}")
Kotlin 复制代码
    private fun getBmpFromDrawId(context: Context, drawId: Int): Bitmap? {
        val drawable = ContextCompat.getDrawable(context, drawId)
        val bitmap = drawable?.let {
            Bitmap.createBitmap(
                it.intrinsicWidth,
                it.intrinsicHeight,
                Bitmap.Config.ARGB_8888
            )
        }

        val canvas = bitmap?.let { Canvas(it) }
        if (canvas != null) {
            drawable.setBounds(0, 0, canvas.width, canvas.height)
            drawable.draw(canvas)
        }

        return bitmap
    }

Android Drawable转BitmapDrawable再提取Bitmap,Kotlin_android drawable to bitmap-CSDN博客文章浏览阅读864次。*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android传递Bitmap的两种简单方式及其缺陷_android上传bitmap_zhangphil的博客-CSDN博客。_android drawable to bitmaphttps://blog.csdn.net/zhangphil/article/details/132351440

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

相关推荐
恋猫de小郭11 小时前
Android Studio 正式版 10 周年回顾,承载 Androider 的峥嵘十年
android·ide·android studio
aaaweiaaaaaa14 小时前
php的使用及 phpstorm环境部署
android·web安全·网络安全·php·storm
工程师老罗16 小时前
Android记事本App设计开发项目实战教程2025最新版Android Studio
android
pengyu20 小时前
系统化掌握 Dart 编程之异常处理(二):从防御到艺术的进阶之路
android·flutter·dart
消失的旧时光-194320 小时前
android Camera 的进化
android
基哥的奋斗历程1 天前
Openfga 授权模型搭建
android·adb
划水哥~1 天前
Kotlin函数式API
java·开发语言·kotlin
Pakho love1 天前
Linux:文件与fd(被打开的文件)
android·linux·c语言·c++
勿忘初心912 天前
Android车机DIY开发之软件篇(九) NXP AutomotiveOS编译
android·arm开发·经验分享·嵌入式硬件·mcu
lingllllove2 天前
PHP中配置 variables_order详解
android·开发语言·php