使用系统方法

        int px1 = getResources().getDimensionPixelSize(R.dimen.dp_10);
int px2 = getResources().getDimensionPixelOffset(R.dimen.dp_20);
float dp = getResources().getDimension(R.dimen.dp_10);

自己实现工具类

public class UnitConversionUtil {
/**
* 根据手机分辨率从DP转成PX
* @param context
* @param dpValue
* @return
*/
public static int dip2px(Context context, float dpValue) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
} /**
* 将sp值转换为px值,保证文字大小不变
* @param spValue
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
} /**
* 根据手机的分辨率PX(像素)转成DP
* @param context
* @param pxValue
* @return
*/
public static int px2dip(Context context, float pxValue) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
} /**
* 将px值转换为sp值,保证文字大小不变
* @param pxValue
* @return
*/ public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
}

Android 开发 记录一个DP、PX、SP转换工具类的更多相关文章

  1. Android中常用单位dp,px,sp之间的相互转换

    MainActivity如下: package cc.testunitswitch; import android.os.Bundle; import android.util.DisplayMetr ...

  2. Android度量单位说明(DIP,DP,PX,SP) (转帖)

    (一)概念 dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不 ...

  3. Android度量单位说明(DIP,DP,PX,SP)

    本文转载于:http://blog.sina.com.cn/s/blog_6b26569e0100xw6d.html (一)概念 dip: device independent pixels(设备独立 ...

  4. android开发时间和日期的代码实现工具类(一)

    android开发时间和日期工具类的代码实现: package com.gzcivil.utils; import android.annotation.SuppressLint; import an ...

  5. Android开发之使用Handler封装下载图片工具类(源码分享)

    假设每下载一张图片,就得重写一次Http协议,多线程的启动和handler的信息传递就显得太麻烦了,我们直接来封装一个工具类,便于我们以后在开发时随时能够调用. (1)在清单文件加入权限 <us ...

  6. Android开发学习之路-Palette颜色提取工具类使用

    视频(要FQ):https://www.youtube.com/watch?v=5u0dtzXL3PQ Palette是一个在support-v7包中的一个颜色提取工具类,用法比较简单,而且是谷歌官方 ...

  7. Android开发之封装log打印日志的工具类,实用logutils详细代码

    public final class LogUtil { /** all Log print on-off */ private final static boolean all = true; /* ...

  8. Android开发之dip, dp, px, sp区别

    显示单位px和dip以及sp的区别 dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和Q ...

  9. dp px 互转工具类

    public class DensityUtils { public static int dpToPx(Context context,int dp){ float density = contex ...

随机推荐

  1. Buy or Build(UVa1151)

    如果枚举每个套餐,并每次都求最小生成树,总时间复杂度会很高,因而需要先求一次原图的最小生成树,则枚举套餐之后需要考虑的边大大减少了. 具体见代码: #include<cstdio> #in ...

  2. 锋利的jQuery初学(5)

    层级选择器: 层级选择器 符号 解释 使用 空格 后代选择器 $("div p").css("","") + 紧邻选择器 $("d ...

  3. PTA——求n以内k个质数和

    PTA 7-51 求n以内最大的k个素数以及它们的和 #include<stdio.h> #include<math.h> int isPrime(int n); int ma ...

  4. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  5. 第二次作业-分布式版本控制系统Git的安装与使用

    本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2103 我的github远程仓库的地址:https://github ...

  6. tensorflow 代码阅读

    具体实现: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/framework 『深度长文』Tensorflo ...

  7. 内网渗透 关于GPO

    网上有很多讲内网渗透的文章,但看来看去还是一老外的博客给力,博客地址:www.harmj0y.net/blog,看完就明白这里面的很多思路都非常好. 做内网时,有时会碰到目标的机器开防火墙,所有端口基 ...

  8. MFC中给各个控件填充背景颜色的方法

    1.给程序设置大背景色,在OnPaint()函数中添加如下代码: CRect rect; CPaintDC dc(this); GetClientRect(rect); dc.FillSolidRec ...

  9. CodeForce Div 2 C. Masha and two friends

    题目链接: http://codeforces.com/contest/1080/problem/C 思路:双向延长两个矩形方块的4边,会形成一个被分割为9块的更大立方体. 计算所有的9个方框.方框中 ...

  10. docs actifio

    http://docs.actifio.com/ https://www.actifio.com/resource-center/