123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package com.cr.common
- import android.content.Context
- import android.content.res.Resources
- import android.graphics.Color
- import android.graphics.Point
- import android.os.Build
- import android.util.DisplayMetrics
- import android.view.WindowManager
- import com.contrarywind.listener.OnItemSelectedListener
- import com.cr.cruav.CrActivity
- import com.cr.cruav.CrApplication
- import com.cr.cruav.R
- import com.cr.data.CrUtil
- import com.cr.view.CrViewWheel
- import dji.v5.utils.common.ContextUtil
- import java.math.RoundingMode
- import java.text.DecimalFormat
- import java.text.SimpleDateFormat
- import java.util.*
- /**
- * 操作系统:MAC系统
- * 创建者:王成
- * 创建日期:2023/4/14 11:37
- * 描述:单位管理类
- */
- class CrUnitManager {
- companion object {
- /**
- * dp转像素
- * @param dp Float dp值
- * @return Float 像素值
- */
- fun dp2px(dp: Float): Float {
- val metrics = Resources.getSystem().displayMetrics
- return dp * metrics.density
- }
- /**
- * dp转像素
- * @param dp Int dp值
- * @return Float 像素值
- */
- fun dp2px(dp: Int): Int {
- val metrics = Resources.getSystem().displayMetrics
- return (dp * metrics.density).toInt()
- }
- /**
- * 像素转dp
- * @param px Int 像素
- * @return Int dp值
- */
- fun px2dp(px: Int): Int {
- val metrics = Resources.getSystem().displayMetrics
- return (px / metrics.density + 0.5f).toInt()
- }
- /**
- * 获取dimens中的值
- * @param context Context 上下文
- * @param dimensId Int 资源Id
- * @return Float 资源值
- */
- fun getDimens(context: Context,dimensId:Int):Float{
- return context.resources.getDimension(dimensId)
- }
- /**
- * 长度格式化
- * @param length Double 长度值
- * @return String 格式化后的长度值
- */
- fun formatLength(length: Double): String {
- var format = DecimalFormat("#.###")
- format.roundingMode = RoundingMode.FLOOR
- return if (length < 1000) {
- String.format("%s米", format.format(length))
- } else {
- String.format("%s公里", format.format(length / 1000))
- }
- }
- /**
- * 面积格式化
- * @param area Double 面积值
- * @return String 格式化后的面积值
- */
- fun formatArea(area: Double): String {
- var format = DecimalFormat("#.###")
- format.roundingMode = RoundingMode.FLOOR
- return if (area > 1000000) {
- String.format("%s平方公里", format.format(area / 1000000))
- } else {
- String.format("%s平方米", format.format(area))
- }
- }
- /**
- * 查询包含的数据量
- * @param obj Iterable<T> 集合
- * @return Int 数据数量
- */
- fun <T> querySizeByIterable(obj: Iterable<T>): Int {
- var count: Int = 0;
- var it = obj.iterator()
- while (it.hasNext()) {
- it.next()
- count++
- }
- return count
- }
- /**
- * 检验经度数值是否符合要求
- * @param longitude String 经度值
- * @return Boolean 是否符合要求
- */
- fun checkLongitude(longitude: String): Boolean {
- var regex = Regex("""^[\-\+]?(0?\d{1,2}\.\d{1,5}|1[0-7]?\d{1}\.\d{1,5}|180\.0{1,5})$""")
- return regex.matches(longitude)
- }
- /**
- * 检查纬度值是否符合要求
- * @param latitude String 纬度值
- * @return Boolean 是否符合要求
- */
- fun checkLatitude(latitude: String): Boolean {
- var regex = Regex("""^[\-\+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})${'$'}""")
- return regex.matches(latitude)
- }
- /**
- * 获取系统日期
- * @return String 系统日期yyyyMMddHHmmss
- */
- fun toSystemDate(): String {
- var format = SimpleDateFormat("yyyyMMddHHmmss")
- return format.format(Date())
- }
- /**
- * 获取系统日期
- * @return String 系统日期 yyyy-MM-dd HH:mm:ss
- */
- fun toSystemYMDHMSDate(): String {
- var format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
- return format.format(Date())
- }
- /**
- * 文件大小转字符串表示
- * @param size Int 文件大小
- * @return String 字符串表示
- */
- fun toFileSize(size:Int):String{
- return if(size<1024){
- "${size}K"
- }else if(size >=1024 && size < 1024*1024){
- "${String.format("%.2fM",size/1024)}"
- }else{
- ""
- }
- }
- /**
- * 像素转dp
- * @param px Float 像素值
- * @return Float
- */
- fun convertPixelsToDp(px: Float): Float {
- val resources = CrApplication.getContext().resources
- val metrics = resources.displayMetrics
- return px / (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
- }
- /**
- * 获取设备dpi信息
- * @return String
- */
- fun getDeviceDpi(activity:CrActivity):String {
- var displayMetrics = DisplayMetrics()
- if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R){
- var display = activity.display
- display?.getRealMetrics(displayMetrics)
- }else{
- @Suppress("DEPRECATION")
- var display = activity.windowManager.defaultDisplay
- @Suppress("DEPRECATION")
- display.getRealMetrics(displayMetrics)
- }
- var density:Float = displayMetrics.density
- var widthPixel:Int = displayMetrics.widthPixels
- var heightPixel:Int = displayMetrics.heightPixels
- // todo: 2023/7/31 计算
- var screenWidth = widthPixel/density
- var screenHeight = heightPixel/density
- var screenDpi = displayMetrics.densityDpi
- // todo: 2023/7/31 获取1dp 对应的值
- var oneDp:Float = getDimens(activity, R.dimen.cr_1_dp)
- return "像素宽度:$widthPixel 像素高度:$heightPixel 宽度:$screenWidth 高度:$screenHeight 密度Dpi:$screenDpi 密度:$density 1dp:$oneDp"
- }
- /**
- * 获取限定符sw
- * @param context Context 上下文
- * @return Float
- */
- fun getSmallestWidthDP(context: Context):Int{
- return context.resources.configuration.smallestScreenWidthDp
- }
- /**
- * 设置选择器
- * @param wheel CrViewWheel 选择器
- * @param listener OnItemSelectedListener 监听
- */
- fun setWheel(wheel: CrViewWheel, listener: OnItemSelectedListener) {
- wheel.setTextColorCenter(Color.YELLOW) // TODO: 4/17/21 设置选中项颜色
- wheel.setItemsVisibleCount(5)
- wheel.setTextSize(CrUtil.getDimens(R.dimen.sp_5))
- wheel.setTypeface(CrUtil.getFont(ContextUtil.getContext()))
- wheel.setCyclic(false) // TODO: 6/9/21 禁止循环
- wheel.setOnItemSelectedListener(listener)
- wheel.setDividerColor(CrColorManager.getColor(R.color.cadetblue))
- }
- }
- }
|