title: Determine final size of ImageView shared element after animation
body: <p>I have a number of shared elements that I'm transitioning between two activities via <code>ActivityOptions.makeSceneTransitionAnimation</code>. When I enter the second activity, I would like to determine what the final target size that the <code>ImageView</code> will be so I can set the appropriate width of the <code>ImageView</code>. I believe that doing so will allow the object to the right of it (also a shared element) to transition smoothly.</p>
  
  <p>The <code>ImageView</code> is a <code>fitStart</code> scaleType with a fixed height and a <code>wrap_content</code> width. The image will be dynamically loaded and is being shrunk down from the first activity to the second, so I don't know the final width. Before the transition starts, I want to set the <code>ImageView</code>'s <code>LayoutParams</code> width to match the final width that it will be after the transition ends.</p>
  
  <p>Currently, the <code>wrap_content</code> property of this <code>ImageView</code> is causing the element to the right of it to not know where its final resting place will be, so it goes off the screen to the right before coming back in and landing in the right place.</p>
  
  <p>Here's a gif of the animation. FIRST ELEMENT is the <code>ImageView</code>, animating correctly, and Second Element is the button to the right of the <code>ImageView</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/Z77mP.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z77mP.gif" alt="enter image description here"></a></p>
  
  <p>Notice that when the second activity goes back to the first activity, the Second Element transitions smoothly back to the left of the screen because the end position is known.</p>
  
  <p>When I give the <code>ImageView</code> a specific width like <code>100dp</code> instead of <code>wrap_content</code> (and then change it back to <code>wrap_content</code> after the transition finishes), Second Element moves more smoothly to its final resting place, but since the <code>ImageView</code> will be dynamically loaded and scaled, I won't know the true width of the image in XML beforehand so there would always be a jerky movement of the Second Element when I change it to <code>wrap_content</code> after the transition.</p>
  
  <p><a href="https://i.stack.imgur.com/8VaLH.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8VaLH.gif" alt="enter image description here"></a></p>
  
  <p>So I would like to determine the target width of the <code>ImageView</code> and set it programmatically before the transitions begin in the second activity so that Second Element knows how far to the left it needs to go. I've tried playing with <code>ViewTreeObserver</code> and <code>TransitionValues</code> but am missing how to get the final width the ImageView is transitioning to. </p>
  
  <p><strong>Is there something I can call in <code>onCreate</code> of the second activity to determine what width the ImageView will end up at after the transition?</strong></p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried the solution in <a href="https://stackoverflow.com/a/44693781/591510">this answer</a> but it's reporting back the ImageView's beginning width, not the final width it's transitioning to. For reference, these are the layouts of the two activities I'm switching between:
  <a href="https://i.stack.imgur.com/Dlr3k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Dlr3k.png" alt="enter image description here"></a></p>
  
  <p>Note that I'm moving AND shrinking the ImageView.</p>
  
  <p>I tweaked the answer's code since I'm not using the support library and had to move to <code>imageView.getViewTreeObserver()</code> in the <code>onPreDraw()</code> call to avoid an <code>IllegalStateException</code>:    </p>
  
  <pre><code>postponeEnterTransition();
  
  final ImageView imageView = (ImageView) findViewById(R.id.image_view);
  final ViewGroup.LayoutParams imageViewLayoutParams = imageView.getLayoutParams();
  final ViewTreeObserver viewTreeObserver = imageView.getViewTreeObserver();
  
  viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override public boolean onPreDraw() {
      imageView.getViewTreeObserver().removeOnPreDrawListener(this);
  
      // You can acquire the width here
      logoLayoutParams.width = imageView.getWidth();
      Log.i(TAG, "width is " + imageViewLayoutParams.width);
      imageView.setLayoutParams(imageViewLayoutParams);
      // `imageView` has been laid out, but not yet been drawn
      startPostponedEnterTransition();
      return true;
    }
  });
  </code></pre>
  
  <p>The log is reporting a width of 998, which is the larger size of the imageView in Activity 1 (its starting size before the transition). So the Second Element is still flying off the screen.</p>
  
  <p>Now that I have this code in place, I can use the aspect ratio information of the larger image to figure out the final width that the smaller image will be since I know the final height, but is that guaranteed with the <code>fitStart</code> scaleType?</p>
  
  <p>It would be really nice to know if Android can tell me the final size the shared element targeting.</p>