Challenge: Efficient Thumbnail Load

In this chapter, you had to use a crude estimate of the size you should scale down to. This is not ideal, but it works and is quick to implement.

With the out-of-the-box APIs, you can use a tool called ViewTreeObserver, an object that you can get from any view in your Activity’s hierarchy:

    val observer = imageView.viewTreeObserver

You can register a variety of listeners on a ViewTreeObserver, including OnGlobalLayoutListener. This listener fires an event whenever a layout pass happens.

For this challenge, adjust your code so that it uses the dimensions of photoView when they are valid and waits until a layout pass before initially calling updatePhotoView().

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.