Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Add dp() for more convenient #762

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions anko/library/static/commons/src/main/java/Dimensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const val MAXDPI: Int = 0xfffe
fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
fun Context.dip(value: Float): Int = (value * resources.displayMetrics.density).toInt()

fun Context.dp(value: Int): Int = dip(value)
fun Context.dp(value: Float): Int = dip(value)

//return sp dimension value in pixels
fun Context.sp(value: Int): Int = (value * resources.displayMetrics.scaledDensity).toInt()
fun Context.sp(value: Float): Int = (value * resources.displayMetrics.scaledDensity).toInt()
Expand All @@ -46,6 +49,8 @@ fun Context.sp(value: Float): Int = (value * resources.displayMetrics.scaledDens
fun Context.px2dip(px: Int): Float = px.toFloat() / resources.displayMetrics.density
fun Context.px2sp(px: Int): Float = px.toFloat() / resources.displayMetrics.scaledDensity

fun Context.px2dp(px: Int): Float = px2dip(px)

fun Context.dimen(@DimenRes resource: Int): Int = resources.getDimensionPixelSize(resource)


Expand All @@ -58,6 +63,10 @@ inline fun AnkoContext<*>.px2dip(px: Int): Float = ctx.px2dip(px)
inline fun AnkoContext<*>.px2sp(px: Int): Float = ctx.px2sp(px)
inline fun AnkoContext<*>.dimen(@DimenRes resource: Int): Int = ctx.dimen(resource)

inline fun AnkoContext<*>.dp(value: Int): Int = ctx.dp(value)
inline fun AnkoContext<*>.dp(value: Float): Int = ctx.dp(value)
inline fun AnkoContext<*>.px2dp(px: Int): Float = ctx.px2dp(px)

//the same for the views
inline fun View.dip(value: Int): Int = context.dip(value)
inline fun View.dip(value: Float): Int = context.dip(value)
Expand All @@ -67,6 +76,10 @@ inline fun View.px2dip(px: Int): Float = context.px2dip(px)
inline fun View.px2sp(px: Int): Float = context.px2sp(px)
inline fun View.dimen(@DimenRes resource: Int): Int = context.dimen(resource)

inline fun View.dp(value: Int): Int = context.dp(value)
inline fun View.dp(value: Float): Int = context.dp(value)
inline fun View.px2dp(px: Int): Float = context.px2dp(px)

//the same for Fragments
@Deprecated(message = "Use support library fragments instead. Framework fragments were deprecated in API 28.")
inline fun Fragment.dip(value: Int): Int = activity.dip(value)
Expand All @@ -82,3 +95,10 @@ inline fun Fragment.px2dip(px: Int): Float = activity.px2dip(px)
inline fun Fragment.px2sp(px: Int): Float = activity.px2sp(px)
@Deprecated(message = "Use support library fragments instead. Framework fragments were deprecated in API 28.")
inline fun Fragment.dimen(@DimenRes resource: Int): Int = activity.dimen(resource)

@Deprecated(message = "Use support library fragments instead. Framework fragments were deprecated in API 28.")
inline fun Fragment.dp(value: Int): Int = activity.dp(value)
@Deprecated(message = "Use support library fragments instead. Framework fragments were deprecated in API 28.")
inline fun Fragment.dp(value: Float): Int = activity.dp(value)
@Deprecated(message = "Use support library fragments instead. Framework fragments were deprecated in API 28.")
inline fun Fragment.px2dp(px: Int): Float = activity.px2dp(px)