auto resize text view +layout changes

This commit is contained in:
serso 2011-11-02 01:06:56 +04:00
parent f20c34eb5a
commit a7aae1626a
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_three_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_square_brackets_button"/>
</LinearLayout>

View File

@ -105,8 +105,8 @@
<include layout="@layout/calc_eight_digit_button"/>
<include layout="@layout/calc_nine_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_square_brackets_button"/>
</LinearLayout>

View File

@ -68,7 +68,7 @@
<include layout="@layout/calc_round_brackets_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_paste_button"/>

View File

@ -62,7 +62,7 @@
<include layout="@layout/calc_round_brackets_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_paste_button"/>

View File

@ -8,10 +8,9 @@
<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"
a:id="@+id/squareBracketsButton" a:text="[]"
calc:textUp="["
calc:textDown="]"
a:id="@+id/squareBracketsButton"
a:text="."
calc:textUp=","
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -7,9 +7,8 @@
-->
<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"
a:id="@+id/subtractionButton" a:text="-"
calc:textUp=","
a:id="@+id/subtractionButton"
a:text="-"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -10,7 +10,6 @@
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/zeroDigitButton"
a:text="0"
calc:textUp="."
calc:textDown="000"
calc:directionTextScale="0.5"
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())
setAddEllipsis(false);
setMinTextSize(10);
setMaxTextSize(70);
setTextSize(70);
resizeText();
}

View File

@ -12,7 +12,6 @@ import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
/**
@ -45,23 +44,19 @@ public class AutoResizeTextView extends TextView {
// Flag for text and/or size changes to force a resize
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
private float mMinTextSize = MIN_TEXT_SIZE;
private float minTextSize = MIN_TEXT_SIZE;
// Text view line spacing multiplier
private float mSpacingMult = 1.0f;
private float spacingMult = 1.0f;
// 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
private boolean mAddEllipsis = true;
private boolean addEllipsis = true;
private float textSizeStartPoint = MIN_TEXT_SIZE;
// Default constructor override
public AutoResizeTextView(Context context) {
@ -76,7 +71,6 @@ public class AutoResizeTextView extends TextView {
// Default constructor override
public AutoResizeTextView(Context context, AttributeSet attrs, int 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) {
mNeedsResize = true;
// 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
*
* @param listener
*/
public void setOnResizeListener(OnTextResizeListener 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
public void setTextSize(int unit, float size) {
super.setTextSize(unit, size);
mTextSize = getTextSize();
}
/**
@ -131,76 +115,55 @@ public class AutoResizeTextView extends TextView {
@Override
public void setLineSpacing(float add, float mult) {
super.setLineSpacing(add, mult);
mSpacingMult = mult;
mSpacingAdd = 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;
spacingMult = mult;
spacingAdd = add;
}
/**
* Set the lower text size limit and invalidate the view
*
* @param minTextSize
*/
public void setMinTextSize(float minTextSize) {
mMinTextSize = minTextSize;
this.minTextSize = minTextSize;
requestLayout();
invalidate();
}
/**
* Return lower text size limit
*
* @return
*/
public float getMinTextSize() {
return mMinTextSize;
return minTextSize;
}
/**
* Set flag to add ellipsis to text that overflows at the smallest text size
*
* @param 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
*/
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
*/
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if(changed || mNeedsResize) {
if (changed || mNeedsResize) {
resizeText(right - left, bottom - top);
}
super.onLayout(changed, left, top, right, bottom);
@ -217,13 +180,14 @@ public class AutoResizeTextView extends TextView {
/**
* Resize the text size with specified width and height
*
* @param width
* @param height
*/
public void resizeText(int width, int height) {
CharSequence text = getText();
// 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) {
return;
}
@ -232,22 +196,38 @@ public class AutoResizeTextView extends TextView {
// Store the current text size
float oldTextSize = textPaint.getTextSize();
// 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
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
while(textHeight > height && targetTextSize > mMinTextSize) {
targetTextSize = Math.max(targetTextSize - 2, mMinTextSize);
textHeight = getTextHeight(text, textPaint, width, targetTextSize);
while (newTextHeight > height) {
if (newTextSize <= minTextSize) {
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(mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) {
if (addEllipsis && newTextSize == minTextSize && newTextHeight > height) {
// 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);
int lastLine = layout.getLineForVertical(height) - 1;
int start = layout.getLineStart(lastLine);
@ -256,7 +236,7 @@ public class AutoResizeTextView extends TextView {
float ellipseWidth = textPaint.measureText(mEllipsis);
// Trim characters off until we have enough room to draw the ellipsis
while(width < lineWidth + ellipseWidth) {
while (width < lineWidth + ellipseWidth) {
lineWidth = textPaint.measureText(text.subSequence(start, --end + 1).toString());
}
setText(text.subSequence(0, end) + mEllipsis);
@ -265,12 +245,12 @@ public class AutoResizeTextView extends TextView {
// Some devices try to auto adjust line spacing, so force default line spacing
// and invalidate the layout as a side effect
textPaint.setTextSize(targetTextSize);
setLineSpacing(mSpacingAdd, mSpacingMult);
textPaint.setTextSize(newTextSize);
//setLineSpacing(spacingAdd, spacingMult);
// Notify the listener if registered
if(mTextResizeListener != null) {
mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize);
if (mTextResizeListener != null) {
mTextResizeListener.onTextResize(this, oldTextSize, newTextSize);
}
// 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
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
paint.setTextSize(textSize);
// 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);
return layout.getHeight();
}
}