Skip to content

Commit 4e99228

Browse files
committed
Added feature to set TranslationDiff
1 parent 5b0b02b commit 4e99228

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

cardstackview/src/main/java/com/yuyakaido/android/cardstackview/CardStackView.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public CardStackView(Context context, AttributeSet attrs, int defStyle) {
9191
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CardStackView);
9292
setVisibleCount(array.getInt(R.styleable.CardStackView_visibleCount, option.visibleCount));
9393
setSwipeThreshold(array.getFloat(R.styleable.CardStackView_swipeThreshold, option.swipeThreshold));
94+
setTranslationDiff(array.getFloat(R.styleable.CardStackView_translationDiff, option.translationDiff));
9495
setStackFrom(StackFrom.values()[array.getInt(R.styleable.CardStackView_stackFrom, option.stackFrom.ordinal())]);
9596
setElevationEnabled(array.getBoolean(R.styleable.CardStackView_elevationEnabled, option.isElevationEnabled));
9697
setSwipeEnabled(array.getBoolean(R.styleable.CardStackView_swipeEnabled, option.isSwipeEnabled));
@@ -210,12 +211,12 @@ private void update(float percentX, float percentY) {
210211
ViewCompat.setScaleX(view, percent);
211212
ViewCompat.setScaleY(view, percent);
212213

213-
float currentTranslationY = i * 30;
214+
float currentTranslationY = i * Util.toPx(getContext(), option.translationDiff);
214215
if (option.stackFrom == StackFrom.Top) {
215216
currentTranslationY *= -1;
216217
}
217218

218-
float nextTranslationY = (i - 1) * 30;
219+
float nextTranslationY = (i - 1) * Util.toPx(getContext(), option.translationDiff);
219220
if (option.stackFrom == StackFrom.Top) {
220221
nextTranslationY *= -1;
221222
}
@@ -363,6 +364,13 @@ public void setSwipeThreshold(float swipeThreshold) {
363364
}
364365
}
365366

367+
public void setTranslationDiff(float translationDiff) {
368+
option.translationDiff = translationDiff;
369+
if (adapter != null) {
370+
initialize(false);
371+
}
372+
}
373+
366374
public void setStackFrom(StackFrom stackFrom) {
367375
option.stackFrom = stackFrom;
368376
if (adapter != null) {

cardstackview/src/main/java/com/yuyakaido/android/cardstackview/internal/CardStackOption.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class CardStackOption {
99
public int visibleCount = 3;
1010
public float swipeThreshold = 0.75f;
11+
public float translationDiff = 12f; // DP
1112
public StackFrom stackFrom = StackFrom.DEFAULT;
1213
public boolean isElevationEnabled = true;
1314
public boolean isSwipeEnabled = true;

cardstackview/src/main/java/com/yuyakaido/android/cardstackview/internal/Util.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.yuyakaido.android.cardstackview.internal;
22

3+
import android.content.Context;
34
import android.graphics.Point;
45

56
import com.yuyakaido.android.cardstackview.Quadrant;
@@ -8,6 +9,11 @@ public class Util {
89

910
private Util() {}
1011

12+
public static float toPx(Context context, float dp) {
13+
float scale = context.getResources().getDisplayMetrics().density;
14+
return dp * scale + 0.5f;
15+
}
16+
1117
public static double getRadian(float x1, float y1, float x2, float y2) {
1218
float width = x2 - x1;
1319
float height = y1 - y2;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<declare-styleable name="CardStackView">
55
<attr name="visibleCount" format="integer"/>
66
<attr name="swipeThreshold" format="float"/>
7+
<attr name="translationDiff" format="float"/>
78
<attr name="stackFrom" format="enum">
89
<enum name="bottom" value="0"/>
910
<enum name="top" value="1"/>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
android:clipToPadding="false"
1818
app:visibleCount="3"
1919
app:swipeThreshold="0.75"
20+
app:translationDiff="12"
2021
app:stackFrom="top"
2122
app:elevationEnabled="true"
2223
app:swipeEnabled="true"

0 commit comments

Comments
 (0)