Refine search
public void draw(Canvas canvas) { final int saveCount = canvas.save(); if (mTextToDraw != null && mDrawTitle) { float x = mCurrentDrawX; final float descent; mTextPaint.setTextSize(mCurrentTextSize); if (drawTexture) { ascent = mTextureAscent * mScale; descent = mTextureDescent * mScale; } else { ascent = mTextPaint.ascent() * mScale; descent = mTextPaint.descent() * mScale; canvas.scale(mScale, mScale, x, y); canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint); } else { canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
public SectionItemDecoration(Context context, List<City> data) { this.mData = data; TypedValue typedValue = new TypedValue(); context.getTheme().resolveAttribute(R.attr.cpSectionBackground, typedValue, true); mBgColor = context.getResources().getColor(typedValue.resourceId); context.getTheme().resolveAttribute(R.attr.cpSectionHeight, typedValue, true); mSectionHeight = context.getResources().getDimensionPixelSize(typedValue.resourceId); context.getTheme().resolveAttribute(R.attr.cpSectionTextSize, typedValue, true); mTextSize = context.getResources().getDimensionPixelSize(typedValue.resourceId); context.getTheme().resolveAttribute(R.attr.cpSectionTextColor, typedValue, true); mTextColor = context.getResources().getColor(typedValue.resourceId); mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mBgPaint.setColor(mBgColor); mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setTextSize(mTextSize); mTextPaint.setColor(mTextColor); mBounds = new Rect(); }
@Override public void updateDrawState(TextPaint drawState) { drawState.setColor(isPressed ? pressedTextColor : normalTextColor); drawState.bgColor = isPressed ? pressedBackgroundColor : 0; drawState.setUnderlineText(!isPressed); } }
public CollapsingTextHelper(View view) { mView = view; mTextPaint = new TextPaint(); mTextPaint.setAntiAlias(true); mCollapsedBounds = new Rect(); mExpandedBounds = new Rect(); mCurrentBounds = new RectF(); }
touchableSpan = new TouchableSpan() { public boolean onTouch(View widget, MotionEvent m) { ... } public void updateDrawState(TextPaint ds) { ds.setUnderlineText(false); ds.setAntiAlias(true); } }; String rv = "Text to span"; text = new SpannableString(rv); text.setSpan(touchableSpan, begin, end, 0); tv.setText(text, BufferType.SPANNABLE);
Rect canvasBounds = new Rect(); TextPaint badgeTextPaint = new TextPaint(); badgePaint.setFlags(Paint.ANTI_ALIAS_FLAG); badgeTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG); badgeTextPaint.setTextAlign(Paint.Align.CENTER); badgeTextPaint.setTextSize((float) (height * 0.7)); badgeTextPaint.setColor(brand.defaultFill(context)); badgeTextPaint.setColor(brand.defaultTextColor(context)); int rectLength = (int) badgeTextPaint.measureText(badgeText); Canvas canvas = new Canvas(canvasBitmap); canvas.getClipBounds(canvasBounds); rect.bottom = canvasBounds.height(); canvas.drawCircle(firstCircleDx, circleDy, circleRadius, badgePaint); canvas.drawRect(rect, badgePaint); canvas.drawCircle(secondCircleDx, circleDy, circleRadius, badgePaint); canvas.drawText(badgeText, canvasBounds.width() / 2, canvasBounds.height() / 2 - ((badgeTextPaint.descent() + badgeTextPaint.ascent()) / 2), badgeTextPaint);
mTextPaint = new TextPaint(); mTextPaint.setAntiAlias(true); mTextPaint.setTextSize(DEFAULT_TEXT_SIZE); mTextPaint.setColor(0xFF000000); mTextPaint.setTextAlign(Align.CENTER); setPadding(3, 3, 3, 3); mTextPaint.setTextSize(size); requestLayout(); invalidate(); mTextPaint.setColor(color); invalidate(); protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mTextPaint.getTextBounds(mText, 0, mText.length(), text_bounds); setMeasuredDimension( measureWidth(widthMeasureSpec), } else { result = text_bounds.height() + getPaddingLeft() + getPaddingRight(); int specSize = MeasureSpec.getSize(measureSpec); mAscent = (int) mTextPaint.ascent(); if (specMode == MeasureSpec.EXACTLY) {
GifBadge(Context context) { if (bitmap == null) { final DisplayMetrics dm = context.getResources().getDisplayMetrics(); final float density = dm.density; final float scaledDensity = dm.scaledDensity; final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint .SUBPIXEL_TEXT_FLAG); textPaint.setTypeface(Typeface.create(TYPEFACE, TYPEFACE_STYLE)); textPaint.setTextSize(TEXT_SIZE * scaledDensity); final float padding = PADDING * density; final float cornerRadius = CORNER_RADIUS * density; final Rect textBounds = new Rect(); textPaint.getTextBounds(GIF, 0, GIF.length(), textBounds); height = (int) (padding + textBounds.height() + padding); width = (int) (padding + textBounds.width() + padding); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setHasAlpha(true); final Canvas canvas = new Canvas(bitmap); final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); backgroundPaint.setColor(BACKGROUND_COLOR); if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){ canvas.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius, backgroundPaint); } // punch out the word 'GIF', leaving transparency textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawText(GIF, padding, height - padding, textPaint); } paint = new Paint(); }
debugTextPaint = new TextPaint(); debugTextPaint.setColor(0xFFFF0000); debugTextPaint.setTextSize(UiUtil.sp(getContext(), 16)); c.drawRect(textBounds, debugPaint); c.drawRect(targetBounds, debugPaint); c.drawCircle(outerCircleCenter[0], outerCircleCenter[1], 10, debugPaint); c.drawCircle(outerCircleCenter[0], outerCircleCenter[1], calculatedOuterCircleRadius - CIRCLE_PADDING, debugPaint); c.drawCircle(targetBounds.centerX(), targetBounds.centerY(), TARGET_RADIUS + TARGET_PADDING, debugPaint); "Text bounds: " + textBounds.toShortString() + "\n" + "Target bounds: " + targetBounds.toShortString() + "\n" + "Center: " + outerCircleCenter[0] + " " + outerCircleCenter[1] + "\n" +
int saveCount = canvas.save(); canvas.translate(textLeft, textTop); textPaint.setStrokeJoin(Join.ROUND); textPaint.setStrokeWidth(outlineWidth); textPaint.setColor(edgeColor); textPaint.setStyle(Style.FILL_AND_STROKE); layout.draw(canvas); } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) { textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor); } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) { int colorDown = raised ? edgeColor : Color.WHITE; float offset = shadowRadius / 2f; textPaint.setColor(foregroundColor); textPaint.setStyle(Style.FILL); textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp); layout.draw(canvas); textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown); textPaint.setColor(foregroundColor); textPaint.setStyle(Style.FILL); layout.draw(canvas); textPaint.setShadowLayer(0, 0, 0, 0); canvas.restoreToCount(saveCount);
private final TextPaint mPaint = new TextPaint(); private final Rect mBounds = new Rect(); private final Canvas mCanvas = new Canvas(); final Resources res = context.getResources(); mPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); mPaint.setColor(Color.WHITE); mPaint.setTextAlign(Align.CENTER); mPaint.setAntiAlias(true); mPaint.setTextSize(mTileLetterFontSize); mPaint.getTextBounds(mFirstChar, 0, 1, mBounds); c.drawText(mFirstChar, 0, 1, 0 + width / 2, 0 + height / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint);
float drawHeight; final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer); drawWidth = textLayout.getWidth(); drawHeight = textLayout.getLineCount() * lineHeight; Paint.Align originalTextAlign = paint.getTextAlign(); paint.setTextAlign(Paint.Align.LEFT); c.save(); c.translate(translateX, translateY); c.rotate(angleDegrees); c.translate(drawOffsetX, drawOffsetY); textLayout.draw(c); c.restore(); paint.setTextAlign(originalTextAlign);
private void ensureExpandedTexture() { if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) { return; } mTextPaint.setTextSize(mExpandedTextSize); mTextPaint.setColor(mExpandedTextColor); mTextureAscent = mTextPaint.ascent(); mTextureDescent = mTextPaint.descent(); final int w = Math.round(mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length())); final int h = Math.round(mTextureDescent - mTextureAscent); if (w <= 0 && h <= 0) { return; // If the width or height are 0, return } mExpandedTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(mExpandedTitleTexture); c.drawText(mTextToDraw, 0, mTextToDraw.length(), 0, h - mTextPaint.descent(), mTextPaint); if (mTexturePaint == null) { // Make sure we have a paint mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); } }
/** * @param displayName The name used to create the letter for the tile * @return A circular {@link Bitmap} that contains a letter used in the English * alphabet or digit, if there is no letter or digit available, a * default image is shown instead */ public Bitmap getCircularLetterTile(String displayName) { // workaround if(displayName == null || displayName.length() == 0) displayName = "."; final Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); final char firstChar = displayName.charAt(0); final Canvas c = mCanvas; c.setBitmap(bitmap); c.drawColor(pickColor(displayName)); if (isLetterOrDigit(firstChar)) { mFirstChar[0] = Character.toUpperCase(firstChar); mPaint.setTextSize(mTileLetterFontSize); mPaint.getTextBounds(mFirstChar, 0, 1, mBounds); c.drawText(mFirstChar, 0, 1, mWidth / 2, mHeight / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint); } else { // (32 - 24) / 2 = 4 c.drawBitmap(mDefaultBitmap, ViewUtil.dpToPx(4), ViewUtil.dpToPx(4), null); } return getCircularBitmap(bitmap); }
@Override public void draw(Canvas canvas) { // Center drawable within available bounds int boundsWidth = getBounds().width(); int boundsHeight = getBounds().height(); int dimen = Math.min(boundsWidth, boundsHeight); paint.setTextSize(dimen); String textValue = MaterialIconUtils.getIconString(icon.ordinal()); paint.getTextBounds(textValue, 0, 1, mCachedRect); float textBottom = getBounds().top + (boundsHeight - mCachedRect.height()) / 2f + mCachedRect.height() - mCachedRect.bottom; canvas.drawText(textValue, getBounds().left + boundsWidth / 2f, textBottom, paint); }
@Override public void onDraw(Canvas canvas, int startTop, int startBottom) { int tokenStart = getTokenForVertical(startTop, TokenPosition.START_OF_LINE); int tokenEnd = getTokenForVertical(startBottom, TokenPosition.END_OF_LINE); for (int i = Math.max(0, tokenStart - 25); i < tokenEnd + 25 && i < tokens.length; i++) { Token token = tokens[i]; token.draw(canvas, -startTop, paint, params); if (params.debugging) { if (token instanceof LineBreak) { int lastColor = paint.getColor(); boolean lastFakeBold = paint.isFakeBoldText(); Paint.Style lastStyle = paint.getStyle(); Paint.Align lastAlign = paint.getTextAlign(); paint.setColor(Color.YELLOW); paint.setStyle(Paint.Style.FILL); canvas.drawRect(params.insetPaddingLeft, token.y - startTop - getTokenAscent(0), params.parentWidth - params.insetPaddingRight, token.y - startTop + getTokenDescent(0), paint); paint.setColor(Color.BLACK); paint.setFakeBoldText(true); paint.setTextAlign(Paint.Align.CENTER); canvas.drawText("LINEBREAK", params.insetPaddingLeft + (params.parentWidth - params.insetPaddingRight - params.insetPaddingLeft) / 2, token.y - startTop, paint); paint.setStyle(lastStyle); paint.setColor(lastColor); paint.setTextAlign(lastAlign); paint.setFakeBoldText(lastFakeBold); } } } }
targetBounds = new Rect(); drawingBounds = new Rect(); titlePaint = new TextPaint(); titlePaint.setTextSize(target.titleTextSizePx(context)); titlePaint.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL)); titlePaint.setAntiAlias(true); descriptionPaint = new TextPaint(); descriptionPaint.setTextSize(target.descriptionTextSizePx(context)); descriptionPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL)); descriptionPaint.setAntiAlias(true); descriptionPaint.setAlpha((int) (0.54f * 255.0f));
/** * Update the TextSize */ private void updateTextSize(@NonNull Rect viewBounds) { float textSize = (float) viewBounds.height() * (mRespectFontBounds ? 1 : 2); mIconBrush.getPaint().setTextSize(textSize); String textValue = mIcon != null ? String.valueOf(mIcon.getCharacter()) : String.valueOf(mPlainIcon); mIconBrush.getPaint().getTextPath(textValue, 0, textValue.length(), 0, viewBounds.height(), mPath); mPath.computeBounds(mPathBounds, true); if (!mRespectFontBounds) { float deltaWidth = ((float) mPaddingBounds.width() / mPathBounds.width()); float deltaHeight = ((float) mPaddingBounds.height() / mPathBounds.height()); float delta = (deltaWidth < deltaHeight) ? deltaWidth : deltaHeight; textSize *= delta; mIconBrush.getPaint().setTextSize(textSize); mIconBrush.getPaint().getTextPath(textValue, 0, textValue.length(), 0, viewBounds.height(), mPath); mPath.computeBounds(mPathBounds, true); } }
@Test public void testDrawSpannableString() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { SpannableString span = new SpannableString("*hint"); span.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); customView.setHint(span); Method method = FloatingLabelSpinner.class.getDeclaredMethod("drawSpannableString", Canvas.class, CharSequence.class, TextPaint.class, int.class, int.class); method.setAccessible(true); method.invoke(customView, new Canvas(), span, new TextPaint(), 0, 0); }