From 2018937ad70336f88d8efda4b55afc1570e325c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E7=A3=8A?= <294531121@qq.com> Date: Wed, 18 Aug 2021 20:15:38 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-57181=20=E3=80=90FRM=E5=B8=83=E5=B1=80?= =?UTF-8?q?=E6=8E=A8=E8=8D=90=E3=80=91=E7=BB=84=E4=BB=B6=E5=A4=8D=E7=94=A8?= =?UTF-8?q?-=E8=87=AA=E5=8A=A8=E9=97=B4=E8=B7=9D=E7=9B=AE=E5=89=8D?= =?UTF-8?q?=E4=B9=88=E6=9C=89=E6=8A=8A=E5=B0=8F=E6=95=B0=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=88=90=E6=95=B4=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mainframe/MultiSelectionArrangement.java | 66 ++++++++++++++----- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/designer-form/src/main/java/com/fr/design/mainframe/MultiSelectionArrangement.java b/designer-form/src/main/java/com/fr/design/mainframe/MultiSelectionArrangement.java index 27756de83..49dbc8c97 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/MultiSelectionArrangement.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/MultiSelectionArrangement.java @@ -70,22 +70,22 @@ public class MultiSelectionArrangement { // 水平分布,自动,间距由selectedCreators和border共同计算而来 public void horizontalAutoDistribution() { sortHorizontal(); - int gap = calculateHorizontalGap(); - horizontalDistribution(gap); + int[] gaps = calculateHorizontalGaps(); + horizontalDistribution(gaps); } // 水平分布,手动,传入一个间距,排列selectedCreators public void horizontalManualDistribution(int gap) { sortHorizontal(); reSizeRecByHorizontal(gap); - horizontalDistribution(gap); + horizontalDistribution(fillGaps(gap, selectedCreators.size() - 1)); } - private void horizontalDistribution(int gap) { + private void horizontalDistribution(int[] gaps) { for (int i = 1; i < selectedCreators.size() - 1; i++) { XCreator creator = selectedCreators.get(i); XCreator preCreator = selectedCreators.get(i - 1); - creator.setLocation(preCreator.getX() + preCreator.getWidth() + gap, creator.getY()); + creator.setLocation(preCreator.getX() + preCreator.getWidth() + gaps[i - 1], creator.getY()); } update(); } @@ -133,34 +133,35 @@ public class MultiSelectionArrangement { } // 计算selectedCreators的均分间距 - private int calculateHorizontalGap() { + private int[] calculateHorizontalGaps() { int sum = 0; for (XCreator creator : selectedCreators) { sum += creator.getWidth(); } + int gapCount = selectedCreators.size() - 1; XCreator head = selectedCreators.get(0); - XCreator tail = selectedCreators.get(selectedCreators.size() - 1); + XCreator tail = selectedCreators.get(gapCount); int distanceBetweenHeadAndTailCreators = Math.abs(head.getX() - tail.getX()) + tail.getWidth(); - return (distanceBetweenHeadAndTailCreators - sum) / (selectedCreators.size() - 1); + return calculateIntegerGaps(distanceBetweenHeadAndTailCreators - sum, gapCount); } public void verticalAutoDistribution() { sortVertical(); - int gap = calculateVerticalGap(); - verticalDistribution(gap); + int[] gaps = calculateVerticalGaps(); + verticalDistribution(gaps); } public void verticalManualDistribution(int gap) { sortVertical(); reSizeRecByVertical(gap); - verticalDistribution(gap); + verticalDistribution(fillGaps(gap, selectedCreators.size() - 1)); } - private void verticalDistribution(int gap) { + private void verticalDistribution(int[] gaps) { for (int i = 1; i < selectedCreators.size() - 1; i++) { XCreator creator = selectedCreators.get(i); XCreator preCreator = selectedCreators.get(i - 1); - creator.setLocation(creator.getX(), preCreator.getY() + preCreator.getHeight() + gap); + creator.setLocation(creator.getX(), preCreator.getY() + preCreator.getHeight() + gaps[i - 1]); } update(); } @@ -207,15 +208,48 @@ public class MultiSelectionArrangement { }); } - private int calculateVerticalGap() { + private int[] calculateVerticalGaps() { int sum = 0; for (XCreator creator : selectedCreators) { sum += creator.getHeight(); } + int gapCount = selectedCreators.size() - 1; XCreator head = selectedCreators.get(0); - XCreator tail = selectedCreators.get(selectedCreators.size() - 1); + XCreator tail = selectedCreators.get(gapCount); int distanceBetweenHeadAndTailCreators = Math.abs(head.getY() - tail.getY()) + tail.getHeight(); - return (distanceBetweenHeadAndTailCreators - sum) / (selectedCreators.size() - 1); + return calculateIntegerGaps(distanceBetweenHeadAndTailCreators - sum, gapCount); + } + + private int[] calculateIntegerGaps(int gapTotalSize, int gapCount) { + int finalGap[] = new int[gapCount]; + // gapTotalSize: 原先的所有未取整的gap的总和,是一个整数 + int intGap = Math.round((float) gapTotalSize / gapCount); + // 把所有取整的gap求和,得到的整数gap + int intTotalSize = intGap * (gapCount); + // 求差,可以知道总误差 + int difference = intTotalSize - gapTotalSize; + // 遍历,由于取整是四舍五入得到的,取整后每个gap和取整前的gap最多相差0.5,故difference绝对值小于gapCount的 + for (int i = 0; i < gapCount; i++) { + if (i < Math.abs(difference)) { + if (difference < 0) { + // 说明取整后gap总误差小于取整前总gap,一个个加1补上 + finalGap[i] = intGap + 1; + } else { + // 说明取整后gap总误差大于取整前总gap,一个个减1去掉 + finalGap[i] = intGap - 1; + } + } else { + finalGap[i] = intGap; + } + } + return finalGap; + } + + // 创建用gap填满一个size大小的数组 + private int[] fillGaps(int gap, int size) { + int[] gaps = new int[size]; + Arrays.fill(gaps, gap); + return gaps; } private void update() {