Is GetChildDrawingOrder called / used randomly?

I create an isometric map with simple fragments, and I & rsquo; ve extended RelativeLayoutto create a layout containing these fragments. Indeed, just using RelativeLayoutas-works fine, as long as my orientation matches the order in which the tiles were written to the XML file; all i & rsquo; ve are overwritten - these are the constructors where I just call superand setChildrenDrawingOrderEnabled(true);along with setting some variables (grid height and width) and then getChildDrawingOrder.

My code for getChildDrawingOrdercalculates a new index for a given child, and also sets the line in the child i->i', where iis the source index and i'is the new index. i & rsquo; m using this for testing; children set this line on themselves together with their coordinates.

Unfortunately, this does not work correctly, or rather, it is unstable. Of the nine plates in my test case, three dons do not seem to have any at all getChildDrawingOrder: the line mentioned above is - null. Of the rest, at least one of them is disabled, despite the fact that he received the correct index.

Here's the image (in orientation TOP):

Image of getChildDrawingOrder failure

, (0,2), (1,2) (2,1) null, getChildDrawingOrder, -, . , (1,0) (1,1), i (3) i' (1) (1,1) & ss; s (4 4, ).

s 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 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 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;
    }
}

- , ? isn & rsquo; t getChildDrawingOrder ? (1,0) , getChildDrawingOrder?

+5
2

, , Android. getChildDrawingOrder: i - " i?"? " ?" NULL , , i.

, onMeasure, SparseIntArray, getChildDrawingOrder. .

Back- getChildDrawingOrder, , , , . , , , , x y, , . O (n²) (: ). .

+10

, , getChildDrawingOrder

  • , ( )
  • /

RelativeLayout:

public class AlternatingChildDrawingOrderRelativeLayout extends RelativeLayout {

    // the childDrawingOrder modifier
    private int childDrawingOrderModifier = 0;

    public AlternatingChildDrawingOrderRelativeLayout(Context context) {
        super(context);
        init(context);
    }

    void init(Context context) {
        setClickable(true);
        setChildrenDrawingOrderEnabled(true);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // increment to adjust the child drawing order
                childDrawingOrderModifier++;
                // call invalidate to redraw with new order modifier
                ViewCompat.postInvalidateOnAnimation(v);
            }
        });
    }

    @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        // increment i with the modifier, then afford for out of bounds using modulus of the child count
        int returnValue = (i + childDrawingOrderModifier) % childCount;
        Log.v(VIEW_LOG_TAG, "getChildDrawingOrder returnValue=" + returnValue + " i=" + i);
        return returnValue;
    }

    public AlternatingChildDrawingOrderRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AlternatingChildDrawingOrderRelativeLayout(Context context,
                                                      AttributeSet attrs,
                                                      int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public AlternatingChildDrawingOrderRelativeLayout(Context context,
                                                      AttributeSet attrs,
                                                      int defStyleAttr,
                                                      int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

}

xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.ui.AlternatingChildDrawingOrderRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="300dp">

    <View
        android:id="@+id/red"
        android:layout_width="125dp"
        android:layout_height="300dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#f00"
        />

    <View
        android:id="@+id/green"
        android:layout_width="125dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="#0f0"/>

    <View
        android:id="@+id/blue"
        android:layout_width="125dp"
        android:layout_height="300dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#00f"/>

</com.example.ui.AlternatingChildDrawingOrderRelativeLayout>

Initial drawing order (blue) Press first (red) ()></a></p>  <p>      ,       xml:</p>  <pre><code>Red = index 0 Green = index 1 Blue = index 2   </code></pre>  <p>,     (   ),  : , , .     <code>getChildDrawingOrder</code>,  </p>  <pre><code>V/View: getChildDrawingOrder returnValue=0 i=0 V/View: getChildDrawingOrder returnValue=1 i=1 V/View: getChildDrawingOrder returnValue=2 i=2 </code></pre>  <p> ,    ,    , , </p>  <pre><code>V/View: getChildDrawingOrder returnValue=1 i=0 V/View: getChildDrawingOrder returnValue=2 i=1 V/View: getChildDrawingOrder returnValue=0 i=2 </code></pre>  <p>,   ,  ,       ,    : , , .</p>  <pre><code>V/View: getChildDrawingOrder returnValue=2 i=0 V/View: getChildDrawingOrder returnValue=0 i=1 V/View: getChildDrawingOrder returnValue=1 i=2 </code></pre>  <p>          ,       -  </p>  <p>HTHS!</P></div></body></html>

+1

All Articles