Display 相关
/**
* @author : DevMcryYu
* @date : 2020/5/18 19:23
* @description :
*/
internal val Context.screenWidth: Int
get() = resources.displayMetrics.widthPixels
internal val Context.screenHeight: Int
get() = resources.displayMetrics.heightPixels
internal val Float.dp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this,
Resources.getSystem().displayMetrics
)
internal val Float.sp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
this,
Resources.getSystem().displayMetrics
)
internal val Float.px
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX,
this,
Resources.getSystem().displayMetrics
)
internal val Int.dp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
).toInt()
internal val Int.sp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
this.toFloat(),
Resources.getSystem().displayMetrics
).toInt()
internal val Int.px
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX,
this.toFloat(),
Resources.getSystem().displayMetrics
).toInt()
View 相关
/**
* @author : DevMcryYu
* @date : 2020/9/25 11:29
* @description :
*/
internal val View?.localVisibleRect: Rect
get() = Rect().also { this?.getLocalVisibleRect(it) }
internal val View?.globalVisibleRect: Rect
get() = Rect().also { this?.getGlobalVisibleRect(it) }
internal val View?.hitRect: Rect
get() = Rect().also { this?.getHitRect(it) }
internal val View?.isRectVisible: Boolean
get() = this != null && globalVisibleRect != localVisibleRect
internal val View?.isVisible: Boolean
get() = this != null && visibility == View.VISIBLE
internal fun View.makeVisible() {
visibility = View.VISIBLE
}
internal fun View.makeInvisible() {
visibility = View.INVISIBLE
}
internal fun View.makeGone() {
visibility = View.GONE
}
internal inline fun <T : View> T.postApply(crossinline block: T.() -> Unit) {
post { apply(block) }
}
internal inline fun <T : View> T.postDelayed(delayMillis: Long, crossinline block: T.() -> Unit) {
postDelayed({ block() }, delayMillis)
}
internal fun View.applyMargin(
start: Int? = null,
top: Int? = null,
end: Int? = null,
bottom: Int? = null
) {
if (layoutParams is ViewGroup.MarginLayoutParams) {
layoutParams = (layoutParams as ViewGroup.MarginLayoutParams).apply {
marginStart = start ?: marginStart
topMargin = top ?: topMargin
marginEnd = end ?: marginEnd
bottomMargin = bottom ?: bottomMargin
}
}
}
internal fun View.requestNewSize(
width: Int, height: Int) {
layoutParams.width = width
layoutParams.height = height
layoutParams = layoutParams
}
internal fun View.makeViewMatchParent() {
applyMargin(0, 0, 0, 0)
requestNewSize(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT)
}
internal fun View.animateAlpha(from: Float?, to: Float?, duration: Long) {
alpha = from ?: 0f
clearAnimation()
animate()
.alpha(to ?: 0f)
.setDuration(duration)
.start()
}
internal fun View.switchVisibilityWithAnimation() {
val isVisible = visibility == View.VISIBLE
val from = if (isVisible) 1.0f else 0.0f
val to = if (isVisible) 0.0f else 1.0f
ObjectAnimator.ofFloat(this, "alpha", from, to).apply {
duration = ViewConfiguration.getDoubleTapTimeout().toLong()
if (isVisible) {
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) {
makeGone()
}
})
} else {
makeVisible()
}
start()
}
}