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

Commit a3852e0

Browse files
authored
fix math bug & add AutoCompleteMode (#213)
* fix math bug & add AutoCompleteMode * fix comment & and new test * fixed Comment
1 parent c262a32 commit a3852e0

File tree

12 files changed

+311
-8
lines changed

12 files changed

+311
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,8 +1899,8 @@ public void touchAnimateTo(int touchUpMode, float position, float currentVelocit
18991899
} else if (touchUpMode == TOUCH_UP_COMPLETE_TO_END || touchUpMode == TOUCH_UP_NEVER_TO_START) {
19001900
position = 1;
19011901
}
1902-
float stiff = mScene.getSpringStiffiness();
1903-
if(Float.isNaN(stiff)) {
1902+
1903+
if (mScene.getAutoCompleteMode() == TouchResponse.COMPLETE_MODE_CONTINUOUS_VELOCITY) {
19041904
mStopLogic.config(mTransitionLastPosition, position, currentVelocity,
19051905
mTransitionDuration, mScene.getMaxAcceleration(), mScene.getMaxVelocity());
19061906
} else {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,12 @@ int getSpringBoundary() {
17011701
}
17021702
return 0;
17031703
}
1704-
1704+
int getAutoCompleteMode() {
1705+
if (mCurrentTransition != null && mCurrentTransition.mTouchResponse != null) {
1706+
return mCurrentTransition.mTouchResponse.getAutoCompleteMode();
1707+
}
1708+
return 0;
1709+
}
17051710
void setupTouch() {
17061711
if (mCurrentTransition != null && mCurrentTransition.mTouchResponse != null) {
17071712
mCurrentTransition.mTouchResponse.setupTouch();
@@ -1755,7 +1760,7 @@ private boolean hasCycleDependency(int key) {
17551760
}
17561761

17571762
/**
1758-
* Recursive decent of the deriveConstraintsFrom tree reading the motionLayout if
1763+
* Recursive descent of the deriveConstraintsFrom tree reading the motionLayout if
17591764
* needed.
17601765
*
17611766
* @param key

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public class OnSwipe {
3838
private float mSpringStiffness = Float.NaN;
3939
private float mSpringStopThreshold = Float.NaN;
4040
private int mSpringBoundary = 0;
41+
private int mAutoCompleteMode = 0;
42+
43+
public static final int COMPLETE_MODE_CONTINUOUS_VELOCITY = 0;
44+
public static final int COMPLETE_MODE_SPRING = 1;
45+
4146
public static final int SPRING_BOUNDARY_OVERSHOOT = 0;
4247
public static final int SPRING_BOUNDARY_BOUNCESTART = 1;
4348
public static final int SPRING_BOUNDARY_BOUNCEEND = 2;
@@ -383,4 +388,20 @@ public OnSwipe setSpringBoundary(int springBoundary) {
383388
mSpringBoundary = springBoundary;
384389
return this;
385390
}
391+
392+
public int getAutoCompleteMode() {
393+
return mAutoCompleteMode;
394+
}
395+
396+
397+
/**
398+
* sets the behaviour at the boundaries 0 and 1
399+
* COMPLETE_MODE_CONTINUOUS_VELOCITY = 0;
400+
* COMPLETE_MODE_SPRING = 1;
401+
*
402+
*/
403+
public void setAutoCompleteMode(int autoCompleteMode) {
404+
mAutoCompleteMode = autoCompleteMode;
405+
}
406+
386407
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ class TouchResponse {
109109
private float mSpringStiffness = Float.NaN;
110110
private float mSpringStopThreshold = Float.NaN;
111111
private int mSpringBoundary = 0;
112+
private int mAutoCompleteMode = COMPLETE_MODE_CONTINUOUS_VELOCITY;
113+
public static final int COMPLETE_MODE_CONTINUOUS_VELOCITY = 0;
114+
public static final int COMPLETE_MODE_SPRING = 1;
112115

113116
TouchResponse(Context context, MotionLayout layout, XmlPullParser parser) {
114117
mMotionLayout = layout;
@@ -146,6 +149,7 @@ public TouchResponse(MotionLayout layout, OnSwipe onSwipe) {
146149
mSpringMass= onSwipe.getSpringMass();
147150
mSpringStiffness= onSwipe.getSpringStiffness();
148151
mSpringStopThreshold= onSwipe.getSpringStopThreshold();
152+
mAutoCompleteMode = onSwipe.getAutoCompleteMode();
149153
}
150154

151155
public void setRTL(boolean rtl) {
@@ -225,7 +229,8 @@ private void fill(TypedArray a) {
225229
mSpringStopThreshold = a.getFloat(attr, mSpringStopThreshold);
226230
} else if (attr == R.styleable.OnSwipe_springBoundary) {
227231
mSpringBoundary = a.getInt(attr, mSpringBoundary);
228-
232+
} else if (attr == R.styleable.OnSwipe_autoCompleteMode) {
233+
mAutoCompleteMode = a.getInt(attr, mAutoCompleteMode);
229234
}
230235

231236
}
@@ -744,6 +749,24 @@ boolean getMoveWhenScrollAtTop() {
744749
return mMoveWhenScrollAtTop;
745750
}
746751

752+
/**
753+
* Get how the drag progress will return to the start or end state on touch up.
754+
* Can be ether COMPLETE_MODE_CONTINUOUS_VELOCITY (default) or COMPLETE_MODE_SPRING
755+
* @return
756+
*/
757+
public int getAutoCompleteMode() {
758+
return mAutoCompleteMode;
759+
}
760+
/**
761+
* set how the drag progress will return to the start or end state on touch up.
762+
*
763+
*
764+
* @return
765+
*/
766+
void setAutoCompleteMode(int autoCompleteMode) {
767+
mAutoCompleteMode = autoCompleteMode;
768+
}
769+
747770
/**
748771
* This calculates the bounds of the mTouchRegionId view.
749772
* This reuses rect for efficiency as this class will be called many times.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,10 @@
12881288
<declare-styleable name="OnSwipe">
12891289
<attr name="dragScale" format="float" />
12901290
<attr name="dragThreshold" format="float" />
1291+
<attr name="autoCompleteMode" format="enum" >
1292+
<enum name="continuousVelocity" value="0" />
1293+
<enum name="spring" value="1" />
1294+
</attr>
12911295
<attr name="maxVelocity" format="float" />
12921296
<attr name="maxAcceleration" format="float" />
12931297
<attr name="springMass" format="float"/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void compute(double dt) {
109109
double a = (-k * x - c * mV) / mMass;
110110
// This refinement of a simple coding of the acceleration increases accuracy
111111
double avgV = mV + a * dt / 2; // pass 1 calculate the average velocity
112-
double avgX = x + dt * (avgV) / 2 - mTargetPos;// pass 1 calculate the average pos
112+
double avgX = mPos + dt * (avgV) / 2 - mTargetPos;// pass 1 calculate the average pos
113113
a = (-avgX * k - avgV * c) / mMass; // calculate acceleration over that average pos
114114

115115
double dv = a * dt; // calculate change in velocity
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.support.constraint.app;
18+
19+
import android.content.Context;
20+
import android.os.Bundle;
21+
import android.util.Log;
22+
import android.view.View;
23+
24+
import androidx.annotation.Nullable;
25+
import androidx.appcompat.app.AppCompatActivity;
26+
import androidx.constraintlayout.motion.widget.Debug;
27+
import androidx.constraintlayout.motion.widget.MotionLayout;
28+
import androidx.constraintlayout.motion.widget.TransitionAdapter;
29+
30+
/**
31+
* Test transitionToState bug
32+
*/
33+
public class ButtonDriveAnimate extends AppCompatActivity {
34+
private static final String TAG = "CustomSwipeClick";
35+
String layout_name;
36+
MotionLayout mMotionLayout;
37+
38+
@Override
39+
protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
40+
super.onCreate(savedInstanceState);
41+
Bundle extra = getIntent().getExtras();
42+
String prelayout = extra.getString(Utils.KEY);
43+
layout_name = prelayout;
44+
Context ctx = getApplicationContext();
45+
int id = ctx.getResources().getIdentifier(prelayout, "layout", ctx.getPackageName());
46+
setContentView(id);
47+
mMotionLayout = Utils.findMotionLayout(this);
48+
mMotionLayout.setTransitionListener(new TransitionAdapter() {
49+
@Override
50+
public void onTransitionCompleted(MotionLayout motionLayout, int currentId) {
51+
Log.v(TAG, Debug.getLoc()+" ");
52+
}
53+
});
54+
}
55+
56+
public void goLeft(View v) {
57+
Log.v(TAG, Debug.getLoc()+" ");
58+
mMotionLayout.transitionToState(R.id.left);
59+
60+
}
61+
public void goRight(View v) {
62+
Log.v(TAG, Debug.getLoc()+" ");
63+
mMotionLayout.transitionToState(R.id.right);
64+
65+
66+
}
67+
}

projects/MotionLayoutVerification/app/src/main/java/android/support/constraint/app/VerificationActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class VerificationActivity extends AppCompatActivity implements View.OnCl
8888
activity_map.put("constraint_set_01", ConstraintSetVerify.class);
8989
activity_map.put("verification_042", CheckCallbackActivity.class);
9090
activity_map.put("verification_057", CheckSetStates.class);
91+
activity_map.put("verification_502", ButtonDriveAnimate.class);
9192

9293
// activity_map.put("verification_037", RotationToolbar.class);
9394
// activity_map.put("verification_038", RotationRotateToToolbar.class);
@@ -97,7 +98,7 @@ public class VerificationActivity extends AppCompatActivity implements View.OnCl
9798

9899
private static boolean REVERSE = false;
99100

100-
private final String RUN_FIRST = "verification_098";
101+
private final String RUN_FIRST = "verification_502";
101102
private final String LAYOUTS_MATCHES = "v.*_.*";
102103

103104
private static String SHOW_FIRST = "";
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.motion.widget.MotionLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:id="@+id/rootView"
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent"
9+
android:background="#638bdc"
10+
app:layoutDescription="@xml/verification_scene_502"
11+
tools:context=".ButtonDriveAnimate"
12+
>
13+
14+
<androidx.constraintlayout.utils.widget.ImageFilterView
15+
android:id="@+id/base"
16+
android:layout_width="200dp"
17+
android:layout_height="50dp"
18+
android:background="#9cb1f0"
19+
app:layout_constraintBottom_toBottomOf="parent"
20+
app:layout_constraintLeft_toLeftOf="parent"
21+
app:layout_constraintRight_toRightOf="parent"
22+
app:layout_constraintTop_toTopOf="parent"
23+
app:roundPercent="1" />
24+
25+
<androidx.constraintlayout.utils.widget.ImageFilterView
26+
android:id="@+id/undo"
27+
android:layout_width="wrap_content"
28+
android:layout_height="0dp"
29+
android:layout_marginLeft="16dp"
30+
android:src="@drawable/ic_baseline_undo_24"
31+
android:tint="#FFF"
32+
android:onClick="goLeft"
33+
app:layout_constraintBottom_toBottomOf="@+id/base"
34+
app:layout_constraintHorizontal_bias="0"
35+
app:layout_constraintLeft_toLeftOf="@+id/base"
36+
app:layout_constraintRight_toRightOf="@+id/base"
37+
app:layout_constraintTop_toTopOf="@+id/base" />
38+
39+
<androidx.constraintlayout.utils.widget.ImageFilterView
40+
android:id="@+id/redo"
41+
android:layout_width="wrap_content"
42+
android:layout_height="0dp"
43+
android:layout_marginRight="16dp"
44+
android:src="@drawable/ic_baseline_redo_24"
45+
android:tint="#FFF"
46+
android:onClick="goRight"
47+
app:layout_constraintBottom_toBottomOf="@+id/base"
48+
app:layout_constraintHorizontal_bias="1"
49+
app:layout_constraintLeft_toLeftOf="@+id/base"
50+
app:layout_constraintRight_toRightOf="@+id/base"
51+
app:layout_constraintTop_toTopOf="@+id/base" />
52+
53+
54+
<androidx.constraintlayout.utils.widget.ImageFilterView
55+
android:id="@+id/clock"
56+
android:layout_width="0dp"
57+
android:layout_height="0dp"
58+
android:background="#FFFF"
59+
app:layout_constraintBottom_toBottomOf="@+id/base"
60+
app:layout_constraintDimensionRatio="1:1"
61+
app:layout_constraintLeft_toLeftOf="@+id/base"
62+
app:layout_constraintRight_toRightOf="@+id/base"
63+
app:layout_constraintTop_toTopOf="@+id/base"
64+
app:roundPercent="1" />
65+
66+
<androidx.constraintlayout.utils.widget.ImageFilterView
67+
android:id="@+id/min"
68+
android:layout_width="3dp"
69+
android:layout_height="18dp"
70+
android:layout_marginTop="6dp"
71+
android:background="#7697ed"
72+
app:layout_constraintBottom_toBottomOf="@+id/clock"
73+
app:layout_constraintLeft_toLeftOf="@+id/clock"
74+
app:layout_constraintRight_toRightOf="@+id/clock"
75+
app:layout_constraintTop_toTopOf="@+id/clock"
76+
app:layout_constraintVertical_bias="0"
77+
app:roundPercent="1" />
78+
79+
<androidx.constraintlayout.utils.widget.ImageFilterView
80+
android:id="@+id/hour"
81+
android:layout_width="3dp"
82+
android:layout_height="10dp"
83+
android:layout_marginTop="15dp"
84+
android:background="#7697ed"
85+
app:layout_constraintBottom_toBottomOf="@+id/clock"
86+
app:layout_constraintLeft_toLeftOf="@+id/clock"
87+
app:layout_constraintRight_toRightOf="@+id/clock"
88+
app:layout_constraintTop_toTopOf="@+id/clock"
89+
app:layout_constraintVertical_bias="0"
90+
app:roundPercent="1" />
91+
92+
<TextView
93+
android:id="@+id/textView7"
94+
android:layout_width="0dp"
95+
android:layout_height="64dp"
96+
android:text="Test of spring"
97+
app:layout_constraintBottom_toBottomOf="parent"
98+
app:layout_constraintEnd_toEndOf="parent"
99+
app:layout_constraintStart_toStartOf="parent" />
100+
</androidx.constraintlayout.motion.widget.MotionLayout>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<OnSwipe
1111
motion:touchAnchorId="@id/ball"
1212
motion:dragDirection="dragRight"
13+
motion:autoCompleteMode="spring"
1314
motion:springDamping="40"
1415
motion:springMass="1"
1516
motion:springBoundary ="bounceBoth"
@@ -27,6 +28,7 @@
2728
motion:touchAnchorId="@id/ball"
2829
motion:dragDirection="dragDown"
2930
motion:springDamping="50"
31+
motion:autoCompleteMode="spring"
3032
motion:springMass="1"
3133
motion:springBoundary ="bounceBoth"
3234
motion:springStiffness="1500"/>
@@ -44,7 +46,8 @@
4446
motion:touchAnchorId="@id/ball"
4547
motion:dragDirection="dragRight"
4648
motion:springDamping="4"
47-
motion:springMass="1"
49+
motion:autoCompleteMode="spring"
50+
motion:springMass="2"
4851
motion:springBoundary ="bounceEnd"
4952
motion:springStiffness="200"/>
5053
<KeyFrameSet>

0 commit comments

Comments
 (0)