Skip to content

Instantly share code, notes, and snippets.

@boyan01
Created January 10, 2020 02:37
Show Gist options
  • Save boyan01/77fe25c53976ab8b9d54b113b225b942 to your computer and use it in GitHub Desktop.
Save boyan01/77fe25c53976ab8b9d54b113b225b942 to your computer and use it in GitHub Desktop.
自适应屏幕宽高
private static int[] calculateAutoFitWidthHeight(Size parentSize, Size originSize) {
int width = parentSize.getWidth();
int height = parentSize.getHeight();
int originW = originSize.getWidth();
int originH = originSize.getHeight();
float v1 = height / ((float) originH);
float v2 = width / ((float) originW);
float v = Math.min(v1, v2);
float targetWidth = originW * v + 0.5f;
float targetHeight = originH * v + 0.5f;
return new int[]{(int) targetWidth, (int) targetHeight};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment