getChildDrawingOrder名为/不正常使用吗? [英] getChildDrawingOrder called/used erratically?

查看:362
本文介绍了getChildDrawingOrder名为/不正常使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建简单的瓷砖的轴测图,以及I&rsquo的;已经扩展 RelativeLayout的以创建保存这些瓦片的布局。真的,只是使用 RelativeLayout的 AS-作为我的方向一致,其中瓦片被写入到XML文件中的顺序罚款,只要工作;所有的I&rsquo的;已经覆盖是建设者,在这里我简单地调用超级 setChildrenDrawingOrderEnabled(真); 一起建立一些变量(高度和网格的宽度),然后 getChildDrawingOrder 本身。

I am creating an isometric map with simple tiles, and I’ve extended RelativeLayout to create a layout that holds these tiles. Really, just using a RelativeLayout as-is worked fine as long as my orientation matched the order in which the tiles were written to the XML file; all I’ve overwritten are the constructors, where I simply call super and setChildrenDrawingOrderEnabled(true); along with setting up some variables (height and width of the grid), and then getChildDrawingOrder itself.

我的code计算出给定子新的索引,并且还设置了儿童的字符串 I- I标记,其中 I 是原来的索引,而 I 是新指数。 I&rsquo的,M使用此进行测试;孩子们设置与他们一起的坐标上绘制自己这个字符串。

My code for getChildDrawingOrder figures out the new index for the given child, and also sets a string in the child to i->i', where i is the original index and i' is the new index. I’m using this for testing; the children are set to draw this string on themselves along with their coordinates.

不幸的是,它不能正常工作,或者更确切地说,它工作不正常。在我的测试案例九砖,三唐&rsquo的;吨似乎有 getChildDrawingOrder 叫在所有:我上面提到的字符串。其余的,至少有一个正在尽管得到正确的索引传递给它绘制出故障了。

Unfortunately, it does not work correctly, or rather it works erratically. Of the nine tiles in my test case, three don’t seem to have getChildDrawingOrder called at all: the string I mentioned above is null. Of the rest, at least one is being drawn out of order despite getting the correct index passed to it.

下面大局;一张照片(在 TOP 方向):

Here’s a picture (in the TOP orientation):

注意,(0,2),(1,2)和(2,1)都被列为 NULL ,因此, getChildDrawingOrder 似乎从未召之即来。还要注意的是(1,0)是绘制在(1,1)的顶部,即使它的两个I (3)我'(1)是小于(1,1)大局;(分别为4,4,)

Notice that (0,2), (1,2), and (2,1) are all listed as NULL, and therefore getChildDrawingOrder appears to have never been called for them. Also note that (1,0) is drawn on top of (1,1), even though both its i (3) and i' (1) are less than (1,1)’s (4 and 4, respectively).

下面大局;从code getChildDrawingOrder

Here’s the code from getChildDrawingOrder:

@Override
protected int getChildDrawingOrder(int childCount, int i)
{
    TileView ch = (TileView)getChildAt(i);
    ch.order = "Called"; // this string is drawn on my children
    int gx, gy; // the "true" x,y for the current rotation,
                // where 0,0 is the top corner
    switch (rotation)
    {
    case TOP:
        gx = ch.x();
        gy = ch.y();
        break;
    case LEFT:
        gx = (width()-1-ch.x());
        gy = ch.y();
        break;
    case RIGHT:
        gx = ch.x();
        gy = (length()-1-ch.y());
        break;
    case BOTTOM:
        gx = (width()-1-ch.x());
        gy = (length()-1-ch.y());
        break;
    default:
        gx = ch.x();
        gy = ch.y();
    }
    int row = gx+gy; // current row
    if ( row == 0 ) // row 0 is always just the top corner and 0
    {
        ch.order = new String(i+"->0"); // string set to i->i'
        return 0;
    }
    else
    {
        int mx = width()-1, // maximum x value
            my = length()-1, // maximum y value
            mrow = mx+my, // maximum row
            min = Math.min(mx, my), // minor axis length
            maj = Math.max(mx, my), // major axis length
            retn; // for storing the return value
        // inside the top corner
        if ( row <= min )
        {
            // Gauss's formula to get number of cells in previous rows
            // plus the number for which cell in this row this is.
            retn = row*(row+1)/2+gy;
        }
        // in the middle
        else if ( row <= maj )
        {
            // Gauss's formula to get number of cells in top corner
            // plus the number of cells in previous rows of the middle section
            // plus the number for which cell in this row this is.
            retn = min*(min+1)/2+min*(row-min)+gy;
        }
        // bottom corner
        else
        {
            retn = (min+1)*(min+2)/2 // cells in the top corner
                 + min*(maj-min) // cells in the middle
                 + (mrow-maj)*(mrow-maj+1)/2 // total cells in bottom triangle
                 - (mrow-row+1)*(mrow-row+2)/2 // less cells after this one
                 + gy // which cell in this row
                 - (row-maj) // to account for gy not starting at zero
                 ;
        }
        ch.order = new String(i+"->"+retn); // string set to i->i'
        return retn;
    }
}

任何人都可以阐明什么&rsquo的一些轻;回事?为什么ISN&rsquo的;吨 getChildDrawingOrder 正在呼吁这三个砖?为什么是(1,0),以错误的顺序绘制,即使 getChildDrawingOrder 的呼吁呢?

Can anyone shed some light on what’s going on? Why isn’t getChildDrawingOrder being called for those three tiles? Why is (1,0) drawn in the wrong order, even though getChildDrawingOrder is called on it?

推荐答案

OK,理解了它通过查看Android源$ C ​​$ C。我有 getChildDrawingOrder 的映射: I 通过为&ldquo;我应该吸取这孩子的日&rdquo?;不是我什么时候应该画的孩子吗?究其原因, NULL s是因为这些儿童被自己的前拉我获得通过。

OK, figured it out by looking at the Android source code. I had the mapping of getChildDrawingOrder: the i passed is “which child should I draw i th?” not "when should I draw child i?" The reason for the NULLs is because those children were being drawn before their own i was passed.

我改变了我的code弄清楚 onMeasure 传递期间为所有儿童的顺序,保存在一个 SparseIntArray ,然后刚刚返回,从 getChildDrawingOrder 。这工作。

I changed my code to figure out the order for all children during the onMeasure pass, saving that in a SparseIntArray, and then just returned that from getChildDrawingOrder. This works.

备份-计算在 getChildDrawingOrder 函数索引,顺便说一句,是一个坏主意,除非你想依靠在孩子们中声明的顺序。因为如果你不&rsquo的;吨依靠这个顺序,你必须通过孩子的名单步行到找到一个具有相应x和y的值,这意味着你必须通过孩子每个孩子的名单行走。这一大局;一个O(N²)操作(读:效率非常低)。数学也是相当复杂的。

Back-calculating the index in the getChildDrawingOrder function, by the way, is a bad idea unless you want to rely on the order in which the children are declared. Because if you don’t rely on that order, you have to walk through the list of children to find the one that has the appropriate x and y values, which means you have to walk through the list of children for each child. That’s an O(n²) operation (read: fairly inefficient). The mathematics are also reasonably complicated.

这篇关于getChildDrawingOrder名为/不正常使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆