Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/extension/BitmapFactoryExtensions.kt

17 lines
542 B
Kotlin
Raw Normal View History

2017-05-19 17:08:23 +02:00
package org.mariotaku.twidere.extension
import android.graphics.BitmapFactory
2020-06-09 00:50:51 +02:00
import kotlin.math.max
import kotlin.math.roundToInt
2017-05-19 17:08:23 +02:00
fun BitmapFactory.Options.calculateInSampleSize(preferredWidth: Int, preferredHeight: Int): Int {
if (preferredHeight > outHeight && preferredWidth > outWidth) {
return 1
}
2017-06-06 03:14:14 +02:00
if (preferredHeight <= 0 && preferredWidth <= 0) {
return 1
}
2020-06-09 00:50:51 +02:00
val result = (max(outWidth, outHeight) / max(preferredWidth, preferredHeight)
.toFloat()).roundToInt()
return max(1, result)
2017-05-19 17:08:23 +02:00
}