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

相关推荐
2501_9160088918 小时前
Web 前端开发常用工具推荐与团队实践分享
android·前端·ios·小程序·uni-app·iphone·webview
我科绝伦(Huanhuan Zhou)18 小时前
MySQL一键升级脚本(5.7-8.0)
android·mysql·adb
怪兽201420 小时前
Android View, SurfaceView, GLSurfaceView 的区别
android·面试
龚礼鹏20 小时前
android 图像显示框架二——流程分析
android
消失的旧时光-194320 小时前
kmp需要技能
android·设计模式·kotlin
帅得不敢出门21 小时前
Linux服务器编译android报no space left on device导致失败的定位解决
android·linux·服务器
雨白1 天前
协程间的通信管道 —— Kotlin Channel 详解
android·kotlin
天一生水water1 天前
均值回归(配对交易)策略
均值算法·回归·kotlin·量化交易
Dashing1 天前
KN:Kotlin 与 OC 交互
ios·kotlin
TimeFine1 天前
kotlin协程 容易被忽视的CompletableDeferred
android