Skip to content

Commit b368180

Browse files
committed
2 parents 8b5ec50 + 5fc85fd commit b368180

6 files changed

Lines changed: 37 additions & 27 deletions

File tree

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
![效果图](/pic/demo20.gif)
1010

1111
### Use
12-
implementation 'com.uis:stacklayout:0.0.2'
12+
implementation 'com.uis:stacklayout:0.1.1'
1313

1414
*Name*| *Descript*|*Value*
1515
-----|--------|---
16-
stackSpace|间距|10dp
17-
stackEdge|边界距离|10dp
18-
stackZoomX|x方向缩放| 0.0-2.0
19-
stackZoomY|y方向缩放|0.0-0.2
16+
stackSpace|间距|默认值:10dp
17+
stackEdge|边界距离|默认值:10dp
18+
stackZoomX|x方向缩放| 0<x<=1,1表示等间距,默认值:1
19+
stackPadX|x方向偏移|表示偏移间距,默认值:0
20+
stackPadX|PadX*(Size-1) < Space|PadX优先级高于ZoomX
21+
stackZoomY|y方向缩放| 0<y<=1,1表示和顶层等高度,默认值:0.9
2022
stackLooper|自动轮播|false/true
2123
stackSize|层叠数量|3
2224
stackEdgeModel|层叠位置|left/right
23-
24-
2525

2626
```Xml
2727
<?xml version="1.0" encoding="utf-8"?>
@@ -88,7 +88,9 @@ stackEdgeModel|层叠位置|left/right
8888
*Version*| *Descript*|*Fixed*
8989
----|----|----
9090
0.0.1|自动轮播,滑动从顶部移除,整体上浮|初始版本
91-
0.0.2|滑动从顶层加入,整体下沉|内部view点击事件
91+
0.0.2|滑动从顶层加入,整体下沉|fixed 内部view点击事件
92+
0.1.0|zoomX,zoomY呈等比数列|更改属性
93+
0.1.1|只有一条数据时|fixed
9294

9395
### Thanks
9496

demo/src/main/res/layout/stack_left.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
android:layout_height="wrap_content"
77
stack:stackSpace="10dp"
88
stack:stackEdge="10dp"
9-
stack:stackZoomX="0.1"
10-
stack:stackZoomY="0.1"
9+
stack:stackZoomX="1"
10+
stack:stackZoomY="0.9"
1111
stack:stackLooper = "true"
1212
stack:stackSize = "5"
1313
stack:stackEdgeModel = "left">

demo/src/main/res/layout/stack_right.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
xmlns:stack="http://schemas.android.com/apk/res-auto"
44
android:id="@+id/stacklayout"
55
android:layout_width="match_parent"
6-
android:layout_height="wrap_content"
7-
stack:stackSpace="10dp"
6+
android:layout_height="150dp"
7+
stack:stackSpace="15dp"
88
stack:stackEdge="10dp"
9-
stack:stackZoomX="0.2"
10-
stack:stackZoomY="0.1"
9+
stack:stackZoomX="1"
10+
stack:stackPadX="5dp"
11+
stack:stackZoomY="0.9"
1112
stack:stackLooper = "false"
1213
stack:stackSize = "3"
1314
stack:stackEdgeModel = "right">

stacklayout/src/main/java/com/uis/stackview/StackHelper.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ void measureChild(int width,int height){
7070
float stackSpaces = 0;
7171
everyHeight = height - layout.getPaddingTop() - layout.getPaddingBottom();
7272
originX.add(layout.stackEdge);
73+
int padX = layout.stackPadX;
74+
if(padX*(size-1) > layout.stackSpace){
75+
padX = 0;
76+
}
7377
for (int i = 1; i < size; i++) {
74-
int stackSpace = (int)(layout.stackSpace /(1+layout.stackZoomX*(size-1-i)));
78+
int stackSpace = (int)((layout.stackSpace-padX*(size-1-i))*Math.pow(layout.stackZoomX,(size-1-i)));
7579
stackSpaces += stackSpace;
7680
originX.add(originX.get(i - 1) + stackSpace);
7781
}
@@ -97,7 +101,8 @@ void layoutChild(){
97101
if(needRelayout && layout != null) {
98102
needRelayout = false;
99103
int childSize = layout.getChildCount();
100-
int stackSize = layout.stackSize;
104+
int stackSize = layout.getRealStackSize();
105+
int realSize = layout.getRealStackSize();
101106
for (int i = 0; i < childSize; i++) {
102107
int top, bottom, left, right, pivot;
103108
View view = layout.getChildAt(i);
@@ -117,7 +122,7 @@ void layoutChild(){
117122
view.setPivotX(pivot);
118123
view.setPivotY(everyHeight / 2);
119124
view.layout(left, top, right, bottom);
120-
view.setScaleY(getChildScale(i, layout.stackZoomY));
125+
view.setScaleY((float) Math.pow(layout.stackZoomY,realSize-1-i));
121126
if(view.getTranslationX() != 0f){
122127
if(i+1 < stackSize){
123128
view.setTranslationX(0);
@@ -193,11 +198,6 @@ private void removeStackView(View view){
193198
}
194199
}
195200

196-
private float getChildScale(int index,float zoom) {
197-
int rate = layout.getRealStackSize() -1 - index;
198-
return (float) Math.pow(1.0f - zoom,rate);
199-
}
200-
201201
/**
202202
* 在StackLayout是否能水平滑动
203203
* @return true:拦截滑动,false:放行
@@ -301,9 +301,10 @@ void setAutoPlay(boolean looper){
301301
/** 加入底层 */
302302
private void addBottomView(){
303303
int cnt = layout.getAdapter().getItemCount();
304+
int stackSize = layout.getRealStackSize();
304305
displayPosition += 1;
305306
displayPosition %= cnt;
306-
int index = (layout.stackSize - 1 + displayPosition) % cnt;
307+
int index = (stackSize - 1 + displayPosition) % cnt;
307308
View view = getStackView();
308309
layout.getAdapter().onBindView(view, index);
309310
needRelayout = true;

stacklayout/src/main/java/com/uis/stackview/StackLayout.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ public class StackLayout extends ViewGroup{
2424
int stackSpace = 10;
2525
/** 层叠视图边距 */
2626
int stackEdge = 0;
27-
/** 层叠递减倍数 0.0-2.0*/
28-
float stackZoomX = 0.1f;
29-
/** 层叠缩放比例 0.0-0.2*/
30-
float stackZoomY = 0.1f;
27+
/** 层叠递减倍数 >0*/
28+
float stackZoomX = 1f;
29+
int stackPadX = 0;
30+
/** 层叠缩放比例 >0*/
31+
float stackZoomY = 0.9f;
3132
/** 层叠显示数量 */
3233
int stackSize = 3;
3334
/** 自动轮播 */
@@ -58,6 +59,7 @@ public StackLayout(Context context, AttributeSet attrs, int defStyleAttr) {
5859
stackSpace = (int)type.getDimension(R.styleable.StackLayout_stackSpace, stackSpace);
5960
stackEdge = (int)type.getDimension(R.styleable.StackLayout_stackEdge,stackEdge);
6061
stackZoomX = type.getFloat(R.styleable.StackLayout_stackZoomX, stackZoomX);
62+
stackPadX = (int)type.getDimension(R.styleable.StackLayout_stackPadX,stackPadX);
6163
stackZoomY = type.getFloat(R.styleable.StackLayout_stackZoomY, stackZoomY);
6264
stackSize = type.getInteger(R.styleable.StackLayout_stackSize,stackSize);
6365
stackLooper = type.getBoolean(R.styleable.StackLayout_stackLooper,stackLooper);
@@ -73,6 +75,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
7375
int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
7476
int height = MeasureSpec.EXACTLY != MeasureSpec.getMode(heightMeasureSpec) ? width/2
7577
: getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
78+
if(!isInEditMode() && (adapter == null || adapter.getItemCount() == 0)){
79+
height = 0;
80+
}
7681
setMeasuredDimension(width,height);
7782
if(adapter != null) {
7883
stackHelper.measureChild(width,height);

stacklayout/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<resources>
33
<declare-styleable name="StackLayout">
44
<attr name="stackSpace" format="dimension" />
5+
<attr name="stackPadX" format="dimension" />
56
<attr name="stackZoomX" format="float" />
67
<attr name="stackZoomY" format="float" />
78
<attr name="stackLooper" format="boolean"/>

0 commit comments

Comments
 (0)