**
* 正常编码中一般只会用到 [dp]/[sp] ;
* 其中[dp]/[sp] 会根据系统分辨率将输入的dp/sp值转换为对应的px
*/
val Float.dp: Float
get() = android.util.TypedValue.applyDimension(
android.util.TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics
)
val Int.dp: Int
get() = android.util.TypedValue.applyDimension(
android.util.TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
).toInt()
val Float.sp: Float
get() = android.util.TypedValue.applyDimension(
android.util.TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics
)
val Int.sp: Int
get() = android.util.TypedValue.applyDimension(
android.util.TypedValue.COMPLEX_UNIT_SP,
this.toFloat(),
Resources.getSystem().displayMetrics
).toInt()
fun Context.sp2px(sp: Float): Float = applyDimension(
TypedValue.COMPLEX_UNIT_SP,
sp,
resources.displayMetrics
)
fun Context.sp2px(sp: Int): Int = applyDimension(
TypedValue.COMPLEX_UNIT_SP,
sp.toFloat(),
resources.displayMetrics
).toInt()
fun View?.hide() {
if (this?.visibility != View.GONE) {
this?.visibility = View.GONE
}
}
fun View?.show() {
if (this?.visibility != View.VISIBLE) {
this?.visibility = View.VISIBLE
}
}
fun View?.inVisible() {
this?.visibility = View.INVISIBLE
}
fun View?.isGone(): Boolean {
return this?.visibility == View.GONE
}
|