Here you can find the source of pixelsAsDp(Context context, int sizeInPx)
public static int pixelsAsDp(Context context, int sizeInPx)
//package com.java2s; /**//w ww . j av a 2 s. co m * This file is part of LoLin1. * <p/> * LoLin1 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * <p/> * LoLin1 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * <p/> * You should have received a copy of the GNU General Public License * along with LoLin1. If not, see <http://www.gnu.org/licenses/>. * <p/> * Created by Jorge Antonio Diaz-Benito Soriano on 03/01/14. * <p/> * Accessing resources through reflection is said to be ten times faster than through getResources(), and thus it's done when possible. */ import android.content.Context; public class Main { public static int pixelsAsDp(Context context, int sizeInPx) { float scale = context.getResources().getDisplayMetrics().density; return (int) (sizeInPx * scale + 0.5f); } }