package
com.inpor.fmctv.util;
import
android.content.Context;
import
android.content.res.TypedArray;
import
android.util.AttributeSet;
import
android.view.View;
import
android.view.ViewGroup;
import
com.inpor.fmctv.R;
public
class
AntoLineUtil
extends
ViewGroup {
/**
* 子view左右间距
*/
private
int
mHorizontalSpacing;
/**
* 子view上下行距离
*/
private
int
mVerticalSpacing;
private
Context context;
public
AntoLineUtil(Context context) {
this
(context,
null
);
this
.context = context;
}
public
AntoLineUtil(Context context, AttributeSet attrs) {
this
(context, attrs,
0
);
}
public
AntoLineUtil(Context context, AttributeSet attrs,
int
defStyleAttr) {
super
(context, attrs, defStyleAttr);
if
(attrs !=
null
) {
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.AntoLineUtil);
mHorizontalSpacing = array.getDimensionPixelOffset(
R.styleable.AntoLineUtil_horizontalSpacing,
0
);
mVerticalSpacing = array.getDimensionPixelOffset(
R.styleable.AntoLineUtil_verticalSpacing,
0
);
array.recycle();
if
(mHorizontalSpacing <
0
) mHorizontalSpacing =
0
;
if
(mVerticalSpacing <
0
) mVerticalSpacing =
0
;
}
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
int
width = MeasureSpec.getSize(widthMeasureSpec);
int
count = getChildCount();
for
(
int
i =
0
; i < count; i++) {
measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
}
int
widthMode = MeasureSpec.getMode(widthMeasureSpec);
if
(widthMode != MeasureSpec.EXACTLY) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(
getAutoLinefeedWidth(width), widthMode);
}
int
heightMode = MeasureSpec.getMode(heightMeasureSpec);
if
(heightMode != MeasureSpec.EXACTLY) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(
getAutoLinefeedHeight(width), heightMode);
}
super
.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* 自动换行 计算需要的宽度
*
* @param width 可用宽度
* @return 需要的宽度
*/
private
int
getAutoLinefeedWidth(
int
width) {
int
totalWidth = getPaddingLeft() + getPaddingRight();
for
(
int
i =
0
; i < getChildCount(); i++) {
if
(i >
0
) totalWidth += mHorizontalSpacing;
View child = getChildAt(i);
int
childWidth = child.getMeasuredWidth();
totalWidth += childWidth;
if
(totalWidth >= width) {
totalWidth = width;
break
;
}
}
return
totalWidth;
}
/**
* 自动换行 计算需要的高度
*
* @param width 可用宽度
* @return 需要的高度
*/
private
int
getAutoLinefeedHeight(
int
width) {
int
lineWidth = width - getPaddingLeft() - getPaddingRight();
int
availableLineWidth = lineWidth;
int
totalHeight = getPaddingTop() + getPaddingBottom();
int
lineChildIndex =
0
;
int
lineMaxHeight =
0
;
for
(
int
i =
0
; i < getChildCount(); i++) {
View child = getChildAt(i);
int
childWidth = child.getMeasuredWidth();
int
childHeight = child.getMeasuredHeight();
int
needWidth = i ==
0
? childWidth : (childWidth + mHorizontalSpacing);
if
(availableLineWidth < needWidth) {
totalHeight = totalHeight + lineMaxHeight;
if
(i >
0
) totalHeight += mVerticalSpacing;
availableLineWidth = lineWidth;
lineMaxHeight =
0
;
lineChildIndex =
0
;
}
int
realNeedWidth = lineChildIndex ==
0
? childWidth : (childWidth + mHorizontalSpacing);
lineMaxHeight = Math.max(childHeight, lineMaxHeight);
availableLineWidth = availableLineWidth - realNeedWidth;
lineChildIndex++;
}
totalHeight = totalHeight + lineMaxHeight;
return
totalHeight;
}
@Override
protected
void
onLayout(
boolean
changed,
int
l,
int
t,
int
r,
int
b) {
layout();
}
private
void
layout() {
int
count = getChildCount();
int
childLeft = getPaddingLeft();
int
childTop = getPaddingTop();
int
lineWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
int
availableLineWidth = lineWidth;
int
lineChildIndex =
0
;
int
lineMaxHeight =
0
;
for
(
int
i =
0
; i < count; i++) {
View child = getChildAt(i);
int
childWidth = child.getMeasuredWidth();
int
childHeight = child.getMeasuredHeight();
int
needWidth = i ==
0
? childWidth : (childWidth + mHorizontalSpacing);
if
(availableLineWidth < needWidth) {
availableLineWidth = lineWidth;
childTop += lineMaxHeight;
if
(i >
0
) childTop += mVerticalSpacing;
lineMaxHeight =
0
;
childLeft = getPaddingLeft();
lineChildIndex =
0
;
}
int
realNeedWidth = lineChildIndex ==
0
? childWidth : (childWidth + mHorizontalSpacing);
lineMaxHeight = Math.max(lineMaxHeight, childHeight);
child.layout(childLeft + realNeedWidth - childWidth, childTop, childLeft + realNeedWidth, childTop + childHeight);
availableLineWidth -= realNeedWidth;
childLeft += realNeedWidth;
lineChildIndex++;
}
}
public
int
getHorizontalSpacing() {
return
mHorizontalSpacing;
}
public
void
setHorizontalSpacing(
int
horizontalSpacing) {
mHorizontalSpacing = horizontalSpacing;
}
public
int
getVerticalSpacing() {
return
mVerticalSpacing;
}
public
void
setVerticalSpacing(
int
verticalSpacing) {
mVerticalSpacing = verticalSpacing;
}
}