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

相关推荐
哲科软件9 小时前
跨平台开发的抉择:Flutter vs 原生安卓(Kotlin)的优劣对比与选型建议
android·flutter·kotlin
jyan_敬言15 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
程序员老刘15 小时前
Android 16开发者全解读
android·flutter·客户端
福柯柯16 小时前
Android ContentProvider的使用
android·contenprovider
不想迷路的小男孩16 小时前
Android Studio 中Palette跟Component Tree面板消失怎么恢复正常
android·ide·android studio
餐桌上的王子16 小时前
Android 构建可管理生命周期的应用(一)
android
菠萝加点糖16 小时前
Android Camera2 + OpenGL离屏渲染示例
android·opengl·camera
用户20187928316716 小时前
🌟 童话:四大Context徽章诞生记
android
yzpyzp16 小时前
Android studio在点击运行按钮时执行过程中输出的compileDebugKotlin 这个任务是由gradle执行的吗
android·gradle·android studio
aningxiaoxixi16 小时前
安卓之service
android