Android Imageview layoutparams in dip?

I am 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 sets the dimensions of the layout in pixels.

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

+4
source share
3 answers

The following helper method accepts input independent pixels (DIP) and returns the 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; } 
+19
source

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

 getResources().getDimensionPixelSize(id); 

It will also simplify layout customization.

+10
source

All Articles