Android Open Source - DemiGod Pixel Unit Converter

From Project

Back to project page DemiGod.


The source code is released under:

GNU General Public License

If you think the Android project DemiGod listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//ww  w  .  jav  a2s .  c o  m
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;

public class PixelUnitConverter {

  Context context;
  DisplayMetrics displayMetrics;

  public PixelUnitConverter(Context context) {
    this.context = context;
    this.displayMetrics = context.getResources().getDisplayMetrics();

  public int dpToPx(int dp) {
    int valueInDp = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
    return valueInDp;

  public int pxToDp(int px) {
    int valueInDp = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_PX, px, context.getResources()
    return valueInDp;


Java Source Code List