ResizingTextViewpublic class ResizingTextView extends android.widget.TextView A {@link android.widget.TextView} that adjusts text size automatically in response
to certain trigger conditions, such as text that wraps over multiple lines. |
Fields Summary |
---|
public static final int | TRIGGER_MAX_LINESTrigger text resize when text flows into the last line of a multi-line text view. | private int | mTriggerConditions | private int | mResizedTextSize | private boolean | mMaintainLineSpacing | private int | mResizedPaddingAdjustmentTop | private int | mResizedPaddingAdjustmentBottom | private boolean | mIsResized | private boolean | mDefaultsInitialized | private int | mDefaultTextSize | private float | mDefaultLineSpacingExtra | private int | mDefaultPaddingTop | private int | mDefaultPaddingBottom |
Constructors Summary |
---|
public ResizingTextView(android.content.Context ctx, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)
super(ctx, attrs, defStyleAttr);
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.lbResizingTextView,
defStyleAttr, defStyleRes);
try {
mTriggerConditions = a.getInt(
R.styleable.lbResizingTextView_resizeTrigger, TRIGGER_MAX_LINES);
mResizedTextSize = a.getDimensionPixelSize(
R.styleable.lbResizingTextView_resizedTextSize, -1);
mMaintainLineSpacing = a.getBoolean(
R.styleable.lbResizingTextView_maintainLineSpacing, false);
mResizedPaddingAdjustmentTop = a.getDimensionPixelOffset(
R.styleable.lbResizingTextView_resizedPaddingAdjustmentTop, 0);
mResizedPaddingAdjustmentBottom = a.getDimensionPixelOffset(
R.styleable.lbResizingTextView_resizedPaddingAdjustmentBottom, 0);
} finally {
a.recycle();
}
| public ResizingTextView(android.content.Context ctx, android.util.AttributeSet attrs, int defStyleAttr)
this(ctx, attrs, defStyleAttr, 0);
| public ResizingTextView(android.content.Context ctx, android.util.AttributeSet attrs)
// TODO We should define our own style that inherits from TextViewStyle, to set defaults
// for new styleables, We then pass the appropriate R.attr up the constructor chain here.
this(ctx, attrs, android.R.attr.textViewStyle);
| public ResizingTextView(android.content.Context ctx)
this(ctx, null);
|
Methods Summary |
---|
public boolean | getMaintainLineSpacing()
return mMaintainLineSpacing;
| public int | getResizedPaddingAdjustmentBottom()
return mResizedPaddingAdjustmentBottom;
| public int | getResizedPaddingAdjustmentTop()
return mResizedPaddingAdjustmentTop;
| public int | getResizedTextSize()
return mResizedTextSize;
| public int | getTriggerConditions()
return mTriggerConditions;
| protected void | onMeasure(int widthMeasureSpec, int heightMeasureSpec)
if (!mDefaultsInitialized) {
mDefaultTextSize = (int) getTextSize();
mDefaultLineSpacingExtra = getLineSpacingExtra();
mDefaultPaddingTop = getPaddingTop();
mDefaultPaddingBottom = getPaddingBottom();
mDefaultsInitialized = true;
}
// Always try first to measure with defaults. Otherwise, we may think we can get away
// with larger text sizes later when we actually can't.
setTextSize(TypedValue.COMPLEX_UNIT_PX, mDefaultTextSize);
setLineSpacing(mDefaultLineSpacingExtra, getLineSpacingMultiplier());
setPaddingTopAndBottom(mDefaultPaddingTop, mDefaultPaddingBottom);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
boolean resizeText = false;
final Layout layout = getLayout();
if (layout != null) {
if ((mTriggerConditions & TRIGGER_MAX_LINES) > 0) {
final int lineCount = layout.getLineCount();
final int maxLines = getMaxLines();
if (maxLines > 1) {
resizeText = lineCount == maxLines;
}
}
}
final int currentSizePx = (int) getTextSize();
boolean remeasure = false;
if (resizeText) {
if (mResizedTextSize != -1 && currentSizePx != mResizedTextSize) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, mResizedTextSize);
remeasure = true;
}
// Check for other desired adjustments in addition to the text size
final float targetLineSpacingExtra = mDefaultLineSpacingExtra +
mDefaultTextSize - mResizedTextSize;
if (mMaintainLineSpacing && getLineSpacingExtra() != targetLineSpacingExtra) {
setLineSpacing(targetLineSpacingExtra, getLineSpacingMultiplier());
remeasure = true;
}
final int paddingTop = mDefaultPaddingTop + mResizedPaddingAdjustmentTop;
final int paddingBottom = mDefaultPaddingBottom + mResizedPaddingAdjustmentBottom;
if (getPaddingTop() != paddingTop || getPaddingBottom() != paddingBottom) {
setPaddingTopAndBottom(paddingTop, paddingBottom);
remeasure = true;
}
} else {
// Use default size, line spacing, and padding
if (mResizedTextSize != -1 && currentSizePx != mDefaultTextSize) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, mDefaultTextSize);
remeasure = true;
}
if (mMaintainLineSpacing && getLineSpacingExtra() != mDefaultLineSpacingExtra) {
setLineSpacing(mDefaultLineSpacingExtra, getLineSpacingMultiplier());
remeasure = true;
}
if (getPaddingTop() != mDefaultPaddingTop ||
getPaddingBottom() != mDefaultPaddingBottom) {
setPaddingTopAndBottom(mDefaultPaddingTop, mDefaultPaddingBottom);
remeasure = true;
}
}
mIsResized = resizeText;
if (remeasure) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
| private void | resizeParamsChanged()
// If we're not resized, then changing resize parameters doesn't
// affect layout, so don't bother requesting.
if (mIsResized) {
requestLayout();
}
| public void | setMaintainLineSpacing(boolean maintain)Set whether or not to maintain line spacing when resizing text.
The default is true.
if (mMaintainLineSpacing != maintain) {
mMaintainLineSpacing = maintain;
resizeParamsChanged();
}
| private void | setPaddingTopAndBottom(int paddingTop, int paddingBottom)
if (isPaddingRelative()) {
setPaddingRelative(getPaddingStart(), paddingTop, getPaddingEnd(), paddingBottom);
} else {
setPadding(getPaddingLeft(), paddingTop, getPaddingRight(), paddingBottom);
}
| public void | setResizedPaddingAdjustmentBottom(int adjustment)Set the desired adjustment to bottom padding for resized text.
if (mResizedPaddingAdjustmentBottom != adjustment) {
mResizedPaddingAdjustmentBottom = adjustment;
resizeParamsChanged();
}
| public void | setResizedPaddingAdjustmentTop(int adjustment)Set the desired adjustment to top padding for resized text.
if (mResizedPaddingAdjustmentTop != adjustment) {
mResizedPaddingAdjustmentTop = adjustment;
resizeParamsChanged();
}
| public void | setResizedTextSize(int size)Set the text size for resized text.
if (mResizedTextSize != size) {
mResizedTextSize = size;
resizeParamsChanged();
}
| public void | setTriggerConditions(int conditions)Set the trigger conditions used to determine whether resize occurs. Pass
a union of trigger condition constants, such as {@link ResizingTextView#TRIGGER_MAX_LINES}.
if (mTriggerConditions != conditions) {
mTriggerConditions = conditions;
// Always request a layout when trigger conditions change
requestLayout();
}
|
|