Skip to content

Instantly share code, notes, and snippets.

@diefferson
Created September 10, 2019 17:25
Show Gist options
  • Select an option

  • Save diefferson/f5ef123705ceb5d2694323f45b3212dd to your computer and use it in GitHub Desktop.

Select an option

Save diefferson/f5ef123705ceb5d2694323f45b3212dd to your computer and use it in GitHub Desktop.
Animations Utils
import android.content.Context
import android.util.DisplayMetrics
fun linearMap(source: Float, sourceMin: Float, sourceMax: Float, targetMin: Float, targetMax: Float): Float {
return ((source - sourceMin) / (sourceMax - sourceMin)) * (targetMax - targetMin) + targetMin
}
fun clampedLinearPercent(percent: Float, expandAnchor: Float, collapseAnchor: Float) : Float {
return ((Math.max(percent, collapseAnchor) - collapseAnchor) / (expandAnchor - collapseAnchor)) * expandAnchor
}
fun convertDpToPixel(dp: Float, context: Context): Float {
return dp * (context.resources.displayMetrics.densityDpi.toFloat()/ DisplayMetrics.DENSITY_DEFAULT)
}
fun convertPixelsToDp(px: Float, context: Context): Float {
return px / (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment