I've been working on an app where it is important that drawables are always the same size relative to the display's width, e.g. always 1/8 of the screen height in pixels. I understand how density independent pixels work, but as their goal is to always represent the same physical size, it is pretty much the opposite of what i need. How can I achive this? I have tried using a .png-file with a very high resolution and downscale accordingly, but its always blurry and the quality loss is unbelievable. Thank you for your help, best regards.
Asked
Active
Viewed 30 times
1

IchBinGrumpig
- 119
- 1
- 1
- 10
-
*their goal is to always represent the same physical size* - I think you have that backwards: "A device-independent pixel (also: density-independent pixel, dip, dp) is a physical unit of measurement based on a coordinate system held by a computer and represents an abstraction of a pixel for use by an application that an underlying system then converts to physical pixels." – Tim Sep 28 '17 at 14:11
-
Calculate the height of the device display and divide it by 8 to get 1/8th of the display and change the height of the image view to the calculated height with layout params – hushed_voice Sep 28 '17 at 14:32
-
But is this approach still considered good/best practice? I have read in the google developer docs about supporting multiple screens that apps should not mind with single pixels, but only with density and physical size of the screen. That, unfortunately, is not really an option for me, I believe. – IchBinGrumpig Sep 29 '17 at 18:09
1 Answers
0
Had the exact same issue. Here's my solution:
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
Double newHeight = 0.125 * displayMetrics.heightPixels;
yourImageView.getLayoutParams().height = newHeight.intValue();
Alternatively, use a LinearLayout. Set your ImageView's weight to 1, and the LinearLayout's weightSum to 8.

AD S2dios
- 31
- 5