From 9eee427cbb989b888426a89c60a1c7e6fbb21b41 Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 15 Feb 2016 09:50:57 +0100 Subject: [PATCH] Don't remove OnPreDrawListener but keep the last used height --- .../org/solovyev/android/views/Adjuster.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/views/Adjuster.java b/app/src/main/java/org/solovyev/android/views/Adjuster.java index 35bc5eac..251df6bd 100644 --- a/app/src/main/java/org/solovyev/android/views/Adjuster.java +++ b/app/src/main/java/org/solovyev/android/views/Adjuster.java @@ -1,5 +1,7 @@ package org.solovyev.android.views; +import static android.graphics.Matrix.MSCALE_Y; + import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -9,8 +11,6 @@ import android.view.ViewTreeObserver; import android.widget.ImageView; import android.widget.TextView; -import static android.graphics.Matrix.MSCALE_Y; - public class Adjuster { private static final float[] MATRIX = new float[9]; @@ -47,6 +47,7 @@ public class Adjuster { @NonNull private final TextView view; private final float percentage; + private int lastHeight; public TextViewAdjuster(@NonNull TextView view, float percentage) { this.view = view; @@ -55,12 +56,11 @@ public class Adjuster { @Override public boolean onPreDraw() { - // assume that the view properties are constant - final ViewTreeObserver treeObserver = getTreeObserver(view); - if (treeObserver != null) { - treeObserver.removeOnPreDrawListener(this); - } final int height = view.getHeight(); + if (lastHeight == height || height <= 0) { + return true; + } + lastHeight = height; final float oldTextSize = Math.round(view.getTextSize()); final float newTextSize = Math.round(height * percentage); if (oldTextSize == newTextSize) { @@ -75,6 +75,7 @@ public class Adjuster { @NonNull private final ImageView view; private final float percentage; + private int lastHeight; public ImageViewAdjuster(@NonNull ImageView view, float percentage) { this.view = view; @@ -83,17 +84,15 @@ public class Adjuster { @Override public boolean onPreDraw() { - // assume that the view properties are constant - final ViewTreeObserver treeObserver = getTreeObserver(view); - if (treeObserver != null) { - treeObserver.removeOnPreDrawListener(this); + final int height = view.getHeight(); + if (lastHeight == height || height <= 0) { + return true; } - + lastHeight = height; final Drawable d = view.getDrawable(); if (d == null) { return true; } - final int height = view.getHeight(); view.getImageMatrix().getValues(MATRIX); final int oldImageHeight = Math.round(d.getIntrinsicHeight() * MATRIX[MSCALE_Y]); final int newImageHeight = Math.round(height * percentage);