multiline text display

This commit is contained in:
serso 2011-11-02 13:10:56 +04:00
parent 7c4f274f0c
commit 0518d92c44
4 changed files with 21 additions and 19 deletions

View File

@ -10,5 +10,7 @@
xmlns:a="http://schemas.android.com/apk/res/android" xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/calculatorDisplay" a:id="@+id/calculatorDisplay"
style="@style/display_style" style="@style/display_style"
a:scrollHorizontally="true" a:inputType="textMultiLine"
a:maxLines="3"
a:scrollHorizontally="false"
a:scrollbars="none"/> a:scrollbars="none"/>

View File

@ -58,7 +58,7 @@
</style> </style>
<style name="display_style" parent="display_style_parent"> <style name="display_style" parent="display_style_parent">
<item name="android:gravity">center_vertical|right</item> <item name="android:gravity">top|right</item>
<item name="android:layout_width">fill_parent</item> <item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item> <item name="android:layout_height">fill_parent</item>
</style> </style>

View File

@ -84,7 +84,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
final CharSequence text = display.getText(); final CharSequence text = display.getText();
if (!StringUtils.isEmpty(text)) { if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text); clipboard.setText(text.toString());
Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show(); Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show();
} }
} }

View File

@ -33,16 +33,16 @@ public class AutoResizeTextView extends TextView {
} }
// Off screen canvas for text size rendering // Off screen canvas for text size rendering
private static final Canvas sTextResizeCanvas = new Canvas(); private static final Canvas textResizeCanvas = new Canvas();
// Our ellipse string // Our ellipse string
private static final String mEllipsis = "..."; private static final String ellipsis = "...";
// Registered resize listener // Registered resize listener
private OnTextResizeListener mTextResizeListener; private OnTextResizeListener textResizeListener;
// 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 needsResize = false;
// Lower bounds for text size // Lower bounds for text size
private float minTextSize = MIN_TEXT_SIZE; private float minTextSize = MIN_TEXT_SIZE;
@ -78,7 +78,7 @@ public class AutoResizeTextView extends TextView {
*/ */
@Override @Override
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; needsResize = 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
} }
@ -88,7 +88,7 @@ public class AutoResizeTextView extends TextView {
@Override @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw || h != oldh) { if (w != oldw || h != oldh) {
mNeedsResize = true; needsResize = true;
} }
} }
@ -98,7 +98,7 @@ public class AutoResizeTextView extends TextView {
* @param listener * @param listener
*/ */
public void setOnResizeListener(OnTextResizeListener listener) { public void setOnResizeListener(OnTextResizeListener listener) {
mTextResizeListener = listener; textResizeListener = listener;
} }
/** /**
@ -163,7 +163,7 @@ 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 || mNeedsResize) { if (changed || needsResize) {
resizeText(right - left, bottom - top); resizeText(right - left, bottom - top);
} }
super.onLayout(changed, left, top, right, bottom); super.onLayout(changed, left, top, right, bottom);
@ -228,33 +228,33 @@ public class AutoResizeTextView extends TextView {
if (addEllipsis && newTextSize == minTextSize && newTextHeight > 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, spacingMult, spacingAdd, false); StaticLayout layout = new StaticLayout(text, textPaint, width, Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, false);
layout.draw(sTextResizeCanvas); layout.draw(textResizeCanvas);
int lastLine = layout.getLineForVertical(height) - 1; int lastLine = layout.getLineForVertical(height) - 1;
int start = layout.getLineStart(lastLine); int start = layout.getLineStart(lastLine);
int end = layout.getLineEnd(lastLine); int end = layout.getLineEnd(lastLine);
float lineWidth = layout.getLineWidth(lastLine); float lineWidth = layout.getLineWidth(lastLine);
float ellipseWidth = textPaint.measureText(mEllipsis); float ellipseWidth = textPaint.measureText(ellipsis);
// Trim characters off until we have enough room to draw the ellipsis // 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()); lineWidth = textPaint.measureText(text.subSequence(start, --end + 1).toString());
} }
setText(text.subSequence(0, end) + mEllipsis); setText(text.subSequence(0, end) + ellipsis);
} }
// 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(newTextSize); textPaint.setTextSize(newTextSize);
//setLineSpacing(spacingAdd, spacingMult); setLineSpacing(spacingAdd, spacingMult);
// Notify the listener if registered // Notify the listener if registered
if (mTextResizeListener != null) { if (textResizeListener != null) {
mTextResizeListener.onTextResize(this, oldTextSize, newTextSize); textResizeListener.onTextResize(this, oldTextSize, newTextSize);
} }
// Reset force resize flag // Reset force resize flag
mNeedsResize = false; needsResize = false;
} }
// 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
@ -263,7 +263,7 @@ public class AutoResizeTextView extends TextView {
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, spacingMult, spacingAdd, false); StaticLayout layout = new StaticLayout(source, paint, width, Alignment.ALIGN_NORMAL, spacingMult, spacingAdd, false);
layout.draw(sTextResizeCanvas); layout.draw(textResizeCanvas);
return layout.getHeight(); return layout.getHeight();
} }
} }