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

Sping #199

Merged
merged 9 commits into from
Mar 31, 2021
Merged

Sping #199

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
resolve comments
  • Loading branch information
jafu888 committed Mar 31, 2021
commit d067721112df75b7dc2c66c0c2bf522ae66979dd
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class StopLogic extends MotionInterpolator {
private StopLogicEngine mStopLogicEngine = new StopLogicEngine();
private SpringStopEngine mSpringStopEngine;
private StopEngine engine = mStopLogicEngine;
private StopEngine mEngine = mStopLogicEngine;

/**
* Debugging logic to log the state.
Expand All @@ -43,39 +43,56 @@ public class StopLogic extends MotionInterpolator {
*/

public String debug(String desc, float time) {
return engine.debug(desc, time);
return mEngine.debug(desc, time);
}

public float getVelocity(float x) {
return engine.getVelocity(x);
return mEngine.getVelocity(x);
}

public void config(float currentPos, float destination, float currentVelocity, float maxTime, float maxAcceleration, float maxVelocity) {
engine = mStopLogicEngine;
mEngine = mStopLogicEngine;
mStopLogicEngine.config(currentPos, destination, currentVelocity, maxTime, maxAcceleration, maxVelocity);
}

/**
* This configure the stop logic to be a spring.
* Moving from currentPosition(P0) to destination with an initial velocity of currentVelocity (V0)
* moving as if it has a mass (m) with spring constant stiffness(k), and friction(c)
* It moves with the equation acceleration a = (-k.x-c.v)/m.
* x = current position - destination
* v is velocity
*
* @param currentPos The current position
* @param destination The destination position
* @param currentVelocity the initial velocity
* @param mass the mass
* @param stiffness the stiffness or spring constant (the force by which the spring pulls)
* @param damping the stiffness or spring constant. (the resistance to the motion)
* @param stopThreshold (When the max velocity of the movement is below this it stops)
* @param boundaryMode This will allow you to control if it overshoots or bounces when it hits 0 and 1
*/
public void springConfig(float currentPos, float destination, float currentVelocity,
float mass, float stiffness, float damping, float stopThreshold,
int boundaryMode) {
if (mSpringStopEngine == null) {
mSpringStopEngine = new SpringStopEngine();
}
engine = mSpringStopEngine;
mEngine = mSpringStopEngine;
mSpringStopEngine.springConfig(currentPos, destination, currentVelocity, mass, stiffness, damping, stopThreshold, boundaryMode);
}

@Override
public float getInterpolation(float v) {
return engine.getInterpolation(v);
return mEngine.getInterpolation(v);
}

@Override
public float getVelocity() {
return engine.getVelocity();
return mEngine.getVelocity();
}

public boolean isStopped() {
return engine.isStopped();
return mEngine.isStopped();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,14 @@ public void touchAnimateTo(int touchUpMode, float position, float currentVelocit
mAnimationStartTime = getNanoTime();
invalidate();
}

/**
* Allows you to use trigger spring motion touch behaviour.
* You must have configured all the spring parameters in the Transition's OnSwipe
*
* @param position the position 0 - 1
* @param currentVelocity the current velocity rate of change in position per second
*/
public void touchSpringTo(float position, float currentVelocity) {
if (DEBUG) {
Log.v(TAG, " " + Debug.getLocation() + " touchAnimateTo " + position + " " + currentVelocity);
Expand Down Expand Up @@ -4067,10 +4075,6 @@ public boolean onTouchEvent(MotionEvent event) {
}
return super.onTouchEvent(event);
}
boolean mTesting = true;
public void setupForTest(){
onAttachedToWindow();
}

@Override
protected void onAttachedToWindow() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ public float getSpringDamping() {
* Set the damping of the spring if using spring.
* c in "a = (-k*x-c*v)/m" equation for the acceleration of a spring
*
* @param mSpringDamping
* @param springDamping
* @return this
*/
public OnSwipe setSpringDamping(float mSpringDamping) {
this.mSpringDamping = mSpringDamping;
public OnSwipe setSpringDamping(float springDamping) {
mSpringDamping = springDamping;
return this;
}

Expand All @@ -307,11 +307,11 @@ public float getSpringMass() {
* Set the Mass of the spring if using spring.
* m in "a = (-k*x-c*v)/m" equation for the acceleration of a spring
*
* @param mSpringMass
* @param springMass
* @return this
*/
public OnSwipe setSpringMass(float mSpringMass) {
this.mSpringMass = mSpringMass;
public OnSwipe setSpringMass(float springMass) {
mSpringMass = springMass;
return this;
}

Expand All @@ -334,7 +334,7 @@ public float getSpringStiffness() {
* @return
*/
public OnSwipe setSpringStiffness(float springStiffness) {
this.mSpringStiffness = springStiffness;
mSpringStiffness = springStiffness;
return this;
}

Expand All @@ -352,11 +352,11 @@ public float getSpringStopThreshold() {
* This is in change in progress / second
* If the spring will never go above that threshold again it will stop.
*
* @param mSpringStopThreshold
* @param springStopThreshold
* @return
*/
public OnSwipe setSpringStopThreshold(float mSpringStopThreshold) {
this.mSpringStopThreshold = mSpringStopThreshold;
public OnSwipe setSpringStopThreshold(float springStopThreshold) {
mSpringStopThreshold = springStopThreshold;
return this;
}

Expand All @@ -376,11 +376,11 @@ public int getSpringBoundary() {
* SPRING_BOUNDARY_BOUNCE_END = 2;
* SPRING_BOUNDARY_BOUNCE_BOTH = 3;
*
* @param mSpringBoundary
* @param springBoundary
* @return
*/
public OnSwipe setSpringBoundary(int mSpringBoundary) {
this.mSpringBoundary = mSpringBoundary;
public OnSwipe setSpringBoundary(int springBoundary) {
mSpringBoundary = springBoundary;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ private void init(Context context, AttributeSet attrs) {

mLayers[0] = mDrawable = getDrawable().mutate();
mLayers[1] = mAltDrawable.mutate();
Log.v("ImageFilterButton", Debug.getLoc()+" mDrawable = "+mDrawable);
Log.v("ImageFilterButton", Debug.getLoc()+" mAltDrawable = "+mAltDrawable);

mLayer = new LayerDrawable(mLayers);
mLayer.getDrawable(1).setAlpha((int) (255 * (mCrossfade)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public class CheckSetStates extends AppCompatActivity {
private static final String TAG = "CustomSwipeClick";
String layout_name;
MotionLayout mMotionLayout;
int []states = {R.id.s1,R.id.s2,R.id.s3,R.id.s4,R.id.s5,R.id.s6,R.id.s7,R.id.s8,R.id.s9};
int[] states = {R.id.s1, R.id.s2, R.id.s3, R.id.s4, R.id.s5, R.id.s6, R.id.s7, R.id.s8, R.id.s9};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getIntent().getExtras();
String prelayout = extra.getString(Utils.KEY);
Expand All @@ -47,39 +47,41 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

TextView text = findViewById(R.id.text);
}
int getNextState() {
int state = 0;
int cstate = mMotionLayout.getCurrentState();
Log.v(TAG, Debug.getLoc() + " current " + Debug.getName(getApplicationContext(),cstate));
for (int i = 0; i < states.length; i++) {
if (cstate == states[i]) {
state = i + 1;
return state % states.length;

}
}
return state;
}
int getNextState() {
int state = 0;
int cstate = mMotionLayout.getCurrentState();
Log.v(TAG, Debug.getLoc() + " current " + Debug.getName(getApplicationContext(), cstate));
for (int i = 0; i < states.length; i++) {
if (cstate == states[i]) {
state = i + 1;
return state % states.length;

}
}
return state;
}

public void jump1(View view) {
int state = getNextState();
mMotionLayout.transitionToState(states[state]);
Log.v(TAG, Debug.getLoc()+" "+state );
Log.v(TAG, Debug.getLoc() + " " + state);

}

public void jump2(View view) {
int state = getNextState();
int cstate = mMotionLayout.getCurrentState();
int end = mMotionLayout.getEndState();
int start = mMotionLayout.getStartState();
Log.v(TAG, Debug.getLoc() + "set " + Debug.getName(getApplicationContext(),start) +
" -> " + Debug.getName(getApplicationContext(),end) +
" now " + Debug.getName(getApplicationContext(),cstate) );
Log.v(TAG, Debug.getLoc() + "set " + Debug.getName(getApplicationContext(), start) +
" -> " + Debug.getName(getApplicationContext(), end) +
" now " + Debug.getName(getApplicationContext(), cstate));
//mMotionLayout.transitionToState(states[state], 1);
mMotionLayout.jumpToState(states[state]);
cstate = mMotionLayout.getCurrentState();
Log.v(TAG, Debug.getLoc() + "set " + Debug.getName(getApplicationContext(),states[state]) +
" = " + Debug.getName(getApplicationContext(),cstate));
cstate = mMotionLayout.getCurrentState();
Log.v(TAG, Debug.getLoc() + "set " + Debug.getName(getApplicationContext(), states[state]) +
" = " + Debug.getName(getApplicationContext(), cstate));

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 The Android Open Source Project
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down