Skip to content

Commit af35c18

Browse files
committed
2 parents 0e3465e + 647ffc1 commit af35c18

5 files changed

Lines changed: 44 additions & 40 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<li>注释:此图解释参数意义,展示效果不太精确,图片真实宽度为**上层橙色**
1414

1515
### Use
16-
implementation 'com.uis:stacklayout:0.3.0'
16+
implementation 'com.uis:stacklayout:0.3.1'
1717

1818
*Name*| *Descript*|*Value*
1919
-----|--------|---
@@ -94,6 +94,7 @@ stackEdgeModel|层叠位置|left/right
9494
0.2.0|只有一个元素,不支持轮播和滑动|新增功能
9595
0.2.1|减少child层级,见child.measure()|新增功能
9696
0.3.0|增加联动效果(缩放+平移)|新增功能
97+
0.3.1|联动动画平滑过度|fixed
9798

9899
### License
99100

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ buildscript {
44

55
repositories {
66
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
7+
google()
78
jcenter()
8-
google()
99
}
1010

1111
dependencies {
@@ -18,8 +18,8 @@ buildscript {
1818
allprojects {
1919
repositories {
2020
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
21+
google()
2122
jcenter()
22-
google()
2323
}
2424
}
2525

demo/src/main/java/com/uis/stackview/demo/activity/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected void onCreate(Bundle savedInstanceState) {
8383
recyclerView.setLayoutManager(new LinearLayoutManager(this));
8484
recyclerView.setAdapter(new StackAdapter());
8585
//stackViewLayout.setStackLooper(true);
86-
stackViewLayout.setPosition(10);
86+
//stackViewLayout.setPosition(10);
8787
stackViewLayout.setAdapter(new StackLayout.StackAdapter() {
8888
@Override
8989
public View onCreateView(ViewGroup parent) {
@@ -103,7 +103,7 @@ public void onBindView(View view, int position) {
103103

104104
@Override
105105
public int getItemCount() {
106-
return dataList.size();
106+
return 5;//dataList.size();
107107
}
108108

109109
@Override
@@ -114,8 +114,8 @@ public void onItemDisplay(int position) {
114114
@Override
115115
public void onItemClicked(int position) {
116116
Log.e("xx","clicked = " + position);
117-
stackViewLayout.setStackLooper(false);
118-
stackViewLayout.setPosition(position+3);
117+
//stackViewLayout.setStackLooper(false);
118+
//stackViewLayout.setPosition(position+3);
119119
}
120120
});
121121
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<androidx.viewpager.widget.ViewPager
1212
android:id="@+id/viewPager"
1313
android:layout_width="match_parent"
14-
android:layout_height="150dp"
14+
android:layout_height="100dp"
1515
android:clipChildren="true"
1616
android:clipToPadding="false"
1717
android:paddingRight="80dp"

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

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,13 @@ void layoutChild(){
127127
}
128128
view.setPivotX(pivot);
129129
view.setPivotY(everyHeight/2f);
130-
if(mAnimator.isRunning() && i > 0){
131-
132-
}else{
133-
view.layout(left, top, right, bottom);
134-
view.setScaleY((float) Math.pow(layout.stackZoomY,stackSize-1-i));
130+
if(!mAnimator.isRunning() || 0 == i){
131+
float scale = (float) Math.pow(layout.stackZoomY,stackSize-1-i);
132+
view.setScaleY(scale);
135133
view.setTranslationX(0);
134+
view.layout(left, top, right, bottom);
136135
}
137-
}else if(!isTopRemove){//顶层加入
136+
}else if(!isTopRemove && !mAnimator.isRunning()){//顶层加入
138137
if (layout.stackEdgeModel == MODEL_LEFT) {
139138
left = layout.getWidth() - layout.stackEdge;
140139
right = everyWidth + left;
@@ -191,6 +190,8 @@ private View getStackView(){
191190
item = weakViews.removeLast();
192191
view = item.get();
193192
if(view != null){
193+
view.setTranslationX(0);
194+
view.setScaleY(1f);
194195
break;
195196
}
196197
}
@@ -205,8 +206,6 @@ private View getStackView(){
205206
private void removeStackView(View view){
206207
if(view != null && layout != null) {
207208
layout.removeView(view);
208-
view.setTranslationX(0);
209-
view.setScaleY(1f);
210209
weakViews.add(new WeakReference<>(view));
211210
}
212211
}
@@ -227,7 +226,7 @@ boolean canScroll(float downX, float downY, float x, float y){
227226
// 垂直滑动
228227
}
229228
}
230-
return enableScroll;
229+
return enableScroll && !mAnimator.isRunning();
231230
}
232231

233232
void fingerTouchDown(){
@@ -261,19 +260,23 @@ void executeScroll(int dx) {
261260
}
262261

263262
void scaleTransChild(int dx){
264-
int first = layout.getChildCount()>layout.stackSize ? 1 : 0;
265-
int size = layout.getChildCount()-1;
266-
int stackSize = layout.stackSize;
267-
for(int i = first; i<size; i++){
263+
int cnt = layout.getChildCount();
264+
int stackSize = layout.getRealStackSize();
265+
int size = Math.min(cnt,stackSize)-(cnt==stackSize ? 1:0);
266+
int first = cnt > stackSize ? 1 : 0;
267+
int index = 0;
268+
for (int i = first; i < size; i++) {
268269
View v = layout.getChildAt(i);
269270
int sign = MODEL_LEFT == layout.stackEdgeModel ? 1 : -1;
270-
float rate = 1f*dx/layout.getWidth();
271-
int tx = originX.get(i-first+1)-originX.get(i-first);
272-
v.setTranslationX(v.getTranslationX() + rate*tx);
273-
274-
double scaley = Math.pow(layout.stackZoomY,stackSize-2-i);
275-
double scale = Math.pow(layout.stackZoomY,stackSize-1-i);
276-
v.setScaleY(v.getScaleY()+(float)(sign*rate*(scaley-scale)));
271+
float rate = 1f * dx / layout.getWidth();
272+
int indexX = index;
273+
int tx = originX.get( indexX+1) - originX.get(indexX);
274+
v.setTranslationX(v.getTranslationX() + rate * tx);
275+
int indexY = size - index;
276+
double scaley = Math.pow(layout.stackZoomY, indexY - 1);
277+
double scale = Math.pow(layout.stackZoomY, indexY);
278+
v.setScaleY(v.getScaleY() + (float) (sign * rate * (scaley - scale)));
279+
index++;
277280
}
278281
}
279282

@@ -340,8 +343,8 @@ private void addBottomView(){
340343
int index = (stackSize - 1 + displayPosition) % cnt;
341344
View view = getStackView();
342345
layout.getAdapter().onBindView(view, index);
343-
needRelayout = true;
344346
layout.addView(view, 0);
347+
needRelayout = true;
345348
layout.getAdapter().onItemDisplay(displayPosition);
346349
}
347350

@@ -354,8 +357,8 @@ private View addTopView(){
354357
}
355358
View view = getStackView();
356359
layout.getAdapter().onBindView(view, position);
357-
needRelayout = true;
358360
layout.addView(view);
361+
needRelayout = true;
359362
return view;
360363
}
361364

@@ -366,7 +369,6 @@ private void removeBottomView(){
366369
if(displayPosition < 0){
367370
displayPosition += cnt;
368371
}
369-
//needRelayout = true;
370372
View view = layout.getChildAt(0);
371373
removeStackView(view);
372374
layout.getAdapter().onItemDisplay(displayPosition);
@@ -381,9 +383,8 @@ public void onAnimationUpdate(ValueAnimator animation) {
381383
scaleTransChild((int)(mAnimator.getTranslationX()-lastValue));
382384
if (fraction >= 1.0f) {
383385
mAnimator.endAnimator(this);
384-
needRelayout = true;
385386
layout.requestLayout();
386-
387+
needRelayout = true;
387388
} else if(fraction > 0.2 && mAnimator.needAddBottomView()){
388389
addBottomView();
389390
}
@@ -437,13 +438,15 @@ void setTranslationX(float value){
437438
}
438439

439440
void startAnimator(boolean needRemoveTop,boolean needAddBottom,float transX, ValueAnimator.AnimatorUpdateListener listener){
440-
int mills = needAddBottom ? duration : (int)(1.2*duration*Math.abs(transX)/mWidth);
441-
needAddBottomView = needAddBottom;
442-
needRemoveTopView = needRemoveTop;
443-
animator = ValueAnimator.ofFloat(0f, transX).setDuration(mills);
444-
animator.setInterpolator(needAddBottom ? interpolatorAuto : interpolator);
445-
animator.addUpdateListener(listener);
446-
animator.start();
441+
if(animator == null) {
442+
int mills = needAddBottom ? duration : (int) (1.2 * duration * Math.abs(transX) / mWidth);
443+
needAddBottomView = needAddBottom;
444+
needRemoveTopView = needRemoveTop;
445+
animator = ValueAnimator.ofFloat(0f, transX).setDuration(mills);
446+
animator.setInterpolator(needAddBottom ? interpolatorAuto : interpolator);
447+
animator.addUpdateListener(listener);
448+
animator.start();
449+
}
447450
}
448451

449452
void cancelAnimator(){

0 commit comments

Comments
 (0)