setMeasuredDimension()中的尺寸不正确或尺寸计算不正确?

唐11995

我需要诸如“网格布局”之类的东西,但要具有元素定位的自身逻辑。我展开了“框架布局”,并编写了以下逻辑:

super.onMeasure(widthMeasureSpec,heightMeasureSpec);
int childCount = getChildCount();
    if (childCount==0) return;
    int cellSize= 0;
    for (int i=0;i<getChildCount();i++) {
        cellSize = Math.max(cellSize, getChildAt(i).getMeasuredHeight());
    }
    cellSize= Math.min(getMeasuredHeight()/rowCount,cellSize);
    if (autoColumnCount&&cellSize>0) {
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Point displaySize = new Point();
        display.getSize(displaySize);
        int screenWidth = displaySize.x;
        columnCount = Math.max(screenWidth/cellSize - autoColumnOffset,1);
    }
    cellSize = cellSize - cellPadding*2;
    ArrayList<ArrayList<Integer>> sortedIndexes = new ArrayList<>();
    int currentIndex=0;
    while (true) {
        if (currentIndex>=childCount) break;
        ArrayList<Integer> indexes = new ArrayList<>();
        indexes.add(currentIndex);
        for (int y=1;y<rowCount;y++) {
            int index = currentIndex + (y*columnCount);
            if (index<childCount)
                indexes.add(index);
            else
                break;
        }
        sortedIndexes.add(indexes);
        currentIndex++;
        if (currentIndex%columnCount==0) {
            currentIndex = (currentIndex-columnCount)+(rowCount*columnCount);
        }
    }
    int maxRows = 1;
    for (int i=0;i<rowCount;i++) {
        for (int y=0;y<sortedIndexes.size();y++) {
            ArrayList<Integer> indexes = sortedIndexes.get(y);
            if (i<indexes.size()) {
                maxRows = Math.max(maxRows,i+1);
                View v = getChildAt(indexes.get(i));
                int newX = y*cellSize+(y+1)*cellPadding+y*cellPadding;
                int newY = i*cellSize+(i+1)*cellPadding+i*cellPadding;
                int measureSpecWidth = MeasureSpec.makeMeasureSpec(Math.min(cellSize,v.getMeasuredWidth()), MeasureSpec.AT_MOST);
                int measureSpecHeight = MeasureSpec.makeMeasureSpec(Math.min(cellSize,v.getMeasuredHeight()), MeasureSpec.AT_MOST);
                v.measure(measureSpecWidth, measureSpecHeight);
                centerViewInBounds(v, newX, newY, newX + cellSize, newY + cellSize);
            }
        }
    }
    final int neededHeight = maxRows*cellSize+maxRows*cellPadding*2;
    final int neededWidth = sortedIndexes.size()*cellSize+sortedIndexes.size()*cellPadding*2;
    MyLog.print("asd", "Column count: " + sortedIndexes.size() + "\nCell size (with padding): " + (cellSize + cellPadding * 2) + "\nWidth: " + neededWidth);
    setMeasuredDimension(neededWidth, neededHeight);

单元格应为正方形,布局只能在宽度上增长并存储在Horizo​​ntalScrollView中。问题是该函数setMeasuredDimension无法正常工作,或者我收到的大小错误(尽管在我看来,所有功能都可以正常工作):通过这种逻辑,我得到了下一个东西:截屏

问题出在哪儿?提前致谢。

唐11995

好的,我阅读了有关“ android如何绘制视图”的全部信息,并在互联网上找到了更多解释,并写了下一个逻辑:

    private void sortChildren() {
    int childCount = getChildCount();
    sortedIndexes.clear();
    int currentIndex=0;
    while (true) {
        if (currentIndex>=childCount) break;
        ArrayList<Integer> indexes = new ArrayList<>();
        indexes.add(currentIndex);
        for (int y=1;y<rowCount;y++) {
            int index = currentIndex + (y*columnCount);
            if (index<childCount) {
                indexes.add(index);
                maxVisibleRows = Math.max(indexes.size(), maxVisibleRows);
            } else break;
        }
        sortedIndexes.add(indexes);
        currentIndex++;
        if (currentIndex%columnCount==0) {
            currentIndex = (currentIndex-columnCount)+(rowCount*columnCount);
        }
    }
    maxVisibleColumns = sortedIndexes.size();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int width = 0;
    int height = 0;

    sortChildren();

    switch (heightMode) {
        case MeasureSpec.EXACTLY:
            height = MeasureSpec.getSize(heightMeasureSpec);
            cellSize = height/rowCount;
            break;
        case MeasureSpec.AT_MOST:
            height = MeasureSpec.getSize(heightMeasureSpec);
            cellSize = height/rowCount;
            height = Math.min(height,cellSize* maxVisibleRows);
            break;
        case MeasureSpec.UNSPECIFIED:
            cellSize = defaultCellSize;
            height = defaultCellSize* maxVisibleRows;
            break;
    }

    switch (widthMode) {
        case MeasureSpec.EXACTLY:
            width = MeasureSpec.getSize(widthMeasureSpec);
            break;
        case MeasureSpec.AT_MOST:
            width = Math.min(MeasureSpec.getSize(widthMeasureSpec),maxVisibleColumns*cellSize);
            break;
        case MeasureSpec.UNSPECIFIED:
            width = maxVisibleColumns*cellSize;
            break;
    }

    int childCount = getChildCount();
    for (int i=0;i<childCount;i++) {
        View view = getChildAt(i);
        int cellMeasureSpec = MeasureSpec.makeMeasureSpec(cellSize-cellPadding*2,MeasureSpec.EXACTLY);
        view.measure(cellMeasureSpec,cellMeasureSpec);
    }

    setMeasuredDimension(width,height);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    for (int i=0;i<rowCount;i++) {
        for (int y = 0; y < sortedIndexes.size(); y++) {
            ArrayList<Integer> indexes = sortedIndexes.get(y);
            if (i < indexes.size()) {
                maxVisibleRows = Math.max(maxVisibleRows, i + 1);
                View v = getChildAt(indexes.get(i));
                if (v == null) continue;
                int yOffset = (int)((1.0f*rowCount-maxVisibleRows)/2*cellSize);
                int newX = y * cellSize;
                int newY = i * cellSize + yOffset;
                v.layout(newX + cellPadding, newY + cellPadding, newX + cellSize - cellPadding, newY + cellSize - cellPadding);
            }
        }
    }
}

现在,它可以按预期工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

setMeasuredDimension()中的尺寸不正确或尺寸计算不正确?

来自分类Dev

尺寸不正确?

来自分类Dev

不正确的尺寸不正确

来自分类Dev

尺寸数不正确-并行R计算

来自分类Dev

尺寸数不正确-并行R计算

来自分类Dev

for循环中的尺寸不正确

来自分类Dev

java打印:纸张尺寸不正确

来自分类Dev

iAd默认横幅尺寸不正确

来自分类Dev

图片显示尺寸不正确

来自分类Dev

8拼图图像尺寸不正确

来自分类Dev

JFrame/JPanel 的尺寸不正确

来自分类Dev

PyQT按钮在QGridLayout中的尺寸不正确

来自分类Dev

拟合函数中的 y_train 尺寸不正确

来自分类Dev

计算不正确

来自分类Dev

计算不正确

来自分类Dev

使用CoordinatorLayout时,我的ScrollView的尺寸不正确

来自分类Dev

为什么这些div的尺寸设置不正确?

来自分类Dev

“显示:无”,带有高图-尺寸不正确

来自分类Dev

Scrollview内部的布局不正确适合屏幕尺寸

来自分类Dev

getDisplayMetrics()返回不正确的密度和屏幕尺寸

来自分类Dev

“ CreateDC失败,屏幕尺寸信息可能不正确”

来自分类Dev

面包屑的dcdrilldown尺寸不正确

来自分类Dev

div显示不正确,以及尺寸调整问题

来自分类Dev

Conky窗口尺寸不正确,无法输出

来自分类Dev

G.drawimage 以不正确的尺寸绘制

来自分类Dev

自动编码器尺寸的结果不正确

来自分类Dev

Gnuplot的计算不正确

来自分类Dev

CakePHP计算不正确

来自分类Dev

GROUP BY计算不正确