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
}