领透酷医云安卓客户端

SizeUtils.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.linkdialy.mobile.kuyicloud.utils;
  2. import android.content.Context;
  3. import android.view.View;
  4. import android.view.ViewTreeObserver;
  5. public class SizeUtils {
  6. /**
  7. * The absolute height of the display in pixels
  8. *
  9. * @param context Context
  10. * @return px(int)
  11. */
  12. public static int getScreenHeight(Context context) {
  13. return context.getResources().getDisplayMetrics().heightPixels;
  14. }
  15. /**
  16. * The absolute width of the display in pixels
  17. *
  18. * @param context Context
  19. * @return px(int)
  20. */
  21. public static int getScreenWidth(Context context) {
  22. return context.getResources().getDisplayMetrics().widthPixels;
  23. }
  24. /**
  25. * Size tool to convert dp to px
  26. *
  27. * @param context Context
  28. * @param dpValue dp unit
  29. * @return The converted size in pixel
  30. */
  31. public static int convertDp2Px(Context context, int dpValue) {
  32. final float scale = context.getResources().getDisplayMetrics().density;
  33. return (int) (dpValue * scale + 0.5f);
  34. }
  35. /**
  36. * Size tool to convert px to dp
  37. *
  38. * @param context Context
  39. * @param pxValue px unit
  40. * @return The converted size in pixel
  41. */
  42. public static int convertPx2Dp(Context context, float pxValue) {
  43. final float scale = context.getResources().getDisplayMetrics().density;
  44. return (int) (pxValue / scale + 0.5f);
  45. }
  46. /**
  47. * Size tool to convert px to dp
  48. *
  49. * @param context Context
  50. * @param spValue px unit
  51. * @return The converted size in pixel
  52. */
  53. public static int convertSp2Px(Context context, float spValue) {
  54. final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
  55. return (int) (spValue * fontScale + 0.5f);
  56. }
  57. /**
  58. * Size tool to convert px to dp
  59. *
  60. * @param context Context
  61. * @param pxValue px unit
  62. * @return The converted size in pixel
  63. */
  64. public static int convertPx2Sp(Context context, float pxValue) {
  65. final float scale = context.getResources().getDisplayMetrics().scaledDensity;
  66. return (int) (pxValue / scale + 0.5f);
  67. }
  68. /**
  69. * Gain the width of the widget
  70. *
  71. * @param view The view to be measured
  72. * @return Get the width of the widget
  73. */
  74. public static int getWidgetWidth(View view) {
  75. int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
  76. int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
  77. view.measure(w, h);
  78. return view.getMeasuredWidth();
  79. }
  80. /**
  81. * Gain the height of the widget
  82. *
  83. * @param view The view to be measured
  84. * @return Get the Height of the widget
  85. */
  86. public static int getWidgetHeight(View view) {
  87. int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
  88. int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
  89. view.measure(w, h);
  90. return view.getMeasuredHeight();
  91. }
  92. public static int getWidgetHeightWithObv(final View view) {
  93. int height = 0;
  94. final ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
  95. viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
  96. @Override
  97. public boolean onPreDraw() {
  98. viewTreeObserver.removeOnPreDrawListener(this);
  99. // h = view.getMeasuredHeight();
  100. return true;
  101. }
  102. });
  103. return height;
  104. }
  105. //
  106. // No2:
  107. //
  108. // ViewTreeObserver vto = imageView.getViewTreeObserver();
  109. // vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
  110. // public boolean onPreDraw() {
  111. // vto.removeOnPreDrawListener(this);
  112. // int height = imageView.getMeasuredHeight();
  113. // int width = imageView.getMeasuredWidth();
  114. // return true;
  115. // }
  116. // });
  117. //
  118. // 这个方法,我们需要注册一个ViewTreeObserver的监听回调,这个监听回调,就是专门监听绘图的,既然是监听绘图,那么我们自然可以获取测量值了,同时,我们在每次监听前remove前一次的监听,避免重复监听。
  119. //
  120. //
  121. //
  122. // No3:
  123. //
  124. // ViewTreeObserver vto = imageView.getViewTreeObserver();
  125. // vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  126. // @Override
  127. // public void onGlobalLayout() {
  128. // imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  129. // imageView.getHeight();
  130. // imageView.getWidth();
  131. // }
  132. // });
  133. //
  134. // 这个方法于第2个方法基本相同,但他是全局的布局改变监听器,所以是最推荐使用的。
  135. }