单位转换及格式化显示
来源:互联网 发布:oracle 防火墙端口 编辑:程序博客网 时间:2024/06/15 18:02
时间格式化处理:
//01:32:55public 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());
文件大小格式化处理:
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
0 0
- 单位转换及格式化显示
- 时间单位及转换
- 格式化输出及转换
- 获取手机分辨率及单位转换
- 电子类单位及公式转换
- 常用存储单位及转换关系
- C# 类型转换及格式化
- 类型转换、验证及格式化
- 单位转换
- 单位转换
- 转换单位
- 单位转换
- 单位转换
- 单位转换
- 单位转换
- 单位转换
- 单位转换
- 单位转换
- Hibrenate 延迟加载
- HTTP的识别,认证与安全
- FZU 1341 & POJ 2536 (二分图)
- Css语法
- win10 驱动无法安装 系统找不到指定文件
- 单位转换及格式化显示
- Chapter 19:Advanced Function Topics
- 第八周项目3—指向学生类的指针(1)
- Git基础介绍
- 备份指南
- Gradle build error, Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug
- Android 开发值得订阅的日报和周刊
- HDU 1431 素数回文
- NYOJ 题目2 括号配对问题