List of usage examples for android.graphics Canvas drawText
public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint)
From source file:com.camnter.easyrecyclerviewsidebar.EasyRecyclerViewSidebar.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int viewWidth = this.getWidth(); int viewHeight = this.getHeight(); int viewHalfWidth = viewWidth / 2; this.sectionHeight = viewHeight / MAX_SECTION_COUNT; boolean isPreviousImage = false; boolean isPreviousLetter = false; float allSectionHeight; if (this.sections.size() > 0) { allSectionHeight = this.sectionHeight * this.sections.size(); this.drawBeginY = (viewHeight - allSectionHeight) / 2; this.drawEndY = this.drawBeginY + allSectionHeight; float top = viewHeight / 2 - allSectionHeight / 2 + this.sectionHeight / 2 - this.sectionFontSize / 2; for (int i = 0; i < this.sections.size(); i++) { EasySection section = this.sections.get(i); if (section instanceof EasyImageSection) { EasyImageSection imageSection = (EasyImageSection) section; this.setPaintShader(imageSection); if (isPreviousLetter) { top -= this.letterSize - (Math.max(this.letterHeight, this.sectionHeight) - Math.min(this.letterHeight, this.sectionHeight)); }// w w w .j a va2s . c om canvas.save(); canvas.translate(viewHalfWidth - this.letterSize / 2, top + this.sectionHeight * i); Paint imagePaint = this.imagePaints.get(imageSection.hashCode()); switch (imageSection.imageType) { case EasyImageSection.ROUND: { canvas.drawRoundRect(this.imageSectionRect, this.imageSectionBorderRadius, this.imageSectionBorderRadius, imagePaint); break; } case EasyImageSection.CIRCLE: { canvas.drawRoundRect(this.imageSectionRect, this.imageSectionCircleBorderRadius, this.imageSectionCircleBorderRadius, imagePaint); break; } } canvas.restore(); isPreviousImage = true; isPreviousLetter = false; } else { if (isPreviousImage) { top = top + this.letterSize; } canvas.drawText(section.letter, viewHalfWidth, top + this.sectionHeight * i, this.letterPaint); isPreviousImage = false; isPreviousLetter = true; } } } else { this.sectionHeight = 0; } }
From source file:com.jjoe64.graphview.GridLabelRenderer.java
/** * draws the vertical steps for the//from w ww . j ava2s. co m * second scale on the right side * * @param canvas canvas */ protected void drawVerticalStepsSecondScale(Canvas canvas) { if (mGraphView.mSecondScale == null) { return; } // draw only the vertical labels on the right float startLeft = mGraphView.getGraphContentLeft() + mGraphView.getGraphContentWidth(); mPaintLabel.setColor(getVerticalLabelsSecondScaleColor()); mPaintLabel.setTextAlign(getVerticalLabelsSecondScaleAlign()); for (Map.Entry<Integer, Double> e : mStepsVerticalSecondScale.entrySet()) { // draw label int labelsWidth = mLabelVerticalSecondScaleWidth; int labelsOffset = (int) startLeft; if (getVerticalLabelsSecondScaleAlign() == Paint.Align.RIGHT) { labelsOffset += labelsWidth; } else if (getVerticalLabelsSecondScaleAlign() == Paint.Align.CENTER) { labelsOffset += labelsWidth / 2; } float y = e.getKey(); String[] lines = mGraphView.mSecondScale.mLabelFormatter.formatLabel(e.getValue(), false).split("\n"); y += (lines.length * getTextSize() * 1.1f) / 2; // center text vertically for (int li = 0; li < lines.length; li++) { // for the last line y = height float y2 = y - (lines.length - li - 1) * getTextSize() * 1.1f; canvas.drawText(lines[li], labelsOffset, y2, mPaintLabel); } } }
From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
private void drawHours(Rect r, Canvas canvas, Paint p) { setupHourTextPaint(p);//from w w w .ja v a 2 s . c om int y = HOUR_GAP + mHoursTextHeight + HOURS_TOP_MARGIN; for (int i = 0; i < 24; i++) { String time = mHourStrs[i]; canvas.drawText(time, HOURS_LEFT_MARGIN, y, p); y += mCellHeight + HOUR_GAP; } }
From source file:com.kabootar.GlassMemeGenerator.ImageOverlay.java
/** * Draws the given string centered, as big as possible, on either the top or * bottom 20% of the image given./*from w w w .ja v a 2 s .c om*/ */ private static void drawStringCentered(Canvas g, String text, Bitmap image, boolean top, Context baseContext) throws InterruptedException { if (text == null) text = ""; int height = 0; int fontSize = MAX_FONT_SIZE; int maxCaptionHeight = image.getHeight() / 5; int maxLineWidth = image.getWidth() - SIDE_MARGIN * 2; String formattedString = ""; Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint stkPaint = new Paint(Paint.ANTI_ALIAS_FLAG); stkPaint.setStyle(STROKE); stkPaint.setStrokeWidth(8); stkPaint.setColor(Color.BLACK); //Typeface tf = Typeface.create("Arial", Typeface.BOLD); Typeface tf = Typeface.createFromAsset(baseContext.getAssets(), "fonts/impact.ttf"); paint.setTypeface(tf); stkPaint.setTypeface(tf); do { paint.setTextSize(fontSize); // first inject newlines into the text to wrap properly StringBuilder sb = new StringBuilder(); int left = 0; int right = text.length() - 1; while (left < right) { String substring = text.substring(left, right + 1); Rect stringBounds = new Rect(); paint.getTextBounds(substring, 0, substring.length(), stringBounds); while (stringBounds.width() > maxLineWidth) { if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); } // look for a space to break the line boolean spaceFound = false; for (int i = right; i > left; i--) { if (text.charAt(i) == ' ') { right = i - 1; spaceFound = true; break; } } substring = text.substring(left, right + 1); paint.getTextBounds(substring, 0, substring.length(), stringBounds); // If we're down to a single word and we are still too wide, // the font is just too big. if (!spaceFound && stringBounds.width() > maxLineWidth) { break; } } sb.append(substring).append("\n"); left = right + 2; right = text.length() - 1; } formattedString = sb.toString(); // now determine if this font size is too big for the allowed height height = 0; for (String line : formattedString.split("\n")) { Rect stringBounds = new Rect(); paint.getTextBounds(line, 0, line.length(), stringBounds); height += stringBounds.height(); } fontSize--; } while (height > maxCaptionHeight); // draw the string one line at a time int y = 0; if (top) { y = TOP_MARGIN; } else { y = image.getHeight() - height - BOTTOM_MARGIN; } for (String line : formattedString.split("\n")) { // Draw each string twice for a shadow effect Rect stringBounds = new Rect(); paint.getTextBounds(line, 0, line.length(), stringBounds); //paint.setColor(Color.BLACK); //g.drawText(line, (image.getWidth() - (int) stringBounds.width()) / 2 + 2, y + stringBounds.height() + 2, paint); paint.setColor(Color.WHITE); g.drawText(line, (image.getWidth() - (int) stringBounds.width()) / 2, y + stringBounds.height(), paint); //stroke Rect strokeBounds = new Rect(); stkPaint.setTextSize(fontSize); stkPaint.getTextBounds(line, 0, line.length(), strokeBounds); g.drawText(line, (image.getWidth() - (int) strokeBounds.width()) / 2, y + strokeBounds.height(), stkPaint); y += stringBounds.height(); } }
From source file:zxyilian.com.myapplication.ui.ProgressWheel.java
protected void onDraw(Canvas canvas) { super.onDraw(canvas); //Draw the inner circle canvas.drawArc(innerCircleBounds, 360, 360, false, circlePaint); //Draw the rim canvas.drawArc(circleBounds, 360, 360, false, rimPaint); canvas.drawArc(circleOuterContour, 360, 360, false, contourPaint); //canvas.drawArc(circleInnerContour, 360, 360, false, contourPaint); //Draw the bar if (isSpinning) { /* double cx = 0; double cy = 0;//from ww w . j a va2 s. c o m int width = getMeasuredWidth(); int height = getMeasuredHeight(); float r = (innerCircleBounds.right-innerCircleBounds.left)/2; if (anInt > 0 && anInt < 90) { cx = (float) (width + (r * Math.sin(anInt * Math.PI / 180))); cy = (float) (height - (r * Math.cos(anInt * Math.PI / 180))); } else if (anInt > 90 && anInt < 180) {// 90-180 anInt = 180 - anInt; cx = (float) (width + (r * Math.sin(anInt * Math.PI / 180))); cy = (float) (height + (r * Math.cos(anInt * Math.PI / 180))); } else if (anInt > 180 && anInt < 270) {//180-270 anInt = 270 - anInt; cx = (float) (width - (r * Math.cos(anInt * Math.PI / 180))); cy = (float) (height + (r * Math.sin(anInt * Math.PI / 180))); } else if (anInt > 270 && anInt < 360) {//270-360 anInt = 360 - anInt; cx = (float) (width - (r * Math.sin(anInt * Math.PI / 180))); cy = (float) (height - (r * Math.cos(anInt * Math.PI / 180))); } canvas.drawCircle( (float) cx,(float) cy,5,mCicle);*/ //canvas.drawRect(circleBounds,barPaint); canvas.drawArc(circleBounds, progress - 90, barLength, false, barPaint); } else { canvas.drawArc(circleBounds, -90, progress, false, barPaint); } //Draw the text (attempts to center it horizontally and vertically) float textHeight = textPaint.descent() - textPaint.ascent(); float verticalTextOffset = (textHeight / 2) - textPaint.descent(); for (String line : splitText) { float horizontalTextOffset = textPaint.measureText(line) / 2; canvas.drawText(line, this.getWidth() / 2 - horizontalTextOffset, this.getHeight() / 2 + verticalTextOffset, textPaint); } if (isSpinning) { scheduleRedraw(); } }
From source file:com.appsummary.luoxf.myappsummary.navigationtabstrip.NavigationTabStrip.java
@Override protected void onDraw(final Canvas canvas) { // Set bound of strip mStripBounds.set(mStripLeft - (mStripType == StripType.POINT ? mStripWeight * 0.5F : 0.0F), mStripGravity == StripGravity.BOTTOM ? mBounds.height() - mStripWeight : 0.0F, mStripRight - (mStripType == StripType.POINT ? mStripWeight * 0.5F : 0.0F), mStripGravity == StripGravity.BOTTOM ? mBounds.height() : mStripWeight); // Draw strip if (mCornersRadius == 0) canvas.drawRect(mStripBounds, mStripPaint); else// ww w.j a v a 2s .c o m canvas.drawRoundRect(mStripBounds, mCornersRadius, mCornersRadius, mStripPaint); // Draw tab titles for (int i = 0; i < mTitles.length; i++) { final String title = mTitles[i]; final float leftTitleOffset = (mTabSize * i) + (mTabSize * 0.5F); mTitlePaint.getTextBounds(title, 0, title.length(), mTitleBounds); final float topTitleOffset = (mBounds.height() - mStripWeight) * 0.5F + mTitleBounds.height() * 0.5F - mTitleBounds.bottom; // Get interpolated fraction for left last and current tab final float interpolation = mResizeInterpolator.getResizeInterpolation(mFraction, true); final float lastInterpolation = mResizeInterpolator.getResizeInterpolation(mFraction, false); // Check if we handle tab from touch on NTS or from ViewPager // There is a strange logic of ViewPager onPageScrolled method, so it is if (mIsSetIndexFromTabBar) { if (mIndex == i) updateCurrentTitle(interpolation); else if (mLastIndex == i) updateLastTitle(lastInterpolation); else updateInactiveTitle(); } else { if (i != mIndex && i != mIndex + 1) updateInactiveTitle(); else if (i == mIndex + 1) updateCurrentTitle(interpolation); else if (i == mIndex) updateLastTitle(lastInterpolation); } canvas.drawText(title, leftTitleOffset, topTitleOffset + (mStripGravity == StripGravity.TOP ? mStripWeight : 0.0F), mTitlePaint); } }
From source file:com.borax12.materialdaterangepicker.single.time.AmPmCirclesView.java
@Override public void onDraw(Canvas canvas) { int viewWidth = getWidth(); if (viewWidth == 0 || !mIsInitialized) { return;/*from ww w . ja v a 2s.c o m*/ } if (!mDrawValuesReady) { int layoutXCenter = getWidth() / 2; int layoutYCenter = getHeight() / 2; int circleRadius = (int) (Math.min(layoutXCenter, layoutYCenter) * mCircleRadiusMultiplier); mAmPmCircleRadius = (int) (circleRadius * mAmPmCircleRadiusMultiplier); layoutYCenter += mAmPmCircleRadius * 0.75; int textSize = mAmPmCircleRadius * 3 / 4; mPaint.setTextSize(textSize); // Line up the vertical center of the AM/PM circles with the bottom of the main circle. mAmPmYCenter = layoutYCenter - mAmPmCircleRadius / 2 + circleRadius; // Line up the horizontal edges of the AM/PM circles with the horizontal edges // of the main circle. mAmXCenter = layoutXCenter - circleRadius + mAmPmCircleRadius; mPmXCenter = layoutXCenter + circleRadius - mAmPmCircleRadius; mDrawValuesReady = true; } // We'll need to draw either a lighter blue (for selection), a darker blue (for touching) // or white (for not selected). int amColor = mUnselectedColor; int amAlpha = 255; int amTextColor = mAmPmTextColor; int pmColor = mUnselectedColor; int pmAlpha = 255; int pmTextColor = mAmPmTextColor; if (mAmOrPm == AM) { amColor = mSelectedColor; amAlpha = mSelectedAlpha; amTextColor = mAmPmSelectedTextColor; } else if (mAmOrPm == PM) { pmColor = mSelectedColor; pmAlpha = mSelectedAlpha; pmTextColor = mAmPmSelectedTextColor; } if (mAmOrPmPressed == AM) { amColor = mTouchedColor; amAlpha = mSelectedAlpha; } else if (mAmOrPmPressed == PM) { pmColor = mTouchedColor; pmAlpha = mSelectedAlpha; } if (mAmDisabled) { amColor = mUnselectedColor; amTextColor = mAmPmDisabledTextColor; } if (mPmDisabled) { pmColor = mUnselectedColor; pmTextColor = mAmPmDisabledTextColor; } // Draw the two circles. mPaint.setColor(amColor); mPaint.setAlpha(amAlpha); canvas.drawCircle(mAmXCenter, mAmPmYCenter, mAmPmCircleRadius, mPaint); mPaint.setColor(pmColor); mPaint.setAlpha(pmAlpha); canvas.drawCircle(mPmXCenter, mAmPmYCenter, mAmPmCircleRadius, mPaint); // Draw the AM/PM texts on top. mPaint.setColor(amTextColor); int textYCenter = mAmPmYCenter - (int) (mPaint.descent() + mPaint.ascent()) / 2; canvas.drawText(mAmText, mAmXCenter, textYCenter, mPaint); mPaint.setColor(pmTextColor); canvas.drawText(mPmText, mPmXCenter, textYCenter, mPaint); }
From source file:com.android.datetimepicker.time.AmPmCirclesView.java
@Override public void onDraw(Canvas canvas) { int viewWidth = getWidth(); if (viewWidth == 0 || !mIsInitialized) { return;/*w w w.ja v a 2 s. c om*/ } if (!mDrawValuesReady) { int layoutXCenter = getWidth() / 2; int layoutYCenter = getHeight() / 2; int circleRadius = (int) (Math.min(layoutXCenter, layoutYCenter) * mCircleRadiusMultiplier); mAmPmCircleRadius = (int) (circleRadius * mAmPmCircleRadiusMultiplier); layoutYCenter += mAmPmCircleRadius * 0.75; int textSize = mAmPmCircleRadius * 11 / 16; mPaint.setTextSize(textSize); // Line up the vertical center of the AM/PM circles with the bottom of the main circle. mAmPmYCenter = layoutYCenter - mAmPmCircleRadius / 2 + circleRadius; // Line up the horizontal edges of the AM/PM circles with the horizontal edges // of the main circle. mAmXCenter = layoutXCenter - circleRadius + mAmPmCircleRadius; mPmXCenter = layoutXCenter + circleRadius - mAmPmCircleRadius; mDrawValuesReady = true; } // We'll need to draw either a lighter blue (for selection), a darker blue (for touching) // or white (for not selected). int amColor = mUnselectedColor; int amAlpha = 255; int amTextColor = mAmPmTextColor; int pmColor = mUnselectedColor; int pmAlpha = 255; int pmTextColor = mAmPmTextColor; if (mAmOrPm == AM) { amColor = mSelectedColor; amAlpha = mSelectedAlpha; amTextColor = mAmPmSelectedTextColor; } else if (mAmOrPm == PM) { pmColor = mSelectedColor; pmAlpha = mSelectedAlpha; pmTextColor = mAmPmSelectedTextColor; } if (mAmOrPmPressed == AM) { amColor = mTouchedColor; amAlpha = mSelectedAlpha; } else if (mAmOrPmPressed == PM) { pmColor = mTouchedColor; pmAlpha = mSelectedAlpha; } if (mAmDisabled) { amColor = mUnselectedColor; amTextColor = mAmPmDisabledTextColor; } if (mPmDisabled) { pmColor = mUnselectedColor; pmTextColor = mAmPmDisabledTextColor; } // Draw the two circles. mPaint.setColor(amColor); mPaint.setAlpha(amAlpha); canvas.drawCircle(mAmXCenter, mAmPmYCenter, mAmPmCircleRadius, mPaint); mPaint.setColor(pmColor); mPaint.setAlpha(pmAlpha); canvas.drawCircle(mPmXCenter, mAmPmYCenter, mAmPmCircleRadius, mPaint); // Draw the AM/PM texts on top. mPaint.setColor(amTextColor); int textYCenter = mAmPmYCenter - (int) (mPaint.descent() + mPaint.ascent()) / 2; canvas.drawText(mAmText, mAmXCenter, textYCenter, mPaint); mPaint.setColor(pmTextColor); canvas.drawText(mPmText, mPmXCenter, textYCenter, mPaint); }
From source file:net.hadifar.dope.ui.widget.pagerIndicator.NumericPageIndicator.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mViewPager == null) { return;/*from w w w.j a v a2 s . c om*/ } final int count = mViewPager.getAdapter().getCount(); if (count == 0) { return; } // mCurrentPage is -1 on first start and after orientation changed. If // so, retrieve the correct index from viewpager. if (mCurrentPage == -1 && mViewPager != null) { mCurrentPage = mViewPager.getCurrentItem(); } // Handle the first time we draw the view, when onMeasure has not been // called yet if (mTextFirstPart == null) { updateText(); } // Draw the main text (e.g. "Page 1 of 20"). The hardest part is drawing // the page // number itself, because of the animated effect in which the current // page fades // out and the next one fades in. In order to implement this effect we // are forced to // draw the text in four "chunks": the first part ("Page "), the current // page // number ("1"), the next page number ("2"), and the last part // (" of 20"). // To implement the fade in and fade out animations we simply change the // alpha // of the page number text, relative to the view pager scroll final float currentPageWeight = 1 - mPageOffset; final float nextPageWeight = mPageOffset; final float firstPartWidth = mPaintText.measureText(mTextFirstPart); final String currentPageNumber = Integer.toString(mCurrentPage + 1); final String nextPageNumber = Integer.toString(mCurrentPage + 2); final float currentPageNumberWidth = mPaintText.measureText(currentPageNumber); final float nextPageNumberWidth = mPaintText.measureText(nextPageNumber); final float pageNumberWidth = currentPageWeight * currentPageNumberWidth + nextPageWeight * nextPageNumberWidth; final float lastPartWidth = mPaintText.measureText(mTextLastPart); final float totalWidth = firstPartWidth + pageNumberWidth + lastPartWidth; float currentX = (getWidth() - totalWidth) / 2; canvas.drawText(mTextFirstPart, currentX, mTextBottom, mPaintText); currentX += firstPartWidth; final float pageNumberCenterX = currentX + pageNumberWidth / 2; final int startAlpha = Color.alpha(mColorPageNumberText); final int endAlpha = 0; final float currentPageNumberAlpha = currentPageWeight * startAlpha + nextPageWeight * endAlpha; mPaintPageNumberText.setAlpha((int) currentPageNumberAlpha); canvas.drawText(currentPageNumber, pageNumberCenterX - currentPageNumberWidth / 2, mTextBottom, mPaintPageNumberText); final float nextPageNumberAlpha = nextPageWeight * startAlpha + currentPageWeight * endAlpha; mPaintPageNumberText.setAlpha((int) nextPageNumberAlpha); canvas.drawText(nextPageNumber, pageNumberCenterX - nextPageNumberWidth / 2, mTextBottom, mPaintPageNumberText); currentX += pageNumberWidth; canvas.drawText(mTextLastPart, currentX, mTextBottom, mPaintText); // Draw the "start" and "end" buttons if (mShowStartEndButtons) { final int textStartAlpha = Color.alpha(mColorText); final int textEndAlpha = 0; if (mCurrentPage != 0 && mStartDown) { canvas.drawRect(mRectStart, mPaintButtonBackground); } if (mCurrentPage != count - 1 && mEndDown) { canvas.drawRect(mRectEnd, mPaintButtonBackground); } if (mCurrentPage == 0) { mPaintText.setAlpha((int) (nextPageWeight * textStartAlpha + currentPageWeight * textEndAlpha)); } // canvas.drawText(mTextStartButton, mRectStart.centerX() - mWidthStartText / 2, mRectStartText.bottom, mPaintText); mPaintText.setAlpha(Color.alpha(mColorText)); if (mCurrentPage < count - 1) { if (mCurrentPage == count - 2) { mPaintText.setAlpha((int) (currentPageWeight * textStartAlpha + nextPageWeight * textEndAlpha)); } // canvas.drawText(mTextEndButton, mRectEnd.centerX() - mWidthEndText / 2, mRectEndText.bottom, mPaintText); mPaintText.setAlpha(Color.alpha(mColorText)); } } // Draw the "next" and "previous" buttons if (mShowChangePageButtons) { final int textStartAlpha = Color.alpha(mColorText); final int textEndAlpha = 0; if (mCurrentPage != 0 && mPreviousDown) { canvas.drawRect(mRectPrevious, mPaintButtonBackground); } if (mCurrentPage != count - 1 && mNextDown) { canvas.drawRect(mRectNext, mPaintButtonBackground); } if (mCurrentPage == 0) { mPaintText.setAlpha((int) (nextPageWeight * textStartAlpha + currentPageWeight * textEndAlpha)); } canvas.drawText(mTextPreviousButton, mRectPrevious.centerX() - mWidthPreviousText / 2, mRectPreviousText.bottom, mPaintText); mPaintText.setAlpha(Color.alpha(mColorText)); if (mCurrentPage < count - 1) { if (mCurrentPage == count - 2) { mPaintText.setAlpha((int) (currentPageWeight * textStartAlpha + nextPageWeight * textEndAlpha)); } canvas.drawText(mTextNextButton, mRectNext.centerX() - mWidthNextText / 2, mRectNextText.bottom, mPaintText); mPaintText.setAlpha(Color.alpha(mColorText)); } } }
From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
private void drawAmPm(Canvas canvas, Paint p) { p.setColor(mCalendarAmPmLabel);// w w w . j a v a 2s . c o m p.setTextSize(AMPM_TEXT_SIZE); p.setTypeface(mBold); p.setAntiAlias(true); p.setTextAlign(Align.RIGHT); String text = mAmString; if (mFirstHour >= 12) { text = mPmString; } int y = mFirstCell + mFirstHourOffset + 2 * mHoursTextHeight + HOUR_GAP; canvas.drawText(text, HOURS_LEFT_MARGIN, y, p); if (mFirstHour < 12 && mFirstHour + mNumHours > 12) { // Also draw the "PM" text = mPmString; y = mFirstCell + mFirstHourOffset + (12 - mFirstHour) * (mCellHeight + HOUR_GAP) + 2 * mHoursTextHeight + HOUR_GAP; canvas.drawText(text, HOURS_LEFT_MARGIN, y, p); } }