hashCode = mParams.hashCode(); Layout cachedLayout = sCache.get(hashCode); if (cachedLayout != null) { final int lineHeight = mParams.getLineHeight(); if (mMaxWidthMode == EMS) { width = Math.min(width, mMaxWidth * lineHeight);
/** * Sets the shadow layer for the layout. * * @param radius The radius of the blur for shadow * @param dx The horizontal translation of the origin * @param dy The vertical translation of the origin * @param color The shadow color * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setShadowLayer(float radius, float dx, float dy, @ColorInt int color) { mParams.createNewPaintIfNeeded(); mParams.mShadowRadius = radius; mParams.mShadowDx = dx; mParams.mShadowDy = dy; mParams.mShadowColor = color; mParams.paint.setShadowLayer(radius, dx, dy, color); mSavedLayout = null; return this; }
@Test public void testNoCachingSpannableString() { ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { // Do nothing. } }; SpannableStringBuilder spannable = new SpannableStringBuilder("This is a bold text"); spannable.setSpan(clickableSpan, 10, 13, Spanned.SPAN_INCLUSIVE_INCLUSIVE); mLayout = mBuilder.setText(spannable).setShouldCacheLayout(true).build(); assertEquals(mBuilder.sCache.size(), 0); assertEquals(mBuilder.sCache.get(mBuilder.mParams.hashCode()), null); }
/** * Sets text letter-spacing in em units. Typical values for slight expansion will be around 0.05. * Negative values tighten text. * * @param letterSpacing A text letter-space value in ems. * @see #getLetterSpacing() */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public TextLayoutBuilder setLetterSpacing(float letterSpacing) { if (getLetterSpacing() != letterSpacing) { mParams.createNewPaintIfNeeded(); mParams.paint.setLetterSpacing(letterSpacing); mSavedLayout = null; } return this; }
/** * Sets the density of this layout. This should typically be set to your current display's density * * @param density The density desired * @return This {@link TextLayoutBuilder} */ public TextLayoutBuilder setDensity(float density) { if (mParams.paint.density != density) { mParams.createNewPaintIfNeeded(); mParams.paint.density = density; mSavedLayout = null; } return this; }
/** * Sets the text size for the layout. * * @param size The text size in pixels * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setTextSize(int size) { if (mParams.paint.getTextSize() != size) { mParams.createNewPaintIfNeeded(); mParams.paint.setTextSize(size); mSavedLayout = null; } return this; }
/** * Sets the text color for the layout. * * @param colorStateList The text color state list for the layout * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setTextColor(ColorStateList colorStateList) { mParams.createNewPaintIfNeeded(); mParams.color = colorStateList; mParams.paint.setColor(mParams.color != null ? mParams.color.getDefaultColor() : Color.BLACK); mSavedLayout = null; return this; }
/** * Updates the text colors based on the drawable state. * * @param drawableState The current drawable state of the View holding this layout * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setDrawableState(int[] drawableState) { mParams.createNewPaintIfNeeded(); mParams.paint.drawableState = drawableState; if (mParams.color != null && mParams.color.isStateful()) { int color = mParams.color.getColorForState(drawableState, 0); mParams.paint.setColor(color); mSavedLayout = null; } return this; }
/** * Sets the typeface used by this TextLayoutBuilder. * * @param typeface The typeface for this TextLayoutBuilder * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setTypeface(Typeface typeface) { if (mParams.paint.getTypeface() != typeface) { mParams.createNewPaintIfNeeded(); mParams.paint.setTypeface(typeface); mSavedLayout = null; } return this; }
@Test public void testNoCaching() { mLayout = mBuilder.setShouldCacheLayout(false).build(); Layout newLayout = mBuilder.build(); assertNotEquals(mLayout, newLayout); assertEquals(mBuilder.sCache.size(), 0); assertEquals(mBuilder.sCache.get(mBuilder.mParams.hashCode()), null); }
/** * Returns the line height for this TextLayoutBuilder. * * @return The line height used by this TextLayoutBuilder */ public float getLineHeight() { return mParams.getLineHeight(); }
/** * Sets the text color for the layout. * * @param color The text color for the layout * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setTextColor(@ColorInt int color) { mParams.createNewPaintIfNeeded(); mParams.color = null; mParams.paint.setColor(color); mSavedLayout = null; return this; }
/** * Sets the link color for the text in the layout. * * @param linkColor The link color * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setLinkColor(@ColorInt int linkColor) { if (mParams.paint.linkColor != linkColor) { mParams.createNewPaintIfNeeded(); mParams.paint.linkColor = linkColor; mSavedLayout = null; } return this; }
@Test public void testCachingSpannableString() { SpannableStringBuilder spannable = new SpannableStringBuilder("This is a bold text"); spannable.setSpan(new StyleSpan(Typeface.BOLD), 10, 13, Spanned.SPAN_INCLUSIVE_INCLUSIVE); mLayout = mBuilder.setText(spannable).setShouldCacheLayout(true).build(); assertEquals(mBuilder.sCache.size(), 1); assertEquals(mBuilder.sCache.get(mBuilder.mParams.hashCode()), mLayout); }
@Test public void testCaching() { mLayout = mBuilder.setShouldCacheLayout(true).build(); Layout newLayout = mBuilder.build(); assertEquals(mLayout, newLayout); assertEquals(mBuilder.sCache.size(), 1); assertEquals(mBuilder.sCache.get(mBuilder.mParams.hashCode()), mLayout); }