ulfben

DIP to Pixel (Android)

Jan 8th, 2017
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int DIP_VALUE = 5;
  2. int pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DIP_VALUE, getResources().getDisplayMetrics());
  3.  
  4. //or simply
  5. float pixels = getResources().getDimensionPixelSize(DIP_VALUE);
  6.  
  7. mTextView.setTextSize(pixels);
  8.  
  9. public static float getDipFromPixels(float px) {
  10.         return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, px, Resources.getSystem().getDisplayMetrics());
  11. }
  12.  
  13. public static float getPixelsFromDip(float dip) {
  14.        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, Resources.getSystem().getDisplayMetrics());
  15. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×