Skip to content

Commit

Permalink
Fixed shared element transition. The onMeasure() function was fitting…
Browse files Browse the repository at this point in the history
… the image to the view by adding a runnable to the message queue. It did this to avoid calculating the image size multiple times as onMeasure() is typically called multiple times. However, when using a shared element transition, the snapshot was always taken before the message queue reached this runnable, so at the time of snapshot, the image size was always 0. This caused the image to shrink until it was gone, and then reappear after a new touch event. I overrode onSizeChanged(), which immediately after the last onMeasure() is called, and moved the content of the runnable to this function. It now works perfectly. Fingers crossed.
  • Loading branch information
TrevinAvery committed Dec 14, 2018
1 parent 02f7316 commit b7d8dc2
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions touchview/src/main/java/com/ortiz/touchview/TouchImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Set view dimensions
//
setMeasuredDimension(width, height);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

//
// Fit content within view.
Expand All @@ -662,25 +667,18 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// widthMeasureSpec == "EXACTLY 1404", then back and forth multiple times in quick
// succession, as the ConstraintLayout tries to solve its constraints.
//
// So we make all changes to class members, such as fitting the image into the new shape of
// the TouchImageView, after the final size has been determined. This helps us avoid both
// onSizeChanged is called once after the final onMeasure is called. So we make all changes
// to class members, such as fitting the image into the new shape of the TouchImageView,
// here, after the final size has been determined. This helps us avoid both
// repeated computations, and making irreversible changes (e.g. making the View temporarily too
// big or too small, thus making the current zoom fall outside of an automatically-changing
// minZoom and maxZoom).
//
removeCallbacks(processFinalViewSize);
post(processFinalViewSize);
viewWidth = w;
viewHeight = h;
fitImageToView();
}

private Runnable processFinalViewSize = new Runnable() {
@Override
public void run() {
viewWidth = getMeasuredWidth();
viewHeight = getMeasuredHeight();
fitImageToView();
}
};

/**
* This function can be called:
* 1. When the TouchImageView is first loaded (onMeasure).
Expand Down Expand Up @@ -779,12 +777,12 @@ private void fitImageToView() {
// to NaN in newTranslationAfterChange. To avoid this, call savePreviousImageValues
// to set them equal to the current values.
//
if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) {
if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) {
savePreviousImageValues();
}
}

//
// Use the previous matrix as our starting point for the new matrix.
//
// Use the previous matrix as our starting point for the new matrix.
//
prevMatrix.getValues(m);

Expand Down

0 comments on commit b7d8dc2

Please sign in to comment.