CrUnitManager.kt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.cr.common
  2. import android.content.Context
  3. import android.content.res.Resources
  4. import android.graphics.Point
  5. import android.os.Build
  6. import android.util.DisplayMetrics
  7. import android.view.WindowManager
  8. import com.cr.cruav.CrActivity
  9. import com.cr.cruav.CrApplication
  10. import com.cr.cruav.R
  11. import java.math.RoundingMode
  12. import java.text.DecimalFormat
  13. import java.text.SimpleDateFormat
  14. import java.util.*
  15. /**
  16. * 操作系统:MAC系统
  17. * 创建者:王成
  18. * 创建日期:2023/4/14 11:37
  19. * 描述:单位管理类
  20. */
  21. class CrUnitManager {
  22. companion object {
  23. /**
  24. * dp转像素
  25. * @param dp Float dp值
  26. * @return Float 像素值
  27. */
  28. fun dp2px(dp: Float): Float {
  29. val metrics = Resources.getSystem().displayMetrics
  30. return dp * metrics.density
  31. }
  32. /**
  33. * dp转像素
  34. * @param dp Int dp值
  35. * @return Float 像素值
  36. */
  37. fun dp2px(dp: Int): Int {
  38. val metrics = Resources.getSystem().displayMetrics
  39. return (dp * metrics.density).toInt()
  40. }
  41. /**
  42. * 像素转dp
  43. * @param px Int 像素
  44. * @return Int dp值
  45. */
  46. fun px2dp(px: Int): Int {
  47. val metrics = Resources.getSystem().displayMetrics
  48. return (px / metrics.density + 0.5f).toInt()
  49. }
  50. /**
  51. * 获取dimens中的值
  52. * @param context Context 上下文
  53. * @param dimensId Int 资源Id
  54. * @return Float 资源值
  55. */
  56. fun getDimens(context: Context,dimensId:Int):Float{
  57. return context.resources.getDimension(dimensId)
  58. }
  59. /**
  60. * 长度格式化
  61. * @param length Double 长度值
  62. * @return String 格式化后的长度值
  63. */
  64. fun formatLength(length: Double): String {
  65. var format = DecimalFormat("#.###")
  66. format.roundingMode = RoundingMode.FLOOR
  67. return if (length < 1000) {
  68. String.format("%s米", format.format(length))
  69. } else {
  70. String.format("%s公里", format.format(length / 1000))
  71. }
  72. }
  73. /**
  74. * 面积格式化
  75. * @param area Double 面积值
  76. * @return String 格式化后的面积值
  77. */
  78. fun formatArea(area: Double): String {
  79. var format = DecimalFormat("#.###")
  80. format.roundingMode = RoundingMode.FLOOR
  81. return if (area > 1000000) {
  82. String.format("%s平方公里", format.format(area / 1000000))
  83. } else {
  84. String.format("%s平方米", format.format(area))
  85. }
  86. }
  87. /**
  88. * 查询包含的数据量
  89. * @param obj Iterable<T> 集合
  90. * @return Int 数据数量
  91. */
  92. fun <T> querySizeByIterable(obj: Iterable<T>): Int {
  93. var count: Int = 0;
  94. var it = obj.iterator()
  95. while (it.hasNext()) {
  96. it.next()
  97. count++
  98. }
  99. return count
  100. }
  101. /**
  102. * 检验经度数值是否符合要求
  103. * @param longitude String 经度值
  104. * @return Boolean 是否符合要求
  105. */
  106. fun checkLongitude(longitude: String): Boolean {
  107. var regex = Regex("""^[\-\+]?(0?\d{1,2}\.\d{1,5}|1[0-7]?\d{1}\.\d{1,5}|180\.0{1,5})$""")
  108. return regex.matches(longitude)
  109. }
  110. /**
  111. * 检查纬度值是否符合要求
  112. * @param latitude String 纬度值
  113. * @return Boolean 是否符合要求
  114. */
  115. fun checkLatitude(latitude: String): Boolean {
  116. var regex = Regex("""^[\-\+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})${'$'}""")
  117. return regex.matches(latitude)
  118. }
  119. /**
  120. * 获取系统日期
  121. * @return String 系统日期yyyyMMddHHmmss
  122. */
  123. fun toSystemDate(): String {
  124. var format = SimpleDateFormat("yyyyMMddHHmmss")
  125. return format.format(Date())
  126. }
  127. /**
  128. * 获取系统日期
  129. * @return String 系统日期 yyyy-MM-dd HH:mm:ss
  130. */
  131. fun toSystemYMDHMSDate(): String {
  132. var format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  133. return format.format(Date())
  134. }
  135. /**
  136. * 文件大小转字符串表示
  137. * @param size Int 文件大小
  138. * @return String 字符串表示
  139. */
  140. fun toFileSize(size:Int):String{
  141. return if(size<1024){
  142. "${size}K"
  143. }else if(size >=1024 && size < 1024*1024){
  144. "${String.format("%.2fM",size/1024)}"
  145. }else{
  146. ""
  147. }
  148. }
  149. /**
  150. * 像素转dp
  151. * @param px Float 像素值
  152. * @return Float
  153. */
  154. fun convertPixelsToDp(px: Float): Float {
  155. val resources = CrApplication.getContext().resources
  156. val metrics = resources.displayMetrics
  157. return px / (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
  158. }
  159. /**
  160. * 获取设备dpi信息
  161. * @return String
  162. */
  163. fun getDeviceDpi(activity:CrActivity):String {
  164. var displayMetrics = DisplayMetrics()
  165. if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R){
  166. var display = activity.display
  167. display?.getRealMetrics(displayMetrics)
  168. }else{
  169. @Suppress("DEPRECATION")
  170. var display = activity.windowManager.defaultDisplay
  171. @Suppress("DEPRECATION")
  172. display.getRealMetrics(displayMetrics)
  173. }
  174. var density:Float = displayMetrics.density
  175. var widthPixel:Int = displayMetrics.widthPixels
  176. var heightPixel:Int = displayMetrics.heightPixels
  177. // todo: 2023/7/31 计算
  178. var screenWidth = widthPixel/density
  179. var screenHeight = heightPixel/density
  180. var screenDpi = displayMetrics.densityDpi
  181. // todo: 2023/7/31 获取1dp 对应的值
  182. var oneDp:Float = getDimens(activity, R.dimen.cr_1_dp)
  183. return "像素宽度:$widthPixel 像素高度:$heightPixel 宽度:$screenWidth 高度:$screenHeight 密度Dpi:$screenDpi 密度:$density 1dp:$oneDp"
  184. }
  185. /**
  186. * 获取限定符sw
  187. * @param context Context 上下文
  188. * @return Float
  189. */
  190. fun getSmallestWidthDP(context: Context):Int{
  191. return context.resources.configuration.smallestScreenWidthDp
  192. }
  193. }
  194. }