Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support downUp ViewTranstions #190

Merged
merged 2 commits into from Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 0 additions & 116 deletions constraintlayout/.idea/codeStyles/Project.xml

This file was deleted.

Expand Up @@ -19,6 +19,7 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
Expand Down Expand Up @@ -69,8 +70,9 @@ public class ViewTransition {
// Transition can be up or down of manually fired
public static final int ONSTATE_ACTION_DOWN = 1;
public static final int ONSTATE_ACTION_UP = 2;
public static final int ONSTATE_SHARED_VALUE_SET = 3;
public static final int ONSTATE_SHARED_VALUE_UNSET = 4;
public static final int ONSTATE_ACTION_DOWN_UP = 3;
public static final int ONSTATE_SHARED_VALUE_SET = 4;
public static final int ONSTATE_SHARED_VALUE_UNSET = 5;

private int mOnStateTransition = UNSET;
private boolean mDisabled = false;
Expand All @@ -82,6 +84,8 @@ public class ViewTransition {
KeyFrames mKeyFrames;
ConstraintSet.Constraint mConstraintDelta;
private int mDuration = UNSET;
private int mUpDuration = UNSET;

private int mTargetId;
private String mTargetString;

Expand Down Expand Up @@ -140,6 +144,7 @@ public void setStateTransition(int stateTransition) {

/**
* Gets the SharedValue it will be listening for.
*
* @return
*/
public int getSharedValue() {
Expand All @@ -155,6 +160,7 @@ public void setSharedValue(int sharedValue) {

/**
* Gets the ID of the SharedValue it will be listening for.
*
* @return the id of the shared value
*/
public int getSharedValueID() {
Expand Down Expand Up @@ -281,6 +287,8 @@ private void parseViewTransitionTags(Context context, XmlPullParser parser) {
mPathMotionArc = a.getInt(attr, mPathMotionArc);
} else if (attr == R.styleable.ViewTransition_duration) {
mDuration = a.getInt(attr, mDuration);
} else if (attr == R.styleable.ViewTransition_upDuration) {
mUpDuration = a.getInt(attr, mUpDuration);
} else if (attr == R.styleable.ViewTransition_viewTransitionMode) {
mViewTransitionMode = a.getInt(attr, mViewTransitionMode);
} else if (attr == R.styleable.ViewTransition_motionInterpolator) {
Expand Down Expand Up @@ -309,21 +317,23 @@ private void parseViewTransitionTags(Context context, XmlPullParser parser) {
mIfTagSet = a.getResourceId(attr, mIfTagSet);
} else if (attr == R.styleable.ViewTransition_ifTagNotSet) {
mIfTagNotSet = a.getResourceId(attr, mIfTagNotSet);
}else if (attr == R.styleable.ViewTransition_SharedValueId) {
} else if (attr == R.styleable.ViewTransition_SharedValueId) {
mSharedValueID = a.getResourceId(attr, mSharedValueID);
}else if (attr == R.styleable.ViewTransition_SharedValue) {
} else if (attr == R.styleable.ViewTransition_SharedValue) {
mSharedValueTarget = a.getInteger(attr, mSharedValueTarget);
}
}
a.recycle();
}

void applyIndependentTransition(ViewTransitionController controller, MotionLayout motionLayout,View view) {
void applyIndependentTransition(ViewTransitionController controller, MotionLayout motionLayout, View view) {
MotionController motionController = new MotionController(view);
motionController.setBothStates(view);
mKeyFrames.addAllFrames(motionController);
motionController.setup(motionLayout.getWidth(), motionLayout.getHeight(), mDuration, System.nanoTime());
new Animate(controller, motionController, mDuration, getInterpolator(motionLayout.getContext()), mSetsTag, mClearsTag);
new Animate(controller, motionController,
mDuration, mUpDuration, mOnStateTransition,
getInterpolator(motionLayout.getContext()), mSetsTag, mClearsTag);
}

static class Animate {
Expand All @@ -332,35 +342,69 @@ static class Animate {
long mStart;
MotionController mMC;
int mDuration;
int mUpDuration;
KeyCache mCache = new KeyCache();
ViewTransitionController mVtController;
Interpolator mInterpolator;
boolean reverse = false;
float mPosition;
float mDpositionDt;
long mLastRender;
Rect mTempRec = new Rect();
boolean hold_at_100 = false;

Animate(ViewTransitionController controller,
MotionController motionController,
int duration,
int duration, int upDuration, int mode,
Interpolator interpolator, int setTag, int clearTag) {
mVtController = controller;
mMC = motionController;
mDuration = duration;
mUpDuration = upDuration;
mStart = System.nanoTime();
mLastRender = mStart;
mVtController.addAnimation(this);
mInterpolator = interpolator;
mSetsTag = setTag;
mClearsTag = clearTag;
if (mode == ONSTATE_ACTION_DOWN_UP) {
hold_at_100 = true;
}
mDpositionDt = (duration == 0) ? Float.MAX_VALUE : 1f / duration;
mutate();
}

void reverse(boolean dir) {
reverse = dir;
if (reverse && mUpDuration != UNSET) {
mDpositionDt = (mUpDuration == 0) ? Float.MAX_VALUE : 1f / mUpDuration;
}
mVtController.invalidate();
mLastRender = System.nanoTime();
}

void mutate() {
if (reverse) {
mutateReverse();
} else {
mutateForward();
}
}

void mutateReverse() {
long current = System.nanoTime();
long elapse = current - mStart;
float position = ((float) (elapse * 1E-6)) / mDuration;
if (position >= 1.0f) {
position = 1.0f;
long elapse = current - mLastRender;
mLastRender = current;

mPosition -= ((float) (elapse * 1E-6)) * mDpositionDt;
if (mPosition < 0.0f) {
mPosition = 0.0f;
}
float ipos = (mInterpolator == null) ? position : mInterpolator.getInterpolation(position);

float ipos = (mInterpolator == null) ? mPosition : mInterpolator.getInterpolation(mPosition);
boolean repaint = mMC.interpolate(mMC.mView, ipos, current, mCache);
if (position >= 1) {

if (mPosition <= 0) {
if (mSetsTag != UNSET) {
mMC.getView().setTag(mSetsTag, System.nanoTime());
}
Expand All @@ -369,10 +413,58 @@ void mutate() {
}
mVtController.removeAnimation(this);
}
if (position < 1f || repaint) {
if (mPosition > 0f || repaint) {
mVtController.invalidate();
}
}

void mutateForward() {

long current = System.nanoTime();
long elapse = current - mLastRender;
mLastRender = current;

mPosition += ((float) (elapse * 1E-6)) * mDpositionDt;
if (mPosition >= 1.0f) {
mPosition = 1.0f;
}

float ipos = (mInterpolator == null) ? mPosition : mInterpolator.getInterpolation(mPosition);
boolean repaint = mMC.interpolate(mMC.mView, ipos, current, mCache);


if (mPosition >= 1) {
if (mSetsTag != UNSET) {
mMC.getView().setTag(mSetsTag, System.nanoTime());
}
if (mClearsTag != UNSET) {
mMC.getView().setTag(mClearsTag, null);
}
if (!hold_at_100) {
mVtController.removeAnimation(this);
}
}
if (mPosition < 1f || repaint) {
mVtController.invalidate();
}
}

public void reactTo(int action, float x, float y) {
switch (action) {
case MotionEvent.ACTION_UP:
if (!reverse) {
reverse(true);
}
return;
case MotionEvent.ACTION_MOVE:
View view = mMC.getView();
view.getHitRect(mTempRec);
if (!mTempRec.contains((int) x, (int) y)) {
if (!reverse)
reverse(true);
}
}
}
}

void applyTransition(ViewTransitionController controller,
Expand Down Expand Up @@ -496,6 +588,9 @@ boolean supports(int action) {
if (mOnStateTransition == ONSTATE_ACTION_UP) {
return action == MotionEvent.ACTION_UP;
}
if (mOnStateTransition == ONSTATE_ACTION_DOWN_UP) {
return action == MotionEvent.ACTION_DOWN;
}
return false;
}

Expand Down
Expand Up @@ -160,6 +160,11 @@ void touchEvent(MotionEvent event) {
float y = event.getY();
Rect rec = new Rect();
int action = event.getAction();
if (animations != null && !animations.isEmpty()) {
for (ViewTransition.Animate animation : animations) {
animation.reactTo(action,x,y);
}
}
switch (action) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_DOWN:
Expand Down
Expand Up @@ -1730,8 +1730,9 @@
<attr name="onStateTransition" format="enum">
<enum name="actionDown" value="1" />
<enum name="actionUp" value="2" />
<enum name="sharedValueSet" value="3" />
<enum name="sharedValueUnset" value="4" />
<enum name="actionDownUp" value="3" />
<enum name="sharedValueSet" value="4" />
<enum name="sharedValueUnset" value="5" />
</attr>

<attr name="viewTransitionMode" format="enum">
Expand All @@ -1747,6 +1748,7 @@
<attr name="viewTransitionMode" />
<attr name="onStateTransition" />
<attr name="duration" />
<attr name="upDuration" format="integer" />
<attr name="transitionDisable" />
<attr name="pathMotionArc" />
<attr name="motionInterpolator" />
Expand Down
Expand Up @@ -35,6 +35,7 @@
android:label="Test ConstraintSet on ConstraintLayout"
/>
<activity android:name=".CheckCallbackActivity" />
<activity android:name=".CheckSetStates" />

</application>

Expand Down