SHARE
TWEET

DIP to Pixel (Android)

ulfben Jan 8th, 2017 (edited) 71 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
Top