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 R
Here is a simple example that shows how to override getChildDrawingOrder
The RelativeLayout class:
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);
}
}
The xml layout
What it looks like
Pictured on the left is the starting drawing order, which is the default based on the xml layout:
Red = index 0
Green = index 1
Blue = index 2
So from first to last (or think bottom to top) that is: Red, Green, Blue. Here is the log dump of getChildDrawingOrder
being called
V/View: getChildDrawingOrder returnValue=0 i=0
V/View: getChildDrawingOrder returnValue=1 i=1
V/View: getChildDrawingOrder returnValue=2 i=2
In the middle, after our first tap, the order changes to Green, Blue, Red
V/View: getChildDrawingOrder returnValue=1 i=0
V/View: getChildDrawingOrder returnValue=2 i=1
V/View: getChildDrawingOrder returnValue=0 i=2
And, on the right side shows us what it looks like after our second tap since the order change to: Blue, Red, Green.
V/View: getChildDrawingOrder returnValue=2 i=0
V/View: getChildDrawingOrder returnValue=0 i=1
V/View: getChildDrawingOrder returnValue=1 i=2
Any tap after this pretty much loops it back to the original order where blue was last to be drawn due to the modulus calculation
HTHs!