Android圆弧形ListView的实现

本文带大家来实现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的根布局,自己动手丰衣足食!

发布了66 篇原创文章 · 获赞 13 · 访问量 10万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章