Don't remove OnPreDrawListener but keep the last used height

This commit is contained in:
serso 2016-02-15 09:50:57 +01:00
parent fb79a9923f
commit 9eee427cbb

View File

@ -1,5 +1,7 @@
package org.solovyev.android.views; package org.solovyev.android.views;
import static android.graphics.Matrix.MSCALE_Y;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -9,8 +11,6 @@ import android.view.ViewTreeObserver;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import static android.graphics.Matrix.MSCALE_Y;
public class Adjuster { public class Adjuster {
private static final float[] MATRIX = new float[9]; private static final float[] MATRIX = new float[9];
@ -47,6 +47,7 @@ public class Adjuster {
@NonNull @NonNull
private final TextView view; private final TextView view;
private final float percentage; private final float percentage;
private int lastHeight;
public TextViewAdjuster(@NonNull TextView view, float percentage) { public TextViewAdjuster(@NonNull TextView view, float percentage) {
this.view = view; this.view = view;
@ -55,12 +56,11 @@ public class Adjuster {
@Override @Override
public boolean onPreDraw() { 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(); final int height = view.getHeight();
if (lastHeight == height || height <= 0) {
return true;
}
lastHeight = height;
final float oldTextSize = Math.round(view.getTextSize()); final float oldTextSize = Math.round(view.getTextSize());
final float newTextSize = Math.round(height * percentage); final float newTextSize = Math.round(height * percentage);
if (oldTextSize == newTextSize) { if (oldTextSize == newTextSize) {
@ -75,6 +75,7 @@ public class Adjuster {
@NonNull @NonNull
private final ImageView view; private final ImageView view;
private final float percentage; private final float percentage;
private int lastHeight;
public ImageViewAdjuster(@NonNull ImageView view, float percentage) { public ImageViewAdjuster(@NonNull ImageView view, float percentage) {
this.view = view; this.view = view;
@ -83,17 +84,15 @@ public class Adjuster {
@Override @Override
public boolean onPreDraw() { public boolean onPreDraw() {
// assume that the view properties are constant final int height = view.getHeight();
final ViewTreeObserver treeObserver = getTreeObserver(view); if (lastHeight == height || height <= 0) {
if (treeObserver != null) { return true;
treeObserver.removeOnPreDrawListener(this);
} }
lastHeight = height;
final Drawable d = view.getDrawable(); final Drawable d = view.getDrawable();
if (d == null) { if (d == null) {
return true; return true;
} }
final int height = view.getHeight();
view.getImageMatrix().getValues(MATRIX); view.getImageMatrix().getValues(MATRIX);
final int oldImageHeight = Math.round(d.getIntrinsicHeight() * MATRIX[MSCALE_Y]); final int oldImageHeight = Math.round(d.getIntrinsicHeight() * MATRIX[MSCALE_Y]);
final int newImageHeight = Math.round(height * percentage); final int newImageHeight = Math.round(height * percentage);