【No7.】Android 像素转换工具
来源:互联网 发布:本地账户和网络账户 编辑:程序博客网 时间:2024/06/05 14:55
import android.content.Context;import android.content.res.Resources;import com.bmob.im.demo.CustomApplcation;/** * 像素转换工具 */public class PixelUtil {/** * The context. */private static Context mContext = CustomApplcation.getInstance();/** * dp转 px. * * @param value * the value * @return the int */public static int dp2px(float value) {final float scale = mContext.getResources().getDisplayMetrics().densityDpi;return (int) (value * (scale / 160) + 0.5f);}/** * dp转 px. * * @param value * the value * @param context * the context * @return the int */public static int dp2px(float value, Context context) {final float scale = context.getResources().getDisplayMetrics().densityDpi;return (int) (value * (scale / 160) + 0.5f);}/** * px转dp. * * @param value * the value * @return the int */public static int px2dp(float value) {final float scale = mContext.getResources().getDisplayMetrics().densityDpi;return (int) ((value * 160) / scale + 0.5f);}/** * px转dp. * * @param value * the value * @param context * the context * @return the int */public static int px2dp(float value, Context context) {final float scale = context.getResources().getDisplayMetrics().densityDpi;return (int) ((value * 160) / scale + 0.5f);}/** * sp转px. * * @param value * the value * @return the int */public static int sp2px(float value) {Resources r;if (mContext == null) {r = Resources.getSystem();} else {r = mContext.getResources();}float spvalue = value * r.getDisplayMetrics().scaledDensity;return (int) (spvalue + 0.5f);}/** * sp转px. * * @param value * the value * @param context * the context * @return the int */public static int sp2px(float value, Context context) {Resources r;if (context == null) {r = Resources.getSystem();} else {r = context.getResources();}float spvalue = value * r.getDisplayMetrics().scaledDensity;return (int) (spvalue + 0.5f);}/** * px转sp. * * @param value * the value * @return the int */public static int px2sp(float value) {final float scale = mContext.getResources().getDisplayMetrics().scaledDensity;return (int) (value / scale + 0.5f);}/** * px转sp. * * @param value * the value * @param context * the context * @return the int */public static int px2sp(float value, Context context) {final float scale = context.getResources().getDisplayMetrics().scaledDensity;return (int) (value / scale + 0.5f);}}
0 0
- 【No7.】Android 像素转换工具
- PixelUtils:像素转换工具
- PX(像素)转换工具类
- android 像素转换
- Android屏幕像素转换
- Android开发之dp转像素,像素转换为dp工具类,详细代码,带有源文件下载地址。
- Android dp转换像素问题
- Android像素转换的研究
- 【Android】屏幕像素、密度工具类
- Android像素转换的研究(一)
- Android像素转换的研究(二)
- Android像素转换的研究(三)
- Android中将像素转换为sp/dp
- 像素转换
- px(像素)和dp转换工具类
- 像素万能适配转换的工具类
- NO7::CreateWindow
- No7 Java
- HTML配置no-cache(备忘)
- hello!csdn blog
- Wireshark基本介绍和学习TCP三次握手
- 排序算法分析【六】:快速排序(附Python&C++代码)
- HTML5物理游戏开发 - 越野山地自行车(一)建立地行
- 【No7.】Android 像素转换工具
- Android延时执行调用的几种方法
- 开源矩阵Eigen的下载、VS配置与使用
- 新概念C语言教程
- Eclipse编码字符集的设定
- 搭框架时Struts2的各个架包
- 在OpenStack中绕过或停用security group (iptables)
- Android TextView中设置超链接
- Java 序列化对象的一个使用案例