Android Pixel to Dip Convert pixelsAsDp(Context context, int sizeInPx)

Here you can find the source of pixelsAsDp(Context context, int sizeInPx)

Description

pixels As Dp

License

Open Source License

Declaration

public static int pixelsAsDp(Context context, int sizeInPx) 

Method Source Code

//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);
    }
}

Related

  1. pxToDp(Context ctx, int px)
  2. pxToSp(Context context, int pxValue)
  3. getDpFromPixel(float px, Context context)
  4. convertPxtoDp(Context context, int px)
  5. px2dip(Context context, int px)
  6. pixelsToDpi(Resources res, int pixels)
  7. convertPX2DIP(Context context, float px)
  8. px2dip(Context context, float px)
  9. px2sp(Context context, float pxValue)