Skip to content

Commit

Permalink
Merge pull request #35 from fornewid/issues/#31_blur_density
Browse files Browse the repository at this point in the history
Library: Use different blur radius as device density (#31)
  • Loading branch information
fornewid authored Aug 13, 2020
2 parents aa97598 + bb30724 commit b92b49b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import android.graphics.Color
internal data class BlurFactor(
val width: Int,
val height: Int,
val radius: Int = DEFAULT_RADIUS,
val radius: Int = MAX_RADIUS,
val sampling: Int = DEFAULT_SAMPLING,
val color: Int = Color.TRANSPARENT
) {
companion object {
const val DEFAULT_RADIUS = 25
const val MAX_RADIUS = 25
const val DEFAULT_SAMPLING = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@ import android.graphics.Bitmap
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.os.Build
import android.renderscript.*
import android.util.DisplayMetrics
import soup.neumorphism.internal.util.onCanvas
import java.lang.ref.WeakReference
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt

internal class BlurProvider(context: Context) {

private val contextRef = WeakReference(context)
private val defaultBlurRadius: Int

init {
val densityStable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
DisplayMetrics.DENSITY_DEVICE_STABLE / DisplayMetrics.DENSITY_DEFAULT.toFloat()
} else {
context.resources.displayMetrics.density
}
defaultBlurRadius = min(BlurFactor.MAX_RADIUS, (densityStable * 10).roundToInt())
}

fun blur(
source: Bitmap,
radius: Int = BlurFactor.DEFAULT_RADIUS,
radius: Int = defaultBlurRadius,
sampling: Int = BlurFactor.DEFAULT_SAMPLING
): Bitmap? {
val factor = BlurFactor(
Expand Down

0 comments on commit b92b49b

Please sign in to comment.