• 单位转换及格式化显示



    时间格式化处理:

    //01:32:55
    	public static String generateTime(long time) {
            int totalSeconds = (int) (time / 1000);
            int seconds = totalSeconds % 60;
            int minutes = (totalSeconds / 60) % 60;
            int hours = totalSeconds / 3600;
    
            return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
        }
    	/**
    	 * 毫秒转换几分几秒
    	 * @param dur
    	 * @return
    	 */
    	private String formatDuration(long dur){
    		
    		long min=dur/1000/60;
    		long sec=dur/1000%60;
    		return ""+min+"'"+sec+""";
    	}

    dp,dx,sp转化:

    public static int getSizeInPixels(float dp, Context context) {  
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();  
            float pixels = metrics.density * dp;  
            return (int) (pixels + 0.5f);  
          }  
          
          public static float pixelsToSp(Context context, float sp) {  
            float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;  
            return sp * scaledDensity;  
          }  		
    //dip转为px:
    public static int dipToPX(final Context ctx, float dip) {
            return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, ctx.getResources().getDisplayMetrics());
        }


    代码中设置dip,sp方式

    (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    50, getResources().getDisplayMetrics());
    

     
    or

     /**
         * This method convets dp unit to equivalent device specific value in pixels.
         *
         * @param dp      A value in dp(Device independent pixels) unit. Which we need to convert into pixels
         * @param context Context to get resources and device specific display metrics
         * @return A float value to represent Pixels equivalent to dp according to device
         */
        public static float convertDpToPixel(final float dp, final Context context) {
            final Resources resources = context.getResources();
            final DisplayMetrics metrics = resources.getDisplayMetrics();
            return dp * metrics.densityDpi / 160f;
        }
    
        /**
         * This method converts device specific pixels to device independent pixels.
         *
         * @param px      A value in px (pixels) unit. Which we need to convert into db
         * @param context Context to get resources and device specific display metrics
         * @return A float value to represent db equivalent to px value
         */
        public static float convertPixelsToDp(final float px, final Context context) {
            final Resources resources = context.getResources();
            final DisplayMetrics metrics = resources.getDisplayMetrics();
            return px / (metrics.densityDpi / 160f);
        }
    


    文件大小格式化处理:

    android.text.format.Formatter类中formatFileSize(Context, long)方法,用来格式化文件大小(B → KB → MB → GB)
    android.text.format.Formatter.formatFileSize(getApplicationContext(),1024);
    //返回1.00KB
    android.text.format.Formatter.formatFileSize(getApplicationContext(),1024*1024)
    //返回1.00MB

  • 相关阅读:
    查询语句
    索引的增删改成查
    pymysql模块
    mysql备份
    单表查询语法
    单表查询
    mysql增删改差
    Leetcode--1. Two Sum(easy)
    Leetcod--20. Valid Parentheses(极简洁的括号匹配)
    Leetcode-448. Find All Numbers Disappeared in an Array(solve without extra space easy)
  • 原文地址:https://www.cnblogs.com/happyxiaoyu02/p/6150665.html
Copyright © 2020-2023  润新知