【No7.】Android 像素转换工具
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 像素转换工具相关推荐
- PX(像素)转换工具类
PX(像素)转换工具类 最近发现有一些参数需要传入px,但是我们在android 中习惯用的是dp,所以就写了一个工具类来使用,希望 也能帮到大家.话不多说,贴代码: /*** 像素转换工具*/ pu ...
- PixelUtils:像素转换工具
/** 像素转换工具*/ public class PixelUtils {/*** The context.*/private static Context mContext = CustomApp ...
- android 单位转换工具,Android单位转换工具类
Android单位转换工具类 Android单位转换工具类主要由以下4部分构成 1.这个类不能被初始化 2.dip转px接口 3.sp转px接口 4.px转dip接口 代码如下: import and ...
- android 字体像素转换工具类_android px,dp,sp大小转换工具
package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类 * * float s ...
- android 字体像素转换工具类_Android中px与dip,sp与dip等的转换工具类
Android中px与dip,sp与dip等的转换工具类 功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方 ...
- android 字体像素转换工具类_Android开发之拼音转换工具类PinyinUtils示例
本文实例讲述了Android开发之拼音转换工具类PinyinUtils.分享给大家供大家参考,具体如下: 1.首先下载pinyin4j-2.5.0.jar,拷贝到工程的lib目录里 或者点击此处本站下 ...
- Android 拼音转换工具PinyinUtils
1.首先下载pinyin4j-2.5.0.jar,拷贝到工程的lib目录里 2.创建工具类:PinyinUtils /*** 拼音转换工具* * @描述 TODO* @项目名称 App_imooc* ...
- android 字体像素转换工具类_android工具类,转换大小写,保留小数点处理方法
import java.text.DecimalFormat; import java.util.Scanner; /** * 金额转换 * * @author Administrator */ pu ...
- android 单位转换工具,Android单位转换----常用单位转换工具类
前言 有一定开发经验的小伙伴肯定会发现这样一个问题,当我们用xml来写布局的时候,通常用的是dp.sp.(相信大家都知道为什么这样用).当我们用Java代码来创建View控件时,会发现方法接收的参数都 ...
最新文章
- 6、UmbracoNewsSite:添加新闻分类
- GenericFactoryMethod泛型工厂模式实现简单IOC功能
- java自动生成代码框架_DodoFramework- 一个基于代码生成引擎的Java Web系统自动化开发框架...
- elk的一些零碎知识
- 固件中启用的虚拟化否_哪些固件或硬件机制可启用强制关机?
- matlab排版形式是什么样子,工作型PPT,图文排版的4种常见样式
- [react] 解释下react中component和pureComponent两者的区别是什么?
- cfree运行程序错误的原因_Python入门教程 | 第 8 章 错误、调试和测试
- dlib+vs2013+opencv实现人脸特征点检测
- V$LICENSE表结构
- 《程序是怎样跑起来的》读书笔记
- [RMAN]控制文件的恢复
- [转载] python3.5 利用openpyxl模块来处理excel表
- 移动硬盘“脱机”(签名冲突)的硬盘如何正常使用
- golang 建立web服务器 http包源码详解
- linux终端命令大全(完善中)
- Slate轨道工具使用(一)—Track,Clip
- MASM32编写TcpStatC再进阶 显示PID和对应进程说明符
- 烽火计划项目成果-目录索引
- uva 11137	Ingenuous Cubrency