Search in sources :

Example 1 with TopDownGradientSpan

use of com.nolanlawson.keepscore.android.TopDownGradientSpan in project KeepScore by nolanlawson.

the class AutofitTextView method onMeasure.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int height = getHeight() - getCompoundPaddingBottom() - getCompoundPaddingTop();
    int maxNumLines = (height / getLineHeight());
    if (maxNumLines >= 2) {
        String oldText = getText().toString();
        int cutoffIndex = StringUtil.getNthIndexOf('\n', oldText, maxNumLines);
        if (cutoffIndex != -1) {
            // cut off the text
            int startOfLastLine = StringUtil.getNthIndexOf('\n', oldText, maxNumLines - 1);
            // make the last line semi-transparent, to indicate that there
            // are more results left
            // (similar to the ellipsize effect in normal text views, but
            // vertical)
            // get the original color (blue or red)
            ForegroundColorSpan[] foregroundColorSpans = ((SpannedString) getText()).getSpans(startOfLastLine + 1, cutoffIndex, ForegroundColorSpan.class);
            // make an alpha-ized gradient out of the original color
            int originalColor = foregroundColorSpans[0].getForegroundColor();
            int startColor = (START_ALPHA << 24) | (0x00FFFFFF & originalColor);
            int endColor = (END_ALPHA << 24) | (0x00FFFFFF & originalColor);
            int numLines = StringUtil.count(getText().subSequence(0, cutoffIndex).toString(), "\n");
            float startY = (numLines * getLineHeight());
            float endY = startY + getLineHeight();
            // build up a new spannable
            SpannableStringBuilder builder = new SpannableStringBuilder().append(getText().subSequence(0, startOfLastLine)).append(getText().subSequence(startOfLastLine, cutoffIndex).toString());
            builder.setSpan(new TopDownGradientSpan(startColor, endColor, startY, endY), startOfLastLine, cutoffIndex, 0);
            setText(builder);
        }
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) TopDownGradientSpan(com.nolanlawson.keepscore.android.TopDownGradientSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

SpannableStringBuilder (android.text.SpannableStringBuilder)1 SpannedString (android.text.SpannedString)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 TopDownGradientSpan (com.nolanlawson.keepscore.android.TopDownGradientSpan)1