本文带大家来实现ListView的圆弧形的分布排列,原理非常easy,就是依据ListView的每个Item的高度来对每个item进行偏移。

首先自己定义一个LinearLayout,这是ListView的每一个Item的根布局,用来对每一个item进行偏移的。

以下上代码:

public class MatrixLinearLayout extends LinearLayout {
private int h = 0;
private float fullTrans = 40f;
public MatrixView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public void setParentHeight(int height) {<pre name="code" class="java"> //传入ListView的高度
h = height;
}

@Override
protected void dispatchDraw(Canvas canvas) {
canvas.save();
int top = getTop();
float trans = calculatetrans(top,h);
Matrix m = canvas.getMatrix();
m.preTranslate(-2 / getWidth(), -2 / getHeight());
m.postTranslate(trans, 0);
canvas.concat(m);
super.dispatchDraw(canvas);
canvas.restore();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private float calculatetrans(int top,int h){
float result = 0f;
if(top <= h/2.349f){
result = (h/2f-top)/(h/7f)*fullTrans;
}else if(top > h/2.349f){
result = (top-h/3f)/(h/7f)*fullTrans;
}
return result;
}
}

以下大家能够自己写个demo測试一下啦,就是写一个ListView,然后用上面自己定义的MatrixLinearLayout 作为ListView的item的根布局,自己动手丰衣足食!