code: private static int getResizedDimension(int maxPrimary, int maxSecondary, int actualPrimary,
                                             int actualSecondary, ScaleType scaleType) {
  
          // If no dominant value at all, just return the actual.
          if ((maxPrimary == 0) && (maxSecondary == 0)) {
              return actualPrimary;
          }
  
          // If ScaleType.FIT_XY fill the whole rectangle, ignore ratio.
          if (scaleType == ScaleType.FIT_XY) {
              if (maxPrimary == 0) {
                  return actualPrimary;
              }
              return maxPrimary;
          }
  
          // If primary is unspecified, scale primary to match secondary's scaling ratio.
          if (maxPrimary == 0) {
              double ratio = (double) maxSecondary / (double) actualSecondary;
              return (int) (actualPrimary * ratio);
          }
  
          if (maxSecondary == 0) {
              return maxPrimary;
          }
  
          double ratio = (double) actualSecondary / (double) actualPrimary;
          int resized = maxPrimary;
  
          // If ScaleType.CENTER_CROP fill the whole rectangle, preserve aspect ratio.
          if (scaleType == ScaleType.CENTER_CROP) {
              if ((resized * ratio) < maxSecondary) {
                  resized = (int) (maxSecondary / ratio);
              }
              return resized;
          }
  
          if ((resized * ratio) > maxSecondary) {
              resized = (int) (maxSecondary / ratio);
          }
          return resized;
      }
docs: Scales one side of a rectangle to fit aspect ratio.
  
  @param maxPrimary Maximum size of the primary dimension (i.e. width for
  max width), or zero to maintain aspect ratio with secondary
  dimension
  @param maxSecondary Maximum size of the secondary dimension, or zero to
  maintain aspect ratio with primary dimension
  @param actualPrimary Actual size of the primary dimension
  @param actualSecondary Actual size of the secondary dimension
  @param scaleType The ScaleType used to calculate the needed image size.