From 79d472790f0598bf1a8883aaa18b715afc3b6b55 Mon Sep 17 00:00:00 2001 From: p10297854 <944423246@qq.com> Date: Fri, 9 Jul 2021 15:36:02 +0800 Subject: [PATCH] ADD file via upload --- .../musicwork/freeModel/MusicScoreLayout.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 main/java/com/example/musicwork/freeModel/MusicScoreLayout.java diff --git a/main/java/com/example/musicwork/freeModel/MusicScoreLayout.java b/main/java/com/example/musicwork/freeModel/MusicScoreLayout.java new file mode 100644 index 0000000..2bb4c61 --- /dev/null +++ b/main/java/com/example/musicwork/freeModel/MusicScoreLayout.java @@ -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)); + } +}