本文带大家来实现ListView的圆弧形的分布排列,原理很简单,就是根据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的根布局,自己动手丰衣足食!
Android圆弧形ListView的实现
时间: 2024-10-04 15:35:07