DensityUtil(尺寸转化+状态栏高度)
object DensityUtil {
@JvmStatic
fun dip2px(context: Context, dpValue: Float): Int {
val scale = context.resources.displayMetrics.density
return (dpValue * scale + 0.5f).toInt()
}
@JvmStatic
fun px2dip(context: Context, pxValue: Float): Int {
val scale = context.resources.displayMetrics.density
return (pxValue / scale + 0.5f).toInt()
}
@JvmStatic
fun px2sp(context: Context, pxValue: Float): Int {
val fontScale = context.resources.displayMetrics.scaledDensity
return (pxValue / fontScale + 0.5f).toInt()
}
@JvmStatic
fun sp2px(context: Context, spValue: Float): Int {
val fontScale = context.resources.displayMetrics.scaledDensity
return (spValue * fontScale + 0.5f).toInt()
}
@JvmStatic
fun getStatusBarHeightPortrait(context: Context): Double {
var statusBarHeight = 0
val resourceId = context.resources.getIdentifier("status_bar_height_portrait", "dimen", "android")
if (resourceId > 0) {
statusBarHeight = context.resources.getDimensionPixelSize(resourceId)
}
return statusBarHeight.toDouble()
}
}
SofeKeyboardStateHelper(软件盘监听工具)
class SoftKeyboardStateHelper(private val decorView: View) : OnGlobalLayoutListener {
private var isSoftKeyboardOpened = false
private var softKeyHeight = 0
private var softKeyboardStateListener: SoftKeyboardStateListener? = null
init {
decorView.findViewById<View>(R.id.content).viewTreeObserver.addOnGlobalLayoutListener(this)
}
fun getSoftKeyHeight() = softKeyHeight
fun registerSoftKeyboardStateListener(softKeyboardStateListener: SoftKeyboardStateListener?) {
this.softKeyboardStateListener = softKeyboardStateListener
}
fun unRegisterSoftKeyboardStateListener() {
decorView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
override fun onGlobalLayout() {
val mKeyboardUp: Boolean = isKeyboardShown(decorView)
if (mKeyboardUp) {
if (!isSoftKeyboardOpened) {
val r = Rect()
decorView.getWindowVisibleDisplayFrame(r)
val displayHeight = r.bottom - r.top
val parentHeight: Int = decorView.height
softKeyHeight = parentHeight - displayHeight
Log.i("软键盘监听器助手", "展开,parentHeight=${parentHeight},displayHeight = ${displayHeight},softKeyHeight=${softKeyHeight}")
isSoftKeyboardOpened = true
softKeyboardStateListener?.onSoftKeyboardOpened()
}
} else {
if (isSoftKeyboardOpened) {
Log.i("软键盘监听器助手", "关闭")
isSoftKeyboardOpened = false
softKeyboardStateListener?.onSoftKeyboardClosed()
}
}
}
private fun isKeyboardShown(rootView: View): Boolean {
val softKeyboardHeight = 100
val r = Rect()
rootView.getWindowVisibleDisplayFrame(r)
val dm = rootView.resources.displayMetrics
val heightDiff = rootView.bottom - r.bottom
return heightDiff > softKeyboardHeight * dm.density
}
interface SoftKeyboardStateListener {
fun onSoftKeyboardOpened()
fun onSoftKeyboardClosed()
}
}
GsonUtil(Gson工具类)
object GsonUtil {
private val gson by lazy {
GsonBuilder().create()
}
fun toJson(value: Any): String {
return gson.toJson(value)
}
@Throws(JsonSyntaxException::class)
fun <T> formJson(json:String,clazz: Class<T>): T {
return gson.fromJson(json,clazz)
}
@Throws(JsonSyntaxException::class)
fun <T> formJson(json:String,type: Type):T{
return gson.fromJson(json,type)
}
@Throws(JsonSyntaxException::class,IllegalStateException::class)
fun <T> fromJsonList(json: String, clazz: Class<T>): MutableList<T>{
val mList = mutableListOf<T>()
val array = JsonParser().parse(json).asJsonArray
for (elem in array) {
mList.add(gson.fromJson(elem, clazz))
}
return mList
}
@Throws(JsonSyntaxException::class,IllegalStateException::class)
fun <T> fromJsonMap(json: String, cls: Class<T>): MutableMap<String, T>{
val map = mutableMapOf<String, T>()
val jsonObj = JsonParser().parse(json).asJsonObject
val entrySet: Set<Map.Entry<String, JsonElement>> = jsonObj.entrySet()
val iter = entrySet.iterator()
while (iter.hasNext()){
val entry = iter.next()
map[entry.key] = formJson(entry.value.toString(),cls)
}
return map
}
}
GridSpacingItemDecoration(recycleView网格布局间距)