Android 编程下 px - dp 的相互转换
生活随笔
收集整理的這篇文章主要介紹了
Android 编程下 px - dp 的相互转换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在實(shí)際開(kāi)發(fā)中從美工手中獲取的控件或布局長(zhǎng)度有時(shí)為 px 值,這時(shí)就需要我們將 px 值轉(zhuǎn)換為對(duì)應(yīng)的 dp 或者 dip 值,如下的轉(zhuǎn)換工具類提供了將 px 值轉(zhuǎn)換為 dp 值的方法。
package cn.sunzn.utils;import android.content.Context;public class CommonUtil {/*** 根據(jù)手機(jī)分辨率從 dp 轉(zhuǎn)為 px* * @param context* @param dpValue* @return*/public static int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}/*** 根據(jù)手機(jī)的分辨率從 px(像素) 的單位轉(zhuǎn)為 dp* * @param context* @param pxValue* @return*/public static int px2dip(Context context, float pxValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (pxValue / scale + 0.5f);}}?
轉(zhuǎn)載于:https://www.cnblogs.com/sunzn/archive/2013/01/27/2878391.html
總結(jié)
以上是生活随笔為你收集整理的Android 编程下 px - dp 的相互转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 利用jqueryRotare实现抽奖转盘
- 下一篇: zigbee 编译source_zigb