Android開源:CustomLayoutManager - 實現各種炫酷效果
效果圖
圓弧
縮放
圓弧+縮放
畫廊
用法
代碼已上傳Jcenter,在build.gradle中添加
compile 'rouchuan.customlayoutmanager:customlayoutmanager:1.0.1'
然后新建一個自己的layoutManager并繼承CustomLayoutManager,在這里先提一下,CustomLayoutManager有幾個默認的屬性是你可以直接使用的。
protected Context context;
//子view的寬度
protected int mDecoratedChildWidth;
//子view的高度
protected int mDecoratedChildHeight;
//子view距離屏幕最左的偏移,也可以理解為第一個子view在初始狀態下距離屏幕左側的位移,默認居中
protected int startLeft;
//子view距離屏幕頂部的位移,默認居中
protected int startTop;
//主要隨滑動所改變的屬性的偏移量,考慮到view的屬性有int,有float所以這邊統一用float表示偏移
protected float offset;
//相鄰兩個子view間,主要隨滑動而改變的屬性的差值(比如隨滑動改變的是view的角度,那么這個值就是各個view之間的角度間隔)
protected float interval;
繼承CustomLayoutManager之后你必須實現3個方法
public class MyLayoutManager extends CustomLayoutManager{
//默認isClockWise為true
public MyLayoutManager(Context context) {
super(context);
}
//isClockWise為true時從左往右排列,不然則從右往左排列
public MyLayoutManager(Context context, boolean isClockWise) {
super(context, isClockWise);
}
//這個方法會設置默認的interval變量,之后可以直接使用interval
@Override
protected float setInterval() {
return 0;
}
//初始化方法,你可以在這里初始化自己的一些參數,比如實現圓弧布局的半徑,或是更改一些默認的屬性,比如startLeft,startTop
@Override
protected void setUp() {
}
//itemView就是每一個子view,targetOffset就是其對應的將要改變到的屬性值,你可以在這里根據targetOffset對子view的一些屬性進行設置
@Override
protected void setItemViewProperty(View itemView, float targetOffset) {
}
}</code></pre>
此外還有6個你可以選擇重寫的方法
//當子view的屬性超過這個值時,就會被回收掉
@Override
protected float maxRemoveOffset() {
return getHorizontalSpace() - startLeft;
}
//當子view的屬性小于這個值時,就會被回收掉
@Override
protected float minRemoveOffset() {
return -mDecoratedChildWidth-getPaddingLeft() - startLeft;
}
//當view的屬性等于targetOffset時,此view基于初始位置的x坐標,一般返回targetOffset
@Override
protected int calItemLeftPosition(float targetOffset) {
return targetOffset;
}
//當view的屬性等于targetOffset時,此view基于初始位置的y坐標,一般返回0
@Override
protected int calItemTopPosition(float targetOffset) {
return 0;
}
//這邊返回在滾動時子view主要改變的屬性的值
@Override
protected float propertyChangeWhenScroll(View itemView) {
return itemView.getLeft()-startLeft;
}
//滑動產生的偏移dx與offset的比例,默認為1
protected float getDistanceRatio(){
return 1f;
}</code></pre>