android_calculator-7: Bug with displaying result
This commit is contained in:
parent
4e8f860409
commit
92b2a27edf
@ -8,11 +8,12 @@ package org.solovyev.android.view;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
|
import android.text.Editable;
|
||||||
import android.text.Layout.Alignment;
|
import android.text.Layout.Alignment;
|
||||||
import android.text.StaticLayout;
|
import android.text.StaticLayout;
|
||||||
import android.text.TextPaint;
|
import android.text.TextPaint;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.TypedValue;
|
import android.util.Log;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -163,7 +164,9 @@ public class AutoResizeTextView extends TextView {
|
|||||||
@Override
|
@Override
|
||||||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||||||
if (changed || needsResize) {
|
if (changed || needsResize) {
|
||||||
resizeText(right - left, bottom - top, getText());
|
int widthLimit = (right - left) - getCompoundPaddingLeft() - getCompoundPaddingRight();
|
||||||
|
int heightLimit = (bottom - top) - getCompoundPaddingBottom() - getCompoundPaddingTop();
|
||||||
|
resizeText(widthLimit, heightLimit, getText());
|
||||||
}
|
}
|
||||||
super.onLayout(changed, left, top, right, bottom);
|
super.onLayout(changed, left, top, right, bottom);
|
||||||
}
|
}
|
||||||
@ -175,7 +178,7 @@ public class AutoResizeTextView extends TextView {
|
|||||||
resizeText(getText());
|
resizeText(getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resizeText(final CharSequence text) {
|
private void resizeText(final CharSequence text) {
|
||||||
int heightLimit = getHeight() - getPaddingBottom() - getPaddingTop();
|
int heightLimit = getHeight() - getPaddingBottom() - getPaddingTop();
|
||||||
int widthLimit = getWidth() - getPaddingLeft() - getPaddingRight();
|
int widthLimit = getWidth() - getPaddingLeft() - getPaddingRight();
|
||||||
resizeText(widthLimit, heightLimit, text);
|
resizeText(widthLimit, heightLimit, text);
|
||||||
@ -188,7 +191,8 @@ public class AutoResizeTextView extends TextView {
|
|||||||
* @param height
|
* @param height
|
||||||
* @param text
|
* @param text
|
||||||
*/
|
*/
|
||||||
public void resizeText(int width, int height, final CharSequence text) {
|
private void resizeText(int width, int height, CharSequence text) {
|
||||||
|
Log.d(this.getClass().getName(), "Resizing: w=" + width + ", h=" + height + ", text='" + text + "'");
|
||||||
|
|
||||||
// Do not resize if the view does not have dimensions or there is no text
|
// Do not resize if the view does not have dimensions or there is no text
|
||||||
if (text == null || text.length() == 0 || height <= 0 || width <= 0) {
|
if (text == null || text.length() == 0 || height <= 0 || width <= 0) {
|
||||||
@ -200,13 +204,23 @@ public class AutoResizeTextView extends TextView {
|
|||||||
|
|
||||||
// Store the current text size
|
// Store the current text size
|
||||||
float oldTextSize = textPaint.getTextSize();
|
float oldTextSize = textPaint.getTextSize();
|
||||||
|
Log.d(this.getClass().getName(), "Old text size: " + oldTextSize);
|
||||||
|
|
||||||
// If there is a max text size set, use the lesser of that and the default text size
|
// If there is a max text size set, use the lesser of that and the default text size
|
||||||
float newTextSize = 100;
|
float newTextSize = 100;
|
||||||
|
|
||||||
// Get the required text height
|
int newTextHeight;
|
||||||
int newTextHeight = getTextRect(text, textPaint, width, newTextSize);
|
|
||||||
|
|
||||||
|
if (text instanceof Editable) {
|
||||||
|
((Editable) text).append("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
// Get the required text height
|
||||||
|
newTextHeight = getTextRect(text, textPaint, width, newTextSize);
|
||||||
|
|
||||||
|
logDimensions(newTextSize, newTextHeight);
|
||||||
if (newTextHeight > height) {
|
if (newTextHeight > height) {
|
||||||
// Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes
|
// Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes
|
||||||
while (newTextHeight > height) {
|
while (newTextHeight > height) {
|
||||||
@ -215,6 +229,7 @@ public class AutoResizeTextView extends TextView {
|
|||||||
}
|
}
|
||||||
newTextSize = Math.max(newTextSize - 2, minTextSize);
|
newTextSize = Math.max(newTextSize - 2, minTextSize);
|
||||||
newTextHeight = getTextRect(text, textPaint, width, newTextSize);
|
newTextHeight = getTextRect(text, textPaint, width, newTextSize);
|
||||||
|
logDimensions(newTextSize, newTextHeight);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while (newTextHeight < height) {
|
while (newTextHeight < height) {
|
||||||
@ -223,6 +238,12 @@ public class AutoResizeTextView extends TextView {
|
|||||||
}
|
}
|
||||||
newTextSize = Math.max(newTextSize + 2, minTextSize);
|
newTextSize = Math.max(newTextSize + 2, minTextSize);
|
||||||
newTextHeight = getTextRect(text, textPaint, width, newTextSize);
|
newTextHeight = getTextRect(text, textPaint, width, newTextSize);
|
||||||
|
logDimensions(newTextSize, newTextHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (text instanceof Editable) {
|
||||||
|
((Editable) text).delete(text.length() - 1, text.length());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,6 +280,10 @@ public class AutoResizeTextView extends TextView {
|
|||||||
needsResize = false;
|
needsResize = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void logDimensions(float newTextSize, int newTextHeight) {
|
||||||
|
Log.d(this.getClass().getName(), "Nex text size: " + newTextSize + ", new text height: " + newTextHeight);
|
||||||
|
}
|
||||||
|
|
||||||
// Set the text size of the text paint object and use a static layout to render text off screen before measuring
|
// Set the text size of the text paint object and use a static layout to render text off screen before measuring
|
||||||
private int getTextRect(CharSequence source, TextPaint paint, int width, float textSize) {
|
private int getTextRect(CharSequence source, TextPaint paint, int width, float textSize) {
|
||||||
// Update the text paint object
|
// Update the text paint object
|
||||||
|
Loading…
Reference in New Issue
Block a user