react-native-navigation的迁移库

ViewUtils.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.reactnativenavigation.utils;
  2. import android.content.Context;
  3. import android.graphics.PorterDuff;
  4. import android.graphics.PorterDuffColorFilter;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.Build;
  7. import android.support.annotation.Nullable;
  8. import android.util.DisplayMetrics;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.view.ViewParent;
  12. import android.view.ViewTreeObserver;
  13. import android.view.WindowManager;
  14. import com.reactnativenavigation.NavigationApplication;
  15. import com.reactnativenavigation.params.AppStyle;
  16. import com.reactnativenavigation.screens.Screen;
  17. import java.util.concurrent.atomic.AtomicInteger;
  18. public class ViewUtils {
  19. private static final AtomicInteger viewId = new AtomicInteger(1);
  20. public static void runOnPreDraw(final View view, final Runnable task) {
  21. view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
  22. @Override
  23. public boolean onPreDraw() {
  24. if (!view.getViewTreeObserver().isAlive()) {
  25. return true;
  26. }
  27. view.getViewTreeObserver().removeOnPreDrawListener(this);
  28. task.run();
  29. return true;
  30. }
  31. });
  32. }
  33. public static void tintDrawable(Drawable drawable, int tint, boolean enabled) {
  34. drawable.setColorFilter(new PorterDuffColorFilter(enabled ? tint :
  35. AppStyle.appStyle.titleBarDisabledButtonColor.getColor(),
  36. PorterDuff.Mode.SRC_IN));
  37. }
  38. public static float convertDpToPixel(float dp) {
  39. float scale = NavigationApplication.instance.getResources().getDisplayMetrics().density;
  40. return dp * scale + 0.5f;
  41. }
  42. public static float convertPixelToSp(float pixels) {
  43. float scaledDensity = NavigationApplication.instance.getResources().getDisplayMetrics().scaledDensity;
  44. return pixels/scaledDensity;
  45. }
  46. public static float convertSpToPixel(float pixels) {
  47. float scaledDensity = NavigationApplication.instance.getResources().getDisplayMetrics().scaledDensity;
  48. return pixels * scaledDensity;
  49. }
  50. public static float getScreenHeight() {
  51. WindowManager wm = (WindowManager) NavigationApplication.instance.getSystemService(Context.WINDOW_SERVICE);
  52. DisplayMetrics metrics = new DisplayMetrics();
  53. wm.getDefaultDisplay().getMetrics(metrics);
  54. return metrics.heightPixels;
  55. }
  56. public static int generateViewId() {
  57. if (Build.VERSION.SDK_INT >= 17) {
  58. return View.generateViewId();
  59. } else {
  60. return compatGenerateViewId();
  61. }
  62. }
  63. private static int compatGenerateViewId() {
  64. for (; ; ) {
  65. final int result = viewId.get();
  66. // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
  67. int newValue = result + 1;
  68. if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
  69. if (viewId.compareAndSet(result, newValue)) {
  70. return result;
  71. }
  72. }
  73. }
  74. /**
  75. * Returns the first instance of clazz in root
  76. */
  77. @Nullable public static <T> T findChildByClass(ViewGroup root, Class clazz) {
  78. for (int i = 0; i < root.getChildCount(); i++) {
  79. View view = root.getChildAt(i);
  80. if (clazz.isAssignableFrom(view.getClass())) {
  81. return (T) view;
  82. }
  83. if (view instanceof ViewGroup) {
  84. view = findChildByClass((ViewGroup) view, clazz);
  85. if (view != null && clazz.isAssignableFrom(view.getClass())) {
  86. return (T) view;
  87. }
  88. }
  89. }
  90. return null;
  91. }
  92. public static void performOnChildren(ViewGroup root, PerformOnViewTask task) {
  93. for (int i = 0; i < root.getChildCount(); i++) {
  94. View child = root.getChildAt(i);
  95. if (child instanceof ViewGroup) {
  96. performOnChildren((ViewGroup) child, task);
  97. }
  98. task.runOnView(child);
  99. }
  100. }
  101. public interface PerformOnViewTask {
  102. void runOnView(View view);
  103. }
  104. public static void performOnParentScreen(View child, Task<Screen> task) {
  105. Screen parentScreen = findParentScreen(child.getParent());
  106. if (parentScreen != null) {
  107. task.run(parentScreen);
  108. }
  109. }
  110. private static Screen findParentScreen(ViewParent parent) {
  111. if (parent == null) {
  112. return null;
  113. }
  114. if (parent instanceof Screen) {
  115. return (Screen) parent;
  116. }
  117. return findParentScreen(parent.getParent());
  118. }
  119. }