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

相关推荐
TDengine (老段)2 小时前
TDengine 字符串函数 CONCAT_WS 用户手册
android·大数据·数据库·时序数据库·tdengine·涛思数据
会跑的兔子3 小时前
Android 16 Kotlin协程 第一部分
android·开发语言·kotlin
Meteors.4 小时前
安卓进阶——OpenGL ES
android
椰羊sqrt5 小时前
CVE-2025-4334 深度分析:WordPress wp-registration 插件权限提升漏洞
android·开发语言·okhttp·网络安全
2501_916008895 小时前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
sun0077006 小时前
Android设备推送traceroute命令
android
来来走走6 小时前
Android开发(Kotlin) 高阶函数、内联函数
android·开发语言·kotlin
2501_915921436 小时前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
雨白7 小时前
重识 Java IO、NIO 与 OkIO
android·java
啦啦9117148 小时前
Niagara Launcher 全新Android桌面启动器!给手机换个门面!
android·智能手机