android Imageview layoutparams in dip?

15,531

Solution 1

See http://developer.android.com/guide/practices/screens_support.html#screen-independence

On how to convert from dpi to pixels.

Solution 2

The following helper method takes as input device independent pixels (DIP) and returns actual pixels for the current device.

private int getPixels(int dipValue) {
    Resources r = getResources();
    int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                            dipValue, 
                                            r.getDisplayMetrics());
    return px;
}

Solution 3

If you store your dimension as a resource, you can use

getResources().getDimensionPixelSize(id);

This will also make it easier to adjust your layouts.

Share:
15,531
Fabien
Author by

Fabien

Updated on July 04, 2022

Comments

  • Fabien
    Fabien almost 2 years

    I'm using the following code :

    ImageView i = new ImageView(mContext);

    i.setImageResource(mImageIds[position]);

    i.setScaleType(ImageView.ScaleType.FIT_XY);

           //pixels settings !!!
    

    i.setLayoutParams(new Gallery.LayoutParams(200, 100));

    but as you can see, it is setting the layout dimensions in pixels.

    Does anybody know how to do the same thing but in "dip" ?