ADD file via upload

master
p10297854 4 years ago
parent 73068f8388
commit 79d472790f

@ -0,0 +1,89 @@
package com.example.musicwork.freeModel;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class MusicScoreLayout extends ViewGroup {
private int width;
private int height;
//构造方法
public MusicScoreLayout(Context context) {
super(context);
}
public MusicScoreLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MusicScoreLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//设置宽高?
public void setWidth(int width) {
this.width = width;
requestLayout();
}
public void setHeight(int height) {
this.height = height;
requestLayout();
}
//控制子控件的换行
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int cellWidth = width;
int cellheight = height;
int columns = (r - 1) / cellWidth;
if (columns < 0) {
columns = 1;
}
int x = 0, y = 0, i = 0;
int count = getChildCount();//获取子控件数量
for (int j = 0; j < count; j++) {
final View childView = getChildAt(j);//获取子控件
//获取子控件宽高
int w = childView.getMeasuredWidth();
int h = childView.getMeasuredHeight();
//计算子控件顶点坐标?
int left = x + (cellWidth - w) / 2;
int top = y + (cellheight - h) / 2;
//放置子控件?
childView.layout(left, top, left + w, top + h);
if (i >= (columns - 1)) {
i = 0;
x = 0;
y += cellheight;
} else {
i++;
x += cellWidth;
}
}
}
//计算控件及子控件所占区域
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//创建测量参数
int cellWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
int cellHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
//记录ViewGroup中Child的总个数
int count = getChildCount();
//设置子控件的宽高
for (int i = 0; i < count; i++) {
View childView = getChildAt(i);
childView.measure(cellWidthSpec, cellHeightSpec);
}
//设置容器控件所占区域大小
//int paddingLeft = this.getPaddingLeft();
//int paddingRight = this.getPaddingRight();
setMeasuredDimension(resolveSize(width * count, widthMeasureSpec), resolveSize(height * count, heightMeasureSpec));//???
//setMeasuredDimension(width, resolveSize(height * count, heightMeasureSpec));
}
}
Loading…
Cancel
Save