android 获取 View 的曝光比例

348 阅读1分钟
public static float getExposurePercentage(View view) {
  if (view != null && view.getVisibility() == View.VISIBLE) {
    try {
      Rect rect = new Rect();
      if (view.getParent() != null && view.getGlobalVisibleRect(rect)) {
        int visibleArea = (rect.right - rect.left) * (rect.bottom - rect.top);
        float totalArea = view.getWidth() * view.getHeight();
        return (visibleArea / totalArea) * 100;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  return 0f;
}