/* * Copyright (C) 2013 WhiteCat 白猫 (www.thinkandroid.cn) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ta.util.extend.draw; import android.content.Context; /** * @Title DensityUtils * @Package com.ta.util.extend.draw * @Description DensityUtils是一个像素与dp转换的工具 * @author 白猫 * @date 2013-1-22 下午 9:35 * @version V1.0 */ public class DensityUtils { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) * * @param context * @param dpValue * dp值 * @return 返回像素值 */ public static int dipTopx(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp * * @param context * @param pxValue * 像素值 * @return 返回dp值 */ public static int pxTodip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }