res/drawable/bound.xmlpublic class FloorView extends LinearLayout {
private int density ;
private Drawable drawer ; //draw the bound of floor
public FloorView ( Context context ) {
super ( context ) ;
init ( context ) ;
}
public void setBoundDrawer ( Drawable drawable ) {
drawer = drawable ;
}
private void init ( Context context ) {
this.setOrientation ( LinearLayout.VERTICAL ) ;
density = ( int ) context.getResources().getDisplayMetrics().density ;
}
public void relayoutChildren () { //set the margin of each child
int count = getChildCount () ;
for ( int i = 0 ; i < count ; i ++ ) {
View view = getChildAt ( i ) ;
LayoutParams layout = new LayoutParams ( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT ) ;
layout.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL ;
int margin = Math.min ( ( count - i -1 ) , 4) ) * 3 * density ;
layout.leftMargin = layout.rightMargin = margin ;
if ( i == count -1 ) {
layout.topMargin = 0 ;
} else {
layout.topMargin = Math.min ( ( count - i ) , 4 ) * 3 * density ;
}
view.setLayoutParams ( layout ) ;
}
}
@Override
protected void dispatchDraw ( Canvas canvas ) {
int count = getChildCount () ;
if ( null != drawer && count > 0 ) {
for ( int i = count - 1 ; i >= 0 ; i -- ) {
View view = getChildAt ( i ) ;
//这就是最核心的地方
drawer.setBounds ( view.getLeft(), view.getLeft(), view.getRight(), view.getBottom() ) ;
drawer.draw ( canvas ) ;
}
}
super.dispatchDraw ( canvas ) ;
}
}
<?xml version="1.0" encoding="utf-8"?>
<shape xmls:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#3CCCCCCC"/>
</shape>