CrUnitManager.kt 7.4 KB

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