Record commonly used ktx

Record commonly used ktx

fun Context.px2dip(px: Float): Float = px / resources.displayMetrics.density + 0.5f
fun Context.px2dip(px: Int): Int = px2dip(px.toFloat()).toInt()

/**
 * dp转px
 */
fun Context.dip2px(dp: Float): Float = applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    dp,
    resources.displayMetrics
)

fun Context.dp2px(dp: Float): Int = applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    dp,
    resources.displayMetrics
).toInt()

fun Context.dip2px(dp: Int): Int = applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    dp.toFloat(),
    resources.displayMetrics
).toInt()


fun TextView.setColor(colorId: Int) {
    
    
    this.setTextColor(this.context.getColor(colorId))
}

fun View.setBg(drawableId: Int) {
    
    
    this.background = this.context.getDrawable(drawableId)
}

/**
 * 正常编码中一般只会用到 [dp]/[sp] ;
 * 其中[dp]/[sp] 会根据系统分辨率将输入的dp/sp值转换为对应的px
 */
val Float.dp: Float                 // [xxhdpi](360 -> 1080)
    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                 // [xxhdpi](360 -> 1080)
    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()

/**
 * sp转px
 */
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()

/*******View相关********/
fun View?.hide() {
    
    
    if (this?.visibility != View.GONE) {
    
    
        this?.visibility = View.GONE
    }
}

fun View?.show() {
    
    
    if (this?.visibility != View.VISIBLE) {
    
    
        this?.visibility = View.VISIBLE
    }
}

inline fun View.setThrottleListener(
    thresholdMillis: Long = 500L,
    crossinline block: (view: View) -> Unit
) {
    
    
    setOnClickListener {
    
    
        isClickable = false
        postDelayed({
    
     isClickable = true }, thresholdMillis)
        block.invoke(it)
    }
}

fun View?.inVisible() {
    
    
    this?.visibility = View.INVISIBLE
}

fun View?.isGone(): Boolean {
    
    
    return this?.visibility == View.GONE
}

fun hideSoftKeyboard(view: View) {
    
    
    val imm = view.context
        .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(view.windowToken, 0)
}

fun showSoftKeyboard(view: View) {
    
    
    val imm = view.context
        .getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(view, 0)
}

Supongo que te gusta

Origin blog.csdn.net/weixin_38687303/article/details/126470351
Recomendado
Clasificación