Android网易新闻评论盖楼效果的实现



原理解释
我们设置好每个child的margin后,我们在画边框的时候,边框的top=child的left,其他的不变,就有盖楼的效果了。

 
下面是关键代码:

public 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 ) ;

}

}

res/drawable/bound.xml

<?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>



下面是源代码的链接:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值