List of usage examples for android.graphics Canvas drawText
public void drawText(@NonNull CharSequence text, int start, int end, float x, float y, @NonNull Paint paint)
From source file:com.tr4android.support.extension.widget.CollapsingTextHelper.java
public void draw(Canvas canvas) { final int saveCount = canvas.save(); if (mTextToDraw != null && mDrawTitle) { float x = mCurrentDrawX; float y = mCurrentDrawY; final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null; final float ascent; final float descent; // Update the TextPaint to the current text size mTextPaint.setTextSize(mCurrentTextSize); if (drawTexture) { ascent = mTextureAscent * mScale; descent = mTextureDescent * mScale; } else {/*from w ww. jav a2s .c om*/ ascent = mTextPaint.ascent() * mScale; descent = mTextPaint.descent() * mScale; } if (DEBUG_DRAW) { // Just a debug tool, which drawn a Magneta rect in the text bounds canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent, DEBUG_DRAW_PAINT); } if (drawTexture) { y += ascent; } if (mScale != 1f) { canvas.scale(mScale, mScale, x, y); } if (drawTexture) { // If we should use a texture, draw it instead of text canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint); } else { canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint); } } canvas.restoreToCount(saveCount); }
From source file:android.support.design.widget.CustomCollapsingTextHelper.java
public void draw(Canvas canvas) { final int saveCount = canvas.save(); if (mTextToDraw != null && mDrawTitle) { float x = mCurrentDrawX; float y = mCurrentDrawY; float subY = mCurrentSubY; final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null; final float ascent; final float descent; if (drawTexture) { ascent = mTextureAscent * mScale; descent = mTextureDescent * mScale; } else {/*from w w w . j a va 2 s . c om*/ ascent = mTitlePaint.ascent() * mScale; descent = mTitlePaint.descent() * mScale; } if (DEBUG_DRAW) { // Just a debug tool, which drawn a magenta rect in the text bounds canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent, DEBUG_DRAW_PAINT); } if (drawTexture) { y += ascent; } //region modification final int saveCountSub = canvas.save(); if (mSub != null) { if (mSubScale != 1f) { canvas.scale(mSubScale, mSubScale, x, subY); } canvas.drawText(mSub, 0, mSub.length(), x, subY, mSubPaint); canvas.restoreToCount(saveCountSub); } //endregion if (mScale != 1f) { canvas.scale(mScale, mScale, x, y); } if (drawTexture) { // If we should use a texture, draw it instead of text canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint); } else { canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTitlePaint); } } canvas.restoreToCount(saveCount); }
From source file:com.android.contacts.ShortcutIntentBuilder.java
/** * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone * number, and if there is a photo also adds the call action icon. *///from w w w .ja v a2 s.c om private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) { final Resources r = mContext.getResources(); final float density = r.getDisplayMetrics().density; final Drawable phoneDrawable = r.getDrawableForDensity(actionResId, mIconDensity); // These icons have the same height and width so either is fine for the size. final Bitmap phoneIcon = BitmapUtil.drawableToBitmap(phoneDrawable, phoneDrawable.getIntrinsicHeight()); Bitmap icon = generateQuickContactIcon(photo); Canvas canvas = new Canvas(icon); // Copy in the photo Paint photoPaint = new Paint(); photoPaint.setDither(true); photoPaint.setFilterBitmap(true); Rect dst = new Rect(0, 0, mIconSize, mIconSize); // Create an overlay for the phone number type if we're pre-O. O created shortcuts have the // app badge which overlaps the type overlay. CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel); if (!BuildCompat.isAtLeastO() && overlay != null) { TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG); textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size)); textPaint.setColor(r.getColor(R.color.textColorIconOverlay)); textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow)); final FontMetricsInt fmi = textPaint.getFontMetricsInt(); // First fill in a darker background around the text to be drawn final Paint workPaint = new Paint(); workPaint.setColor(mOverlayTextBackgroundColor); workPaint.setStyle(Paint.Style.FILL); final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding); final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2; dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize); canvas.drawRect(dst, workPaint); overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END); final float textWidth = textPaint.measureText(overlay, 0, overlay.length()); canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2, mIconSize - fmi.descent - textPadding, textPaint); } // Draw the phone action icon as an overlay int iconWidth = icon.getWidth(); if (BuildCompat.isAtLeastO()) { // On O we need to calculate where the phone icon goes slightly differently. The whole // canvas area is 108dp, a centered circle with a diameter of 66dp is the "safe zone". // So we start the drawing the phone icon at // 108dp - 21 dp (distance from right edge of safe zone to the edge of the canvas) // - 24 dp (size of the phone icon) on the x axis (left) // The y axis is simply 21dp for the distance to the safe zone (top). // See go/o-icons-eng for more details and a handy picture. final int left = (int) (mIconSize - (45 * density)); final int top = (int) (21 * density); canvas.drawBitmap(phoneIcon, left, top, photoPaint); } else { dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density))); canvas.drawBitmap(phoneIcon, null, dst, photoPaint); } canvas.setBitmap(null); return icon; }
From source file:com.muzakki.ahmad.widget.CollapsingTextHelper.java
public void draw(Canvas canvas) { final int saveCount = canvas.save(); if (mTextToDraw != null && mDrawTitle) { float x = mCurrentDrawX; float y = mCurrentDrawY; float subX = mCurrentSubX; float subY = mCurrentSubY; final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null; final float ascent; final float descent; if (drawTexture) { ascent = mTextureAscent * mScale; descent = mTextureDescent * mScale; } else {/*w ww .jav a 2s . c om*/ ascent = mTextPaint.ascent() * mScale; descent = mTextPaint.descent() * mScale; } if (DEBUG_DRAW) { // Just a debug tool, which drawn a magenta rect in the text bounds canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent, DEBUG_DRAW_PAINT); } if (drawTexture) { y += ascent; } // begin modification // draw subtitle final int saveCountSub = canvas.save(); if (mSub != null) { if (mSubScale != 1f) { canvas.scale(mSubScale, mSubScale, subX, subY); } canvas.drawText(mSub, 0, mSub.length(), subX, subY, mSubPaint); canvas.restoreToCount(saveCountSub); } // end modification if (mScale != 1f) { canvas.scale(mScale, mScale, x, y); } if (drawTexture) { // If we should use a texture, draw it instead of text canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint); } else { canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint); } } canvas.restoreToCount(saveCount); }
From source file:com.yek.keyboard.keyboards.views.AnyKeyboardView.java
@Override public void onDraw(Canvas canvas) { final boolean keyboardChanged = mKeyboardChanged; super.onDraw(canvas); // switching animation if (mAnimationLevel != AskPrefs.AnimationsLevel.None && keyboardChanged && (mInAnimation != null)) { startAnimation(mInAnimation);//from w w w . j a va 2s.c o m mInAnimation = null; } // text pop out animation if (mPopOutText != null && mAnimationLevel != AskPrefs.AnimationsLevel.None) { final int maxVerticalTravel = getHeight() / 2; final long currentAnimationTime = SystemClock.elapsedRealtime() - mPopOutTime; if (currentAnimationTime > TEXT_POP_OUT_ANIMATION_DURATION) { mPopOutText = null; } else { final float popOutPositionProgress = ((float) currentAnimationTime) / ((float) TEXT_POP_OUT_ANIMATION_DURATION); final float animationProgress = mPopOutTextReverting ? 1f - popOutPositionProgress : popOutPositionProgress; final float animationInterpolatorPosition = getPopOutAnimationInterpolator(false, animationProgress); final int y = mPopOutStartPoint.y - (int) (maxVerticalTravel * animationInterpolatorPosition); final int x = mPopOutStartPoint.x; final int alpha = mPopOutTextReverting ? (int) (255 * animationProgress) : 255 - (int) (255 * animationProgress); // drawing setPaintToKeyText(mPaint); // will disappear over time mPaint.setAlpha(alpha); mPaint.setShadowLayer(5, 0, 0, Color.BLACK); // will grow over time mPaint.setTextSize(mPaint.getTextSize() * (1.0f + animationInterpolatorPosition)); canvas.translate(x, y); canvas.drawText(mPopOutText, 0, mPopOutText.length(), 0, 0, mPaint); canvas.translate(-x, -y); //we're doing reverting twice much faster if (mPopOutTextReverting) { mPopOutTime = mPopOutTime - (int) (60 * popOutPositionProgress); } // next frame postInvalidateDelayed(1000 / 60);// doing 60 frames per second; } } //showing alpha/beta icon if needed if (BuildConfig.TESTING_BUILD) { final float textSizeForBuildSign = mPaint.getTextSize() / 2f; final float x = getWidth() - (mBuildTypeSignText.length() * textSizeForBuildSign); final float y = getHeight() - textSizeForBuildSign - getPaddingBottom(); canvas.translate(x, y); canvas.drawText(mBuildTypeSignText, 0, mBuildTypeSignText.length(), 0, 0, mBuildTypeSignPaint); canvas.translate(-x, -y); } }
From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardView.java
@Override public void onDraw(Canvas canvas) { final boolean keyboardChanged = mKeyboardChanged; super.onDraw(canvas); // switching animation if (mAnimationLevel != AnimationsLevel.None && keyboardChanged && (mInAnimation != null)) { startAnimation(mInAnimation);/*w w w. ja v a2 s . c o m*/ mInAnimation = null; } // text pop out animation if (mPopOutText != null && mAnimationLevel != AnimationsLevel.None) { final int maxVerticalTravel = getHeight() / 2; final long currentAnimationTime = SystemClock.elapsedRealtime() - mPopOutTime; if (currentAnimationTime > TEXT_POP_OUT_ANIMATION_DURATION) { mPopOutText = null; } else { final float popOutPositionProgress = ((float) currentAnimationTime) / ((float) TEXT_POP_OUT_ANIMATION_DURATION); final float animationProgress = mPopOutTextReverting ? 1f - popOutPositionProgress : popOutPositionProgress; final float animationInterpolatorPosition = getPopOutAnimationInterpolator(false, animationProgress); final int y = mPopOutStartPoint.y - (int) (maxVerticalTravel * animationInterpolatorPosition); final int x = mPopOutStartPoint.x; final int alpha = mPopOutTextReverting ? (int) (255 * animationProgress) : 255 - (int) (255 * animationProgress); // drawing setPaintToKeyText(mPaint); // will disappear over time mPaint.setAlpha(alpha); mPaint.setShadowLayer(5, 0, 0, Color.BLACK); // will grow over time mPaint.setTextSize(mPaint.getTextSize() * (1.0f + animationInterpolatorPosition)); canvas.translate(x, y); canvas.drawText(mPopOutText, 0, mPopOutText.length(), 0, 0, mPaint); canvas.translate(-x, -y); //we're doing reverting twice much faster if (mPopOutTextReverting) { mPopOutTime = mPopOutTime - (int) (60 * popOutPositionProgress); } // next frame postInvalidateDelayed(1000 / 60);// doing 60 frames per second; } } //showing alpha/beta icon if needed if (BuildConfig.TESTING_BUILD) { final float textSizeForBuildSign = mPaint.getTextSize() / 2f; final float x = getWidth() - (mBuildTypeSignText.length() * textSizeForBuildSign); final float y = getHeight() - textSizeForBuildSign - getPaddingBottom(); canvas.translate(x, y); canvas.drawText(mBuildTypeSignText, 0, mBuildTypeSignText.length(), 0, 0, mBuildTypeSignPaint); canvas.translate(-x, -y); } }
From source file:com.android.ex.chips.RecipientEditTextView.java
private MoreImageSpan createMoreSpan(final int count) { final String moreText = String.format(mMoreItem.getText().toString(), count); final TextPaint morePaint = new TextPaint(getPaint()); morePaint.setTextSize(mMoreItem.getTextSize()); morePaint.setColor(mMoreItem.getCurrentTextColor()); final int width = (int) morePaint.measureText(moreText) + mMoreItem.getPaddingLeft() + mMoreItem.getPaddingRight(); final int height = getLineHeight(); final Bitmap drawable = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(drawable); int adjustedHeight = height; final Layout layout = getLayout(); if (layout != null) adjustedHeight -= layout.getLineDescent(0); canvas.drawText(moreText, 0, moreText.length(), 0, adjustedHeight, morePaint); final Drawable result = new BitmapDrawable(getResources(), drawable); result.setBounds(0, 0, width, height); return new MoreImageSpan(result); }
From source file:com.android.ex.chips.RecipientEditTextView.java
private Bitmap createSelectedChip(final RecipientEntry contact, final TextPaint paint) { // Ellipsize the text so that it takes AT MOST the entire width of the // autocomplete text entry area. Make sure to leave space for padding // on the sides. final int height = (int) mChipHeight; final int deleteWidth = height; final float[] widths = new float[1]; paint.getTextWidths(" ", widths); final String createChipDisplayText = createChipDisplayText(contact); final float calculateAvailableWidth = calculateAvailableWidth(); final CharSequence ellipsizedText = ellipsizeText(createChipDisplayText, paint, calculateAvailableWidth - deleteWidth - widths[0]); // Make sure there is a minimum chip width so the user can ALWAYS // tap a chip without difficulty. final int width = Math.max(deleteWidth * 2, (int) Math.floor(paint.measureText(ellipsizedText, 0, ellipsizedText.length())) + mChipPadding * 2 + deleteWidth);// w w w . j a v a2s. c o m // Create the background of the chip. final Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(tmpBitmap); if (mChipBackgroundPressed != null) { mChipBackgroundPressed.setBounds(0, 0, width, height); mChipBackgroundPressed.draw(canvas); paint.setColor(sSelectedTextColor); // Vertically center the text in the chip. canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mChipPadding, getTextYOffset((String) ellipsizedText, paint, height), paint); // Make the delete a square. final Rect backgroundPadding = new Rect(); mChipBackgroundPressed.getPadding(backgroundPadding); mChipDelete.setBounds(width - deleteWidth + backgroundPadding.left, 0 + backgroundPadding.top, width - backgroundPadding.right, height - backgroundPadding.bottom); mChipDelete.draw(canvas); } else Log.w(TAG, "Unable to draw a background for the chips as it was never set"); return tmpBitmap; }
From source file:com.seek.ruler.SimpleRulerView.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /**// w ww.j a va2 s. c o m * here is the start the selected the end * / / / * |||||||||||||||||||||||||||||||||||||||||||||||||| * | | | | | | | | | | | * 0 1 2 * \____mViewScopeSize_____/ * */ int start = mSelectedIndex - mViewScopeSize; int end = mSelectedIndex + mViewScopeSize; start = Math.max(start, -mViewScopeSize * 2); end = Math.min(end, mRulerCount + mViewScopeSize * 2); if (mSelectedIndex == mMaxValue) { end += mViewScopeSize; } else if (mSelectedIndex == mMinValue) { start -= mViewScopeSize; } float x = start * mIntervalDis; float markHeight = mHeight - mTextSize; for (int i = start; i < end; i++) { // draw line int remainderBy2 = i % 2; int remainderBy5 = i % 5; if (i == mSelectedIndex) { mRulerPaint.setColor(mHighlightColor); } else { mRulerPaint.setColor(mRulerColor); } if (remainderBy2 == 0 && remainderBy5 == 0) { canvas.drawLine(x, 0, x, 0 + markHeight, mRulerPaint); } else if (remainderBy2 != 0 && remainderBy5 == 0) { canvas.drawLine(x, 0, x, markHeight * 3 / 4, mRulerPaint); } else { canvas.drawLine(x, 0, x, markHeight / 2, mRulerPaint); } // draw text if (mRulerCount > 0 && i >= 0 && i < mRulerCount) { mTextPaint.setColor(mTextColor); if (mSelectedIndex == i) { mTextPaint.setColor(mHighlightColor); } if (i % 10 == 0) { String text = null; if (mTextList != null && mTextList.size() > 0) { int index = i / 10; if (index < mTextList.size()) { text = mTextList.get(index); } else { text = ""; } } else { text = format(i * mIntervalValue + mMinValue); } canvas.drawText(text, 0, text.length(), x, mHeight, mTextPaint); } } x += mIntervalDis; } }
From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java
public void draw(Canvas canvas) { final int saveCount = canvas.save(); if (mTextToDraw != null && mDrawTitle) { float x = mCurrentDrawX; float y = mCurrentDrawY; final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null; final float ascent; // MODIFICATION: removed now unused "descent" variable declaration // Update the TextPaint to the current text size mTextPaint.setTextSize(mCurrentTextSize); // BEGIN MODIFICATION: new drawing code if (drawTexture) { ascent = 0;//ww w .j a va 2 s. co m } else { ascent = mTextPaint.ascent() * mScale; } if (DEBUG_DRAW) { // Just a debug tool, which drawn a Magneta rect in the text bounds canvas.drawRect(mCurrentBounds.left, y, mCurrentBounds.right, y + mTextLayout.getHeight() * mScale, DEBUG_DRAW_PAINT); } if (mScale != 1f) { canvas.scale(mScale, mScale, x, y); } // Compute where to draw mTextLayout for this frame final float currentExpandedX = mCurrentDrawX + mTextLayout.getLineLeft(0) - mExpandedFirstLineDrawX * 2; if (drawTexture) { // If we should use a texture, draw it instead of text // Expanded text mTexturePaint.setAlpha((int) (mExpandedTextBlend * 255)); canvas.drawBitmap(mExpandedTitleTexture, currentExpandedX, y, mTexturePaint); // Collapsed text mTexturePaint.setAlpha((int) (mCollapsedTextBlend * 255)); canvas.drawBitmap(mCollapsedTitleTexture, x, y, mTexturePaint); // Cross-section between both texts (should stay at alpha = 255) mTexturePaint.setAlpha(255); canvas.drawBitmap(mCrossSectionTitleTexture, x, y, mTexturePaint); } else { // positon expanded text appropriately canvas.translate(currentExpandedX, y); // Expanded text mTextPaint.setAlpha((int) (mExpandedTextBlend * 255)); mTextLayout.draw(canvas); // position the overlays canvas.translate(x - currentExpandedX, 0); // Collapsed text mTextPaint.setAlpha((int) (mCollapsedTextBlend * 255)); canvas.drawText(mTextToDrawCollapsed, 0, mTextToDrawCollapsed.length(), 0, -ascent / mScale, mTextPaint); // Cross-section between both texts (should stay at alpha = 255) mTextPaint.setAlpha(255); canvas.drawText(mTextToDraw, mTextLayout.getLineStart(0), mTextLayout.getLineEnd(0), 0, -ascent / mScale, mTextPaint); } // END MODIFICATION } canvas.restoreToCount(saveCount); }