diff --git a/app/build.gradle b/app/build.gradle index 41dbcc6..2841e3d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,9 +35,9 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' - implementation 'com.google.android.material:material:1.1.0-alpha07' + implementation 'com.google.android.material:material:1.1.0-alpha09' implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.45-androidx' - implementation 'com.afollestad.material-dialogs:core:3.0.0-rc4' + implementation 'com.afollestad.material-dialogs:core:3.1.0' implementation 'com.afollestad.material-dialogs:files:3.0.0-rc4' implementation project(path: ':ktx') } diff --git a/ktx/build.gradle b/ktx/build.gradle index 0abd81d..378281a 100644 --- a/ktx/build.gradle +++ b/ktx/build.gradle @@ -38,13 +38,13 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' - api "androidx.core:core-ktx:1.2.0-alpha02" + api "androidx.core:core-ktx:1.2.0-alpha03" api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - api 'com.afollestad.material-dialogs:core:3.0.0-rc4' - api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1" - api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha02" - api 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha02' - api 'androidx.recyclerview:recyclerview:1.1.0-beta01' + api 'com.afollestad.material-dialogs:core:3.1.0' + api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.2" + api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha03" + api 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha03' + api 'androidx.recyclerview:recyclerview:1.1.0-beta02' } repositories { diff --git a/ktx/src/main/java/luyao/util/ktx/ext/ViewExt.kt b/ktx/src/main/java/luyao/util/ktx/ext/ViewExt.kt index 929774f..aa2d52e 100644 --- a/ktx/src/main/java/luyao/util/ktx/ext/ViewExt.kt +++ b/ktx/src/main/java/luyao/util/ktx/ext/ViewExt.kt @@ -1,10 +1,12 @@ package luyao.util.ktx.ext import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.drawable.BitmapDrawable import android.view.View +import android.widget.ImageView import androidx.annotation.Px -import androidx.core.graphics.applyCanvas -import androidx.core.view.ViewCompat /** * Created by luyao @@ -37,12 +39,36 @@ inline fun View.postDelayed(delayInMillis: Long, crossinline action: () -> Unit) return runnable } -fun View.toBitmap(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Bitmap { - if (!ViewCompat.isLaidOut(this)) { - throw IllegalStateException("View needs to be laid out before calling toBitmap()") +fun View.toBitmap(scale: Float = 1f, config: Bitmap.Config = Bitmap.Config.ARGB_8888): Bitmap? { + if (this is ImageView) { + if (drawable is BitmapDrawable) return (drawable as BitmapDrawable).bitmap } - return Bitmap.createBitmap(width, height, config).applyCanvas { - translate(-scrollX.toFloat(), -scrollY.toFloat()) - draw(this) + this.clearFocus() + val bitmap = createBitmapSafely((width * scale).toInt(), (height * scale).toInt(), config, 1) + if (bitmap != null) { + Canvas().run { + setBitmap(bitmap) + save() + drawColor(Color.WHITE) + scale(scale, scale) + this@toBitmap.draw(this) + restore() + setBitmap(null) + } } + return bitmap +} + +fun createBitmapSafely(width: Int, height: Int, config: Bitmap.Config, retryCount: Int): Bitmap? { + try { + return Bitmap.createBitmap(width, height, config) + } catch (e: OutOfMemoryError) { + e.printStackTrace() + if (retryCount > 0) { + System.gc() + return createBitmapSafely(width, height, config, retryCount - 1) + } + return null + } + } \ No newline at end of file