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);}}

【No7.】Android 像素转换工具相关推荐

  1. PX(像素)转换工具类

    PX(像素)转换工具类 最近发现有一些参数需要传入px,但是我们在android 中习惯用的是dp,所以就写了一个工具类来使用,希望 也能帮到大家.话不多说,贴代码: /*** 像素转换工具*/ pu ...

  2. PixelUtils:像素转换工具

    /** 像素转换工具*/ public class PixelUtils {/*** The context.*/private static Context mContext = CustomApp ...

  3. android 单位转换工具,Android单位转换工具类

    Android单位转换工具类 Android单位转换工具类主要由以下4部分构成 1.这个类不能被初始化 2.dip转px接口 3.sp转px接口 4.px转dip接口 代码如下: import and ...

  4. android 字体像素转换工具类_android px,dp,sp大小转换工具

    package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类 * * float s ...

  5. android 字体像素转换工具类_Android中px与dip,sp与dip等的转换工具类

    Android中px与dip,sp与dip等的转换工具类 功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方 ...

  6. android 字体像素转换工具类_Android开发之拼音转换工具类PinyinUtils示例

    本文实例讲述了Android开发之拼音转换工具类PinyinUtils.分享给大家供大家参考,具体如下: 1.首先下载pinyin4j-2.5.0.jar,拷贝到工程的lib目录里 或者点击此处本站下 ...

  7. Android 拼音转换工具PinyinUtils

    1.首先下载pinyin4j-2.5.0.jar,拷贝到工程的lib目录里 2.创建工具类:PinyinUtils /*** 拼音转换工具* * @描述 TODO* @项目名称 App_imooc* ...

  8. android 字体像素转换工具类_android工具类,转换大小写,保留小数点处理方法

    import java.text.DecimalFormat; import java.util.Scanner; /** * 金额转换 * * @author Administrator */ pu ...

  9. android 单位转换工具,Android单位转换----常用单位转换工具类

    前言 有一定开发经验的小伙伴肯定会发现这样一个问题,当我们用xml来写布局的时候,通常用的是dp.sp.(相信大家都知道为什么这样用).当我们用Java代码来创建View控件时,会发现方法接收的参数都 ...

最新文章

  1. 6、UmbracoNewsSite:添加新闻分类
  2. GenericFactoryMethod泛型工厂模式实现简单IOC功能
  3. java自动生成代码框架_DodoFramework- 一个基于代码生成引擎的Java Web系统自动化开发框架...
  4. elk的一些零碎知识
  5. 固件中启用的虚拟化否_哪些固件或硬件机制可启用强制关机?
  6. matlab排版形式是什么样子,工作型PPT,图文排版的4种常见样式
  7. [react] 解释下react中component和pureComponent两者的区别是什么?
  8. cfree运行程序错误的原因_Python入门教程 | 第 8 章 错误、调试和测试
  9. dlib+vs2013+opencv实现人脸特征点检测
  10. V$LICENSE表结构
  11. 《程序是怎样跑起来的》读书笔记
  12. [RMAN]控制文件的恢复
  13. [转载] python3.5 利用openpyxl模块来处理excel表
  14. 移动硬盘“脱机”(签名冲突)的硬盘如何正常使用
  15. golang 建立web服务器 http包源码详解
  16. linux终端命令大全(完善中)
  17. Slate轨道工具使用(一)—Track,Clip
  18. MASM32编写TcpStatC再进阶 显示PID和对应进程说明符
  19. 烽火计划项目成果-目录索引
  20. uva 11137 Ingenuous Cubrency

热门文章

  1. 垂直水平居中的方法总结和适用场景
  2. linux 系统安全以及应用
  3. TreeView的使用方法
  4. strtok 详解及实现
  5. 无线网络技术—学习AC的基础配置
  6. 消息中间件 一 之 AMQP译文(下)
  7. 回测好,为什么实盘不靠谱?
  8. 为什么很多企业要自研CRM系统,而不够买SAAS云服务提供的CRM系统或SCRM系统?...
  9. netflow generator: 开源工具nfgen的源码研读和修改(上)
  10. JBPM工作流引擎内核设计思想及构架