Skip to content

Commit

Permalink
Improve drawableToBitmap method with BitmapDrawable
Browse files Browse the repository at this point in the history
  • Loading branch information
lopspower committed Dec 6, 2019
1 parent 426de32 commit 8fd3c4e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apply from: 'dependencies.gradle'

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
Expand All @@ -18,4 +18,4 @@ buildscript {

task clean(type: Delete) {
delete rootProject.buildDir
}
}
2 changes: 1 addition & 1 deletion circularimageview-example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Base.Theme"
tools:ignore="GoogleAppIndexingWarning">

<activity
android:name=".MainActivity"
android:label="@string/app_name">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mikhaellopez.circularimageview

import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.AttributeSet
Expand Down Expand Up @@ -419,20 +420,24 @@ class CircularImageView @JvmOverloads constructor(

private fun drawableToBitmap(drawable: Drawable?): Bitmap? =
drawable?.let {
try {
// Create Bitmap object out of the drawable
val bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
bitmap
} catch (e: Exception) {
e.printStackTrace()
null
when (drawable) {
is BitmapDrawable -> drawable.bitmap
.let { Bitmap.createScaledBitmap(it, drawable.intrinsicWidth, drawable.intrinsicHeight, false) }
else -> try {
// Create Bitmap object out of the drawable
val bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
bitmap
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
//endregion
Expand Down
4 changes: 2 additions & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ allprojects {

ext {
// APP VERSION
androidVersionCode = 16
androidVersionName = "4.1.0"
androidVersionCode = 17
androidVersionName = "4.1.1"

// ANDROID VERSION
androidCompileSdkVersion = 29
Expand Down

0 comments on commit 8fd3c4e

Please sign in to comment.