Skip to content

Commit

Permalink
Experiment: Added ranges for bounds hash code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamim Attafi authored and Tamim Attafi committed Feb 20, 2021
1 parent 2a64749 commit 6e9fd8f
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable
import soup.neumorphism.CornerFamily
import soup.neumorphism.NeumorphShapeDrawable
import soup.neumorphism.ShapeType
import soup.neumorphism.internal.shape.ShapeFactory.Parameters.SIZE_CACHING_STEP
import soup.neumorphism.internal.util.BitmapUtils.clipToRadius
import soup.neumorphism.internal.util.BitmapUtils.toBitmap
import java.lang.ref.SoftReference
Expand Down Expand Up @@ -41,7 +42,7 @@ internal object ShapeFactory {
bounds: Rect
): Shape {
var hashCode = drawableState.hashCode()
hashCode = 31 * hashCode + bounds.hashCode()
hashCode = 31 * hashCode + bounds.calculateHashCode(SIZE_CACHING_STEP)

return reusable_shapes[hashCode]?.get() ?: createNewShape(drawableState, bounds).also { newShape ->
reusable_shapes[hashCode] = SoftReference(newShape)
Expand Down Expand Up @@ -70,7 +71,7 @@ internal object ShapeFactory {
cornerRadius: Float,
drawable: Drawable
): Bitmap {
var hashCode = rect.hashCode()
var hashCode = rect.calculateHashCode(SIZE_CACHING_STEP)
hashCode = 31 * hashCode + cornerFamily
hashCode = 31 * hashCode + cornerRadius.hashCode()
hashCode = 31 * hashCode + drawable.hashCode()
Expand All @@ -80,4 +81,24 @@ internal object ShapeFactory {
}
}

private fun Rect.calculateHashCode(sizeStep: Int): Int {
var result = left / sizeStep
result = 31 * result + top / sizeStep
result = 31 * result + right / sizeStep
result = 31 * result + bottom / sizeStep
return result
}

private fun RectF.calculateHashCode(sizeStep: Int): Int {
var result = if (left != 0.0f) java.lang.Float.floatToIntBits(left) / sizeStep else 0
result = 31 * result + if (top != 0.0f) java.lang.Float.floatToIntBits(top) / sizeStep else 0
result = 31 * result + if (right != 0.0f) java.lang.Float.floatToIntBits(right) / sizeStep else 0
result = 31 * result + if (bottom != 0.0f) java.lang.Float.floatToIntBits(bottom) / sizeStep else 0
return result
}

object Parameters {
const val SIZE_CACHING_STEP = 100
}

}

0 comments on commit 6e9fd8f

Please sign in to comment.