How to convert pixel to dp?

Asked

Viewed 552 times

2

I made an application that is a form, and I did everything via code without using xml, so my textViews and Edittexts were in pixels.

No tablet works well, but mobile with smaller resolution was all "encavalado".

  • Could you post a snippet of your code? Facilitates the "show how".

3 answers

2

In order for the dimensions to remain consistent between the various types of screen you should think in terms of dp and not pixel.

As most (I would say all but I’m not sure) of the methods that use dimensions are expressed in pixels should convert dps into pixels.

To make this conversion you can use the following method:

public static int convertDpToPixels(float dp, Activity context){

    DisplayMetrics metrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float density = metrics.density;
    return (int) Math.ceil(dp * density);
}
  • With this code, on my phone instead of the fields decrease since the screen is smaller, the textView got even bigger.

  • The use of the dp unit does not decrease or increase the size of the views. It keeps their size regardless of the screen density. What happens on your phone is the illusion of getting bigger because the screen is much smaller than the tablet. The solution to this is to have an xml to be used in the case of small screens and another for the case of larger screens. See here as.

  • that’s right, the tablet school is 1.0 and the mobile 2.0, so I’ll see here what I do. but thanks.

1

Try something like that:

public static int converteDpParaPx(Context context, int dps) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dps, displayMetrics));
}

1

I use an auxiliary class to work with unit conversions.

package com.example;

import android.content.res.Resources;

public class UnityConverter {

    private static float scaleFactor = 1;

    private UnityConverter() {

    }

    /**
     * Configura o conversor de acordo com o device do usuário.
     * 
     * @param res
     *            os resources da aplicação
     */
    public static void config(Resources res) {
        scaleFactor = res.getDisplayMetrics().density;
    }

    /**
     * Converte um valor numerico para a unidade dp sem arredondamento
     * 
     * @param value
     *            o valor a ser convertido
     * @return o valor em dp's
     */
    public static float toDp(float value) {
        return value * scaleFactor;
    }

    /**
     * Converte um valor numerico para a unidade dp sem arredondamento
     * 
     * @param value
     *            o valor a ser convertido
     * @return o valor em dp's
     */
    public static float toDp(int value) {
        return value * scaleFactor;
    }

    /**
     * Converte um valor numerico para a unidade dp arredondado
     * 
     * @param value
     *            o valor a ser convertido
     * @return o valor em dp's
     */
    public static int toRoundDp(float value) {
        return (int) (value * scaleFactor);
    }

    /**
     * Converte um valor numerico para a unidade dp arredondado
     * 
     * @param value
     *            o valor a ser convertido
     * @return o valor em dp's
     */
    public static int toRoundDp(int value) {
        return (int) (value * scaleFactor);
    }
}

Use

UnityConverter.config(getResources());
int dp = UnityConverter.toRoundDp(120)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.