安卓工具类------>常用单位转换的辅助类

import android.content.Context;
import android.util.TypedValue;

//常用单位转换的辅助类
public class DensityUtils
{
	private DensityUtils()
	{
		/* cannot be instantiated */
		throw new UnsupportedOperationException("cannot be instantiated");
	}

	/**
	 * dp转px
	 *
	 * @param context
	 * @param val
	 * @return
	 */
	public static int dp2px(Context context, float dpVal)
	{
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				dpVal, context.getResources().getDisplayMetrics());
	}

	/**
	 * sp转px
	 *
	 * @param context
	 * @param val
	 * @return
	 */
	public static int sp2px(Context context, float spVal)
	{
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
				spVal, context.getResources().getDisplayMetrics());
	}

	/**
	 * px转dp
	 *
	 * @param context
	 * @param pxVal
	 * @return
	 */
	public static float px2dp(Context context, float pxVal)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (pxVal / scale);
	}

	/**
	 * px转sp
	 *
	 * @param fontScale
	 * @param pxVal
	 * @return
	 */
	public static float px2sp(Context context, float pxVal)
	{
		return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
	}

}

时间: 2024-08-11 22:08:49

安卓工具类------>常用单位转换的辅助类的相关文章

安卓工具类------>SD卡相关的辅助类

import java.io.File; import android.os.Environment; import android.os.StatFs; //SD卡相关的辅助类 public class SDCardUtils { private SDCardUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** *

java date 工具类 DateUtil 格式转换

package systems.baseutil.tool; /** * <p>Title: 时间和日期的工具类</p> * <p>Description: DateUtil类包含了标准的时间和日期格式,以及这些格式在字符串及日期之间转换的方法</p> * <p>Copyright: Copyright (c) 2007 advance,Inc. All Rights Reserved</p> * <p>Company:

Python脚本 - 常用单位转换

测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:常用单位的转换,这里用内存来模拟 import pstuil def bytes2human(n): symbols = ('K','M','G','T','P','E','Z','Y') prefix = {} for i,s in enumerate(symbols): prefix[s] = 1 << (i + 1) * 10 for s in reversed(symbols): if n >= pre

StringUtils工具类常用api &lt;转&gt;

该工具类是用于操作Java.lang.String类的. StringUtils类与String类的区别在于:此类是null安全的,即如果输入参数String为null,则不会抛出NullPointerException异常,代码更健壮.以函数isEmpty为例子:存在字符串stringTest, 若该字符串为空,返回1.使用String类判断方法为: if(null !=stringTest){ if(stringTest.isEmpty()){ return true; } }else{ r

安卓工具类-------&gt;Http请求的工具类

import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; //Http请

安卓工具类-------&gt;SharedPreferences

import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; public class SPUtils { public SPUtils() { /* cannot be instantiated *

工具类封装(时间转换,文档读写)

时间处理: import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; public class DateTool{ /** * 将日期(中式)转换成String(毫秒) * @param date * @param pattern * @return */ public static String dateToString(Date date,String pattern)

C11工具类:字符转换

1.数值类型和字符串转换 1.1 数值转换为字符 std::string to_string(int value); std::string to_string(long value); std::string to_string(long long value); std::string to_string(unsigned value); std::string to_string(unsigned long value); std::string to_string(unsigned lo

spring静态注入组件——工具类常用

如果直接用spring注入静态属性,则会报错,提示@Resource annotation is not supported on static fields,如果又一定要通过spring注入bean,可以采用@PostConstruct注解在某个用来初始化的方法上,注入时注入到另一个不是 静态的变量里,然后在初始化方法里面将注入好的变量赋值给静态变量,通过这些操作就给静态变量赋值. [java] view plaincopy package com.inth.base.util; import