@ -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 ( ) {