auto resize text view +layout changes

This commit is contained in:
serso 2011-11-02 01:06:56 +04:00
parent 7b10d758cc
commit 7c4f274f0c
9 changed files with 212 additions and 238 deletions

View File

@ -111,8 +111,8 @@
<include layout="@layout/calc_two_digit_button"/> <include layout="@layout/calc_two_digit_button"/>
<include layout="@layout/calc_three_digit_button"/> <include layout="@layout/calc_three_digit_button"/>
<include layout="@layout/calc_zero_digit_button"/> <include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_dot_button"/>
<include layout="@layout/calc_round_brackets_button"/> <include layout="@layout/calc_round_brackets_button"/>
<include layout="@layout/calc_square_brackets_button"/>
</LinearLayout> </LinearLayout>

View File

@ -105,8 +105,8 @@
<include layout="@layout/calc_eight_digit_button"/> <include layout="@layout/calc_eight_digit_button"/>
<include layout="@layout/calc_nine_digit_button"/> <include layout="@layout/calc_nine_digit_button"/>
<include layout="@layout/calc_zero_digit_button"/> <include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_dot_button"/>
<include layout="@layout/calc_round_brackets_button"/> <include layout="@layout/calc_round_brackets_button"/>
<include layout="@layout/calc_square_brackets_button"/>
</LinearLayout> </LinearLayout>

View File

@ -68,7 +68,7 @@
<include layout="@layout/calc_round_brackets_button"/> <include layout="@layout/calc_round_brackets_button"/>
<include layout="@layout/calc_zero_digit_button"/> <include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_square_brackets_button"/> <include layout="@layout/calc_dot_button"/>
<include layout="@layout/calc_subtraction_button"/> <include layout="@layout/calc_subtraction_button"/>
<include layout="@layout/calc_paste_button"/> <include layout="@layout/calc_paste_button"/>

View File

@ -62,7 +62,7 @@
<include layout="@layout/calc_round_brackets_button"/> <include layout="@layout/calc_round_brackets_button"/>
<include layout="@layout/calc_zero_digit_button"/> <include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_square_brackets_button"/> <include layout="@layout/calc_dot_button"/>
<include layout="@layout/calc_subtraction_button"/> <include layout="@layout/calc_subtraction_button"/>
<include layout="@layout/calc_paste_button"/> <include layout="@layout/calc_paste_button"/>

View File

@ -8,10 +8,9 @@
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/squareBracketsButton" a:text="[]" a:id="@+id/squareBracketsButton"
calc:textUp="[" a:text="."
calc:textDown="]" calc:textUp=","
calc:directionTextScale="0.5" calc:directionTextScale="0.5"
style="?digitButtonStyle" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -7,9 +7,8 @@
--> -->
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/subtractionButton" a:text="-" a:id="@+id/subtractionButton"
calc:textUp="," a:text="-"
calc:directionTextScale="0.5" calc:directionTextScale="0.5"
style="?digitButtonStyle" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -10,7 +10,6 @@
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/zeroDigitButton" a:id="@+id/zeroDigitButton"
a:text="0" a:text="0"
calc:textUp="."
calc:textDown="000" calc:textDown="000"
calc:directionTextScale="0.5" calc:directionTextScale="0.5"
style="?digitButtonStyle" style="?digitButtonStyle"

View File

@ -87,8 +87,6 @@ public class CalculatorDisplay extends AutoResizeTextView {
// todo serso: think where to move it (keep in mind org.solovyev.android.view.AutoResizeTextView.resetTextSize()) // todo serso: think where to move it (keep in mind org.solovyev.android.view.AutoResizeTextView.resetTextSize())
setAddEllipsis(false); setAddEllipsis(false);
setMinTextSize(10); setMinTextSize(10);
setMaxTextSize(70);
setTextSize(70);
resizeText(); resizeText();
} }

View File

@ -12,7 +12,6 @@ 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.widget.TextView; import android.widget.TextView;
/** /**
@ -45,23 +44,19 @@ public class AutoResizeTextView extends TextView {
// Flag for text and/or size changes to force a resize // Flag for text and/or size changes to force a resize
private boolean mNeedsResize = false; private boolean mNeedsResize = false;
// Text size that is set from code. This acts as a starting point for resizing
private float mTextSize;
// Temporary upper bounds on the starting text size
private float mMaxTextSize = 0;
// Lower bounds for text size // Lower bounds for text size
private float mMinTextSize = MIN_TEXT_SIZE; private float minTextSize = MIN_TEXT_SIZE;
// Text view line spacing multiplier // Text view line spacing multiplier
private float mSpacingMult = 1.0f; private float spacingMult = 1.0f;
// Text view additional line spacing // Text view additional line spacing
private float mSpacingAdd = 0.0f; private float spacingAdd = 0.0f;
// Add ellipsis to text that overflows at the smallest text size // Add ellipsis to text that overflows at the smallest text size
private boolean mAddEllipsis = true; private boolean addEllipsis = true;
private float textSizeStartPoint = MIN_TEXT_SIZE;
// Default constructor override // Default constructor override
public AutoResizeTextView(Context context) { public AutoResizeTextView(Context context) {
@ -76,7 +71,6 @@ public class AutoResizeTextView extends TextView {
// Default constructor override // Default constructor override
public AutoResizeTextView(Context context, AttributeSet attrs, int defStyle) { public AutoResizeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
mTextSize = getTextSize();
} }
/** /**
@ -86,7 +80,6 @@ public class AutoResizeTextView extends TextView {
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) { protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
mNeedsResize = true; mNeedsResize = true;
// Since this view may be reused, it is good to resetInterpreter the text size // Since this view may be reused, it is good to resetInterpreter the text size
resetTextSize();
} }
/** /**
@ -101,28 +94,19 @@ public class AutoResizeTextView extends TextView {
/** /**
* Register listener to receive resize notifications * Register listener to receive resize notifications
*
* @param listener * @param listener
*/ */
public void setOnResizeListener(OnTextResizeListener listener) { public void setOnResizeListener(OnTextResizeListener listener) {
mTextResizeListener = listener; mTextResizeListener = listener;
} }
/**
* Override the set text size to update our internal reference values
*/
@Override
public void setTextSize(float size) {
super.setTextSize(size);
mTextSize = getTextSize();
}
/** /**
* Override the set text size to update our internal reference values * Override the set text size to update our internal reference values
*/ */
@Override @Override
public void setTextSize(int unit, float size) { public void setTextSize(int unit, float size) {
super.setTextSize(unit, size); super.setTextSize(unit, size);
mTextSize = getTextSize();
} }
/** /**
@ -131,69 +115,48 @@ public class AutoResizeTextView extends TextView {
@Override @Override
public void setLineSpacing(float add, float mult) { public void setLineSpacing(float add, float mult) {
super.setLineSpacing(add, mult); super.setLineSpacing(add, mult);
mSpacingMult = mult; spacingMult = mult;
mSpacingAdd = add; spacingAdd = add;
}
/**
* Set the upper text size limit and invalidate the view
* @param maxTextSize
*/
public void setMaxTextSize(float maxTextSize) {
mMaxTextSize = maxTextSize;
requestLayout();
invalidate();
}
/**
* Return upper text size limit
* @return
*/
public float getMaxTextSize() {
return mMaxTextSize;
} }
/** /**
* Set the lower text size limit and invalidate the view * Set the lower text size limit and invalidate the view
*
* @param minTextSize * @param minTextSize
*/ */
public void setMinTextSize(float minTextSize) { public void setMinTextSize(float minTextSize) {
mMinTextSize = minTextSize; this.minTextSize = minTextSize;
requestLayout(); requestLayout();
invalidate(); invalidate();
} }
/** /**
* Return lower text size limit * Return lower text size limit
*
* @return * @return
*/ */
public float getMinTextSize() { public float getMinTextSize() {
return mMinTextSize; return minTextSize;
} }
/** /**
* Set flag to add ellipsis to text that overflows at the smallest text size * Set flag to add ellipsis to text that overflows at the smallest text size
*
* @param addEllipsis * @param addEllipsis
*/ */
public void setAddEllipsis(boolean addEllipsis) { public void setAddEllipsis(boolean addEllipsis) {
mAddEllipsis = addEllipsis; this.addEllipsis = addEllipsis;
} }
/** /**
* Return flag to add ellipsis to text that overflows at the smallest text size * Return flag to add ellipsis to text that overflows at the smallest text size
*
* @return * @return
*/ */
public boolean getAddEllipsis() { public boolean getAddEllipsis() {
return mAddEllipsis; return addEllipsis;
} }
/**
* Reset the text to the original size
*/
public void resetTextSize() {
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
mMaxTextSize = mTextSize;
}
/** /**
* Resize text after measuring * Resize text after measuring
@ -217,6 +180,7 @@ public class AutoResizeTextView extends TextView {
/** /**
* Resize the text size with specified width and height * Resize the text size with specified width and height
*
* @param width * @param width
* @param height * @param height
*/ */
@ -232,22 +196,38 @@ public class AutoResizeTextView extends TextView {
// Store the current text size // Store the current text size
float oldTextSize = textPaint.getTextSize(); float oldTextSize = textPaint.getTextSize();
// 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 targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize; float newTextSize = textSizeStartPoint;
// Get the required text height // Get the required text height
int textHeight = getTextHeight(text, textPaint, width, targetTextSize); int newTextHeight = getTextRect(text, textPaint, width, newTextSize);
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(textHeight > height && targetTextSize > mMinTextSize) { while (newTextHeight > height) {
targetTextSize = Math.max(targetTextSize - 2, mMinTextSize); if (newTextSize <= minTextSize) {
textHeight = getTextHeight(text, textPaint, width, targetTextSize); break;
}
newTextSize = Math.max(newTextSize - 1, minTextSize);
newTextHeight = getTextRect(text, textPaint, width, newTextSize);
}
} else {
while (newTextHeight < height) {
if (newTextSize <= minTextSize) {
break;
}
newTextSize = Math.max(newTextSize + 1, minTextSize);
newTextHeight = getTextRect(text, textPaint, width, newTextSize);
}
} }
textSizeStartPoint = newTextSize;
// If we had reached our minimum text size and still don't fit, append an ellipsis // If we had reached our minimum text size and still don't fit, append an ellipsis
if(mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) { if (addEllipsis && newTextSize == minTextSize && newTextHeight > height) {
// Draw using a static layout // Draw using a static layout
StaticLayout layout = new StaticLayout(text, textPaint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); StaticLayout layout = new StaticLayout(text, textPaint, width, Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, false);
layout.draw(sTextResizeCanvas); layout.draw(sTextResizeCanvas);
int lastLine = layout.getLineForVertical(height) - 1; int lastLine = layout.getLineForVertical(height) - 1;
int start = layout.getLineStart(lastLine); int start = layout.getLineStart(lastLine);
@ -265,12 +245,12 @@ public class AutoResizeTextView extends TextView {
// Some devices try to auto adjust line spacing, so force default line spacing // Some devices try to auto adjust line spacing, so force default line spacing
// and invalidate the layout as a side effect // and invalidate the layout as a side effect
textPaint.setTextSize(targetTextSize); textPaint.setTextSize(newTextSize);
setLineSpacing(mSpacingAdd, mSpacingMult); //setLineSpacing(spacingAdd, spacingMult);
// Notify the listener if registered // Notify the listener if registered
if (mTextResizeListener != null) { if (mTextResizeListener != null) {
mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize); mTextResizeListener.onTextResize(this, oldTextSize, newTextSize);
} }
// Reset force resize flag // Reset force resize flag
@ -278,13 +258,12 @@ public class AutoResizeTextView extends TextView {
} }
// 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 getTextHeight(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
paint.setTextSize(textSize); paint.setTextSize(textSize);
// Draw using a static layout // Draw using a static layout
StaticLayout layout = new StaticLayout(source, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); StaticLayout layout = new StaticLayout(source, paint, width, Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, false);
layout.draw(sTextResizeCanvas); layout.draw(sTextResizeCanvas);
return layout.getHeight(); return layout.getHeight();
} }
} }