Code example for SparseIntArray

Methods: clear, get, put

0
} 
 
@Override 
public void setTextSize(float size) {
    mMaxTextSize = size;
    mTextCachedSizes.clear();
    adjustTextSize((String) getText());
} 
 
@Override 
public void setMaxLines(int maxlines) {
    super.setMaxLines(maxlines);
    mMaxLines = maxlines;
    reAdjust(); 
} 
 
public int getMaxLines() { 
    return mMaxLines;
} 
 
@Override 
public void setSingleLine() { 
    super.setSingleLine(); 
    mMaxLines = 1;
    reAdjust(); 
} 
 
@Override 
public void setSingleLine(boolean singleLine) {
    super.setSingleLine(singleLine);
    if (singleLine) {
        mMaxLines = 1;
    } else { 
        mMaxLines = NO_LINE_LIMIT;
    } 
    reAdjust(); 
} 
 
@Override 
public void setLines(int lines) {
    super.setLines(lines);
    mMaxLines = lines;
    reAdjust(); 
} 
 
@Override 
public void setTextSize(int unit, float size) {
    Context c = getContext();
    Resources r;
 
    if (c == null)
        r = Resources.getSystem();
    else 
        r = c.getResources();
    mMaxTextSize = TypedValue.applyDimension(unit, size,
            r.getDisplayMetrics());
    mTextCachedSizes.clear();
    adjustTextSize((String) getText());
} 
 
@Override 
public void setLineSpacing(float add, float mult) {
    super.setLineSpacing(add, mult);
    mSpacingMult = mult;
    mSpacingAdd = add;
} 
 
/** 
 * Set the lower text size limit and invalidate the view 
 *  
 * @param minTextSize 
 */ 
public void setMinTextSize(float minTextSize) {
    mMinTextSize = minTextSize;
    reAdjust(); 
} 
 
private void reAdjust() { 
    adjustTextSize((String) getText());
} 
 
private void adjustTextSize(String string) {
    if (!mInitiallized) {
        return; 
    } 
    int startSize = (int) mMinTextSize;
    int heightLimit = getMeasuredHeight() - getPaddingBottom()
            - getPaddingTop();
    mWidthLimit = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    mAvailableSpaceRect.right = mWidthLimit;
    mAvailableSpaceRect.bottom = heightLimit;
    super.setTextSize( 
            TypedValue.COMPLEX_UNIT_PX,
            efficientTextSizeSearch(startSize, (int) mMaxTextSize,
                    mSizeTester, mAvailableSpaceRect));
} 
 
private final SizeTester mSizeTester = new SizeTester() {
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
    @Override 
    public int onTestSize(int suggestedSize, RectF availableSPace) { 
        mPaint.setTextSize(suggestedSize); 
        String text = getText().toString(); 
        boolean singleline = getMaxLines() == 1;  
        if (singleline) {  
            mTextRect.bottom = mPaint.getFontSpacing(); 
            mTextRect.right = mPaint.measureText(text); 
        } else { 
            StaticLayout layout = new StaticLayout(text, mPaint, 
                    mWidthLimit, Alignment.ALIGN_NORMAL, mSpacingMult, 
                    mSpacingAdd, true); 
            // return early if we have more lines 
            if (getMaxLines() != NO_LINE_LIMIT 
                    && layout.getLineCount() > getMaxLines()) { 
                return 1; 
            } 
            mTextRect.bottom = layout.getHeight(); 
            int maxWidth = -1; 
            for (int i = 0; i < layout.getLineCount(); i++) { 
                if (maxWidth < layout.getLineWidth(i)) { 
                    maxWidth = (int) layout.getLineWidth(i); 
                } 
            } 
            mTextRect.right = maxWidth; 
        } 
 
        mTextRect.offsetTo(0, 0); 
        if (availableSPace.contains(mTextRect)) { 
            // may be too small, don't worry we will find the best match 
            return -1; 
        } else { 
            // too big 
            return 1; 
        } 
    } 
}; 
 
/** 
 * Enables or disables size caching, enabling it will improve performance 
 * where you are animating a value inside TextView. This stores the font 
 * size against getText().length() Be careful though while enabling it as 0 
 * takes more space than 1 on some fonts and so on. 
 *  
 * @param enable 
 *            enable font size caching 
 */ 
public void enableSizeCache(boolean enable) {
    mEnableSizeCache = enable;
    mTextCachedSizes.clear();
    adjustTextSize((String) getText());
} 
 
private int efficientTextSizeSearch(int start, int end,
        SizeTester sizeTester, RectF availableSpace) {
    if (!mEnableSizeCache) {
        return binarySearch(start, end, sizeTester, availableSpace);
    } 
    String text = getText().toString();
    int key = text == null ? 0 : text.length();
    int size = mTextCachedSizes.get(key);
    if (size != 0) {
        return size;
    } 
    size = binarySearch(start, end, sizeTester, availableSpace);
    mTextCachedSizes.put(key, size);
    return size;
} 
 
private static int binarySearch(int start, int end, SizeTester sizeTester,
        RectF availableSpace) {