public class DensityUtil {

    private DensityUtil(){
throw new AssertionError();
} /**
* dp转px
* @param context
* @param dpVal
* @return
*/
public static int dp2px(Context context, float dpVal){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal,getDisplayMetrics(context));
} /**
* sp转px
* @param context
* @param spVal
* @return
*/
public static int sp2px(Context context, float spVal){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,getDisplayMetrics(context));
} /**
* px转dp
* @param context
* @param pxVal
* @return
*/
public static int px2dp(Context context, float pxVal){
return (int) (pxVal / getDisplayMetrics(context).density + 0.5f);
} /**
* px转sp
* @param context
* @param pxVal
* @return
*/
public static int px2sp(Context context, float pxVal){
return (int) (pxVal / getDisplayMetrics(context).scaledDensity + 0.5f);
} /**
* 获取DisplayMetrics
* @param context
* @return
*/
public static DisplayMetrics getDisplayMetrics(Context context){
return context.getResources().getDisplayMetrics();
} }

Android的代码适配方案的更多相关文章

  1. 【cocos2d-x 手游研发小技巧(3)Android界面分辨率适配方案】

    先感叹一下吧~~android的各种分辨率各种适配虐我千百遍,每次新项目我依旧待它如初恋···· 每家公司都有自己项目工程适配的方案,这种东西就是没有最好,只有最适合!!! 这次新项目专项针对andr ...

  2. Android开发——Android手机屏幕适配方案总结

    )密度无关像素,单位为dp,是Android特有的单位 Android开发时通常使用dp而不是px单位设置图片大小,因为它可以保证在不同屏幕像素密度的设备上显示相同的效果. /** * dp与px的转 ...

  3. 最清晰的Android多屏幕适配方案

    问题的引入 当您的Android应用即将发布的时候,如果你想让更多的用户去使用你的应用,摆在工程师面前的一个重要问题就是如何让你的应用能在各种各样的终端上运行,这里的各种各样首当其冲的就是不同的屏幕分 ...

  4. Android Notification 版本适配方案

    Notification 介绍见:https://developer.android.com/reference/android/app/Notification.html Android api 一 ...

  5. android屏幕适配方案

    曾经看了android的屏幕适配方案,有非常多种.当中自己用到的一种是:先找一款主流的分辨率的android机,如:1080*1920的分辨率做基准,然后在这个基准上.调整好一切布局.图片.适配其它手 ...

  6. android多分辨率多密度下界面适配方案

    前言 Android 设计之初就考虑到了UI在多平台的适配,它本身提供了一套完善的适配机制,随着版本的发展适配也越来越精确,UI适配主要受平台两个因素的影响:屏幕尺寸 (屏幕的像素宽度及像素高度)和屏 ...

  7. Android 屏幕适配方案

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45460089: 本文出自:[张鸿洋的博客] 1.概述 大家在Android开发 ...

  8. Android最佳实践之SystemBar状态栏全版本适配方案

    前言 自从MD设计规范出来后,关于系统状态栏的适配越受到关注,因为MD在5.0以后把系统状态栏的颜色改为可由开发者配置的,而在5.0之前则无法指定状态栏的颜色,所以这篇就说说使用Toolbar对系统状 ...

  9. 实用Android 屏幕适配方案分享

    转载地址:http://blog.csdn.net/gao_chun/article/details/45645051 真正可用,并且简单易行,可以在多个屏幕大小和屏幕密度上有良好表现的Android ...

随机推荐

  1. hadoop报JAVA_HOME is not set暂时解决办法

    直接在etc/hadoop/hadoop-env.sh中 export JAVA_HOME=XXX

  2. 端口扫描 开启 防火墙 iptables SELinux

    Linux 如何打开端口 - lclc - 博客园 https://www.cnblogs.com/lcword/p/5869522.html linux如何查看端口相关信息_百度经验 https:/ ...

  3. usaco2008 nov 区间异或求和

    Problem 11: Switching Lights [LongFan, 2008] Farmer John tries to keep the cows sharp by letting the ...

  4. Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland

    D. Roads not only in Berland time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. react native 中的ListView

    ListView 的运用: 1.首先在react native中引入这个组件: 2.初始化的ListView 的相关属性: constructor(props) { super(props); con ...

  6. 字符串查找函数(BF)

    //模拟字符串定位函数 // s: abcbbghi // t: ghi // 返回6 #include <iostream> #include <string> #inclu ...

  7. POJ3376 Finding Palindromes —— 扩展KMP + Trie树

    题目链接:https://vjudge.net/problem/POJ-3376 Finding Palindromes Time Limit: 10000MS   Memory Limit: 262 ...

  8. rcfile

    转自:http://flyingdutchman.iteye.com/blog/1871025 在当前的基于Hadoop系统的数据仓库中,数据存储格式是影响数据仓库性能的一个重要因素.Facebook ...

  9. java中io类型及成熟io框架

    就io本身而言,概念上有5中模型:blocking I/O, nonblocking I/O, I/O multiplexing(select and poll), singal driven I/O ...

  10. 时间:NSTimer,代码时运行时间段,

    一:NSTimer 当时间间隔>1s是用NSTimer; 方法: [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selecto ...