Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 7d8eae1

Browse files
authored
small changing to spring (#201)
1 parent 4c88f4d commit 7d8eae1

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/motion/widget/TouchResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class TouchResponse {
104104
static final int FLAG_DISABLE_POST_SCROLL = 1;
105105
static final int FLAG_DISABLE_SCROLL = 2;
106106
private float mDragThreshold = 10;
107-
private float mSpringDamping = Float.NaN;
107+
private float mSpringDamping = 10;
108108
private float mSpringMass = 1;
109109
private float mSpringStiffness = Float.NaN;
110110
private float mSpringStopThreshold = Float.NaN;

constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/utils/widget/MotionButton.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* An ImageButton that can display, combine and filter images. <b>Added in 2.0</b>
3838
* <p>
39-
* Subclass of AppCompatImageButton to handle rounding edges dynamically.
39+
* Subclass of AppCompatButton to handle rounding edges dynamically.
4040
* </p>
4141
* <h2>MotionButton attributes</h2>
4242
* <td>round</td>

constraintlayout/core/src/main/java/androidx/constraintlayout/core/motion/utils/SpringStopEngine.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,17 @@ public boolean isStopped() {
9898

9999
private void compute(double dt) {
100100
double x = (mPos - mTargetPos);
101-
double a = getAcceleration();
102-
double dv = a * dt;
103-
double avgV = mV + dv / 2;
104-
mV += dv / 2;
101+
double k = mStiffness;
102+
double c = mDamping;
103+
double a = (-k * x - c * mV) / mMass;
104+
// This refinement of a simple coding of the acceleration increases accuracy
105+
double avgV = mV + a * dt / 2; // pass 1 calculate the average velocity
106+
double avgX = x + dt * (avgV) / 2 - mTargetPos;// pass 1 calculate the average pos
107+
a = (-avgX * k - avgV * c) / mMass; // calculate acceleration over that average pos
108+
109+
double dv = a * dt; // calculate change in velocity
110+
avgV = mV + dv / 2; // average velocity is current + half change
111+
mV += dv;
105112
mPos += avgV * dt;
106113
if (mBoundaryMode > 0) {
107114
if (mPos < 0 && ((mBoundaryMode & 1) == 1)) {
@@ -114,4 +121,5 @@ private void compute(double dt) {
114121
}
115122
}
116123
}
124+
117125
}

projects/MotionLayoutVerification/app/src/main/res/xml/verification_scene_501.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
<OnSwipe
1111
motion:touchAnchorId="@+id/clock"
1212
motion:dragDirection="dragLeft"
13-
motion:springDamping="10"
1413
motion:onTouchUp="neverCompleteToEnd"
15-
motion:springMass="1"
1614
motion:springBoundary="overshoot"
1715
motion:springStiffness="500" />
1816
<KeyFrameSet></KeyFrameSet>
@@ -27,7 +25,7 @@
2725
<OnSwipe
2826
motion:touchAnchorId="@+id/clock"
2927
motion:dragDirection="dragRight"
30-
motion:springDamping="10"
28+
motion:springDamping="0.2"
3129
motion:springMass="1"
3230
motion:springBoundary="overshoot"
3331
motion:onTouchUp="neverCompleteToEnd"

0 commit comments

Comments
 (0)