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.almalence.opencam.SavingService.java
protected void drawTextWithBackground(Canvas canvas, Paint paint, String text, int foreground, int background, int imageWidth, int imageHeight) { Rect text_bounds = new Rect(); paint.setColor(foreground);//from w ww . j av a2 s.c o m String[] resText = text.split("\n"); String maxLengthText = ""; if (resText.length > 1) { maxLengthText = resText[0].length() > resText[1].length() ? resText[0] : resText[1]; } else if (resText.length > 0) { maxLengthText = resText[0]; } final float scale = getResources().getDisplayMetrics().density; paint.setStyle(Paint.Style.FILL); paint.setColor(background); paint.setAlpha(64); paint.getTextBounds(text, 0, maxLengthText.length(), text_bounds); final int padding = (int) (2 * scale + 0.5f); // convert dps to pixels int textWidth = 0; int textHeight = text_bounds.bottom - text_bounds.top; if (paint.getTextAlign() == Paint.Align.RIGHT || paint.getTextAlign() == Paint.Align.CENTER) { float width = paint.measureText(maxLengthText); // n.b., need to use // measureText // rather than // getTextBounds // here textWidth = (int) width; } text_bounds.left = imageWidth - textWidth - resText.length * padding; text_bounds.right = imageWidth - padding; if (resText.length > 1) { text_bounds.top = imageHeight - resText.length * padding - resText.length * textHeight - textHeight; } else { text_bounds.top = imageHeight - 2 * padding - textHeight; textHeight /= 3; } text_bounds.bottom = imageHeight - padding; paint.setColor(foreground); if (resText.length > 0) { canvas.drawText(resText[0], imageWidth - 5 * padding, imageHeight - resText.length * textHeight - textHeight / 2, paint); } if (resText.length > 1) { canvas.drawText(resText[1], imageWidth - 5 * padding, imageHeight - (resText.length - 1) * textHeight, paint); } if (resText.length > 2) { canvas.drawText(resText[2], imageWidth - 5 * padding, imageHeight - (resText.length - 2) * textHeight + textHeight / 2, paint); } if (resText.length > 3) { canvas.drawText(resText[3], imageWidth - 5 * padding, imageHeight - textHeight / 4, paint); } }
From source file:de.hs_bremen.aurora_hunter.ui.views.PredictionGraphView.java
public void onDraw(Canvas canvas) { if (mPoints.size() == 0) { return;// ww w . java2 s . c o m } int yOffsetForLabels = 0; if (mMax * mScaleFactor > 0.8) { yOffsetForLabels = 15; } Path path = new Path(); int height = canvas.getHeight(); int width = canvas.getWidth(); for (Point point : mPoints) { point.x = point.percentageX * width + 60; // Log.i("mScaleFactor", " : " + mScaleFactor); //Log.i("percent", " : " + percent); point.y = (float) ((1 - point.percentageY * mScaleFactor) * height) + yOffsetForLabels * 3; } if (mPoints.size() > 1) { //calcuate x/y based on size of canvas for (int i = 0; i < mPoints.size(); i++) { if (i >= 0) { Point point = mPoints.get(i); // Log.i("dx",point.x + " - " + point.y ); if (i == 0) { Point next = mPoints.get(i + 1); point.dx = ((next.x - point.x) / 5); point.dy = ((next.y - point.y) / 5); } else if (i == mPoints.size() - 1) { Point prev = mPoints.get(i - 1); point.dx = ((point.x - prev.x) / 5); point.dy = ((point.y - prev.y) / 5); } else { Point next = mPoints.get(i + 1); Point prev = mPoints.get(i - 1); point.dx = ((next.x - prev.x) / 5); point.dy = ((next.y - prev.y) / 5); } } } } if (mPoints.size() > 0) { path.moveTo(0, (float) (canvas.getHeight() * 0.8)); path.lineTo(0, mPoints.get(0).y); } boolean first = true; for (int i = 0; i < mPoints.size(); i++) { Point point = mPoints.get(i); if (first) { first = false; path.cubicTo(point.x - point.x * 2, point.y - point.y / 5, point.x - point.dx, point.y - point.dy, point.x, point.y); } else { Point prev = mPoints.get(i - 1); // Log.i("Draw", point.dx + " " + point.dy); path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y); } } if (mPoints.size() > 0) { path.lineTo(width, mPoints.get(mPoints.size() - 1).y); } path.lineTo(width, height); path.lineTo(0, height); canvas.drawPath(path, mGraphPaint); int detlaY = 30; for (Point p : mPoints) { int val = (int) Math.round(p.percentageY * 100); //if last element if (mPoints.indexOf(p) == mPoints.size() - 1) { //Log.i("last", p.toString()); if (val == 0 || p.y > getHeight()) { canvas.drawText(val + "%", p.x - 150, getHeight() - detlaY, mTextPaint); } else { canvas.drawText(val + "%", p.x - 150, p.y - detlaY + yOffsetForLabels, mTextPaint); } } else { if (val == 0 || p.y > getHeight()) { canvas.drawText(val + "%", p.x - 20, getHeight() - detlaY, mTextPaint); } else { canvas.drawText(val + "%", p.x - 20, p.y - detlaY + yOffsetForLabels, mTextPaint); } } //Log.i("point", p.toString()); } // Log.i("Lenght", mPoints.size() + " "); float levelStart = (float) (canvas.getHeight() - (mNotificationLevel * canvas.getHeight())); float lineHeight = DpToPixelUtil.convertDpToPixel(2, getContext()); if (mViewMode == PredictionFragment.NOTIFICATION_MODE.SET_NOTIFICATION_LEVEL) { mNotificationLevelPaint.setAlpha(150); float leftOffset = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { float arcHeight = DpToPixelUtil.convertDpToPixel(30, getContext()); leftOffset = arcHeight; RectF rectF = new RectF(getWidth() - arcHeight, levelStart - arcHeight / 2, getWidth(), levelStart + arcHeight / 2); canvas.drawArc(rectF, 0, 360, true, mNotificationLevelPaint); } canvas.drawRect(0, levelStart, canvas.getWidth() - leftOffset, levelStart + lineHeight, mNotificationLevelPaint); mNotificationLevelPaint.setAlpha(20); canvas.drawRect(0, levelStart + lineHeight, canvas.getWidth(), canvas.getHeight(), mNotificationLevelPaint); String text = Math.round(mNotificationLevel * 100) + "%"; canvas.drawText(text, DpToPixelUtil.convertDpToPixel(10, getContext()), levelStart + DpToPixelUtil.convertDpToPixel(25, getContext()), mNotificationLevelTextPaint); } else { mNotificationLevelPaint.setAlpha(30); canvas.drawRect(0, levelStart, canvas.getWidth(), levelStart + lineHeight, mNotificationLevelPaint); } }
From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java
public void drawCompareImage(Canvas canvas, Bitmap image) { MasterImage master = MasterImage.getImage(); boolean showsOriginal = master.showsOriginal(); if (!showsOriginal && !mTouchShowOriginal) return;/*from ww w. jav a2s . c o m*/ canvas.save(); if (image != null) { if (mShowOriginalDirection == 0) { if (Math.abs(mTouch.y - mTouchDown.y) > Math.abs(mTouch.x - mTouchDown.x)) { mShowOriginalDirection = UNVEIL_VERTICAL; } else { mShowOriginalDirection = UNVEIL_HORIZONTAL; } } int px = 0; int py = 0; if (mShowOriginalDirection == UNVEIL_VERTICAL) { px = mImageBounds.width(); py = mTouch.y - mImageBounds.top; } else { px = mTouch.x - mImageBounds.left; py = mImageBounds.height(); if (showsOriginal) { px = mImageBounds.width(); } } Rect d = new Rect(mImageBounds.left, mImageBounds.top, mImageBounds.left + px, mImageBounds.top + py); if (mShowOriginalDirection == UNVEIL_HORIZONTAL) { if (mTouchDown.x - mTouch.x > 0) { d.set(mImageBounds.left + px, mImageBounds.top, mImageBounds.right, mImageBounds.top + py); } } else { if (mTouchDown.y - mTouch.y > 0) { d.set(mImageBounds.left, mImageBounds.top + py, mImageBounds.left + px, mImageBounds.bottom); } } canvas.clipRect(d); Matrix m = master.computeImageToScreen(image, 0, false); canvas.drawBitmap(image, m, mPaint); Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setStrokeWidth(3); if (mShowOriginalDirection == UNVEIL_VERTICAL) { canvas.drawLine(mImageBounds.left, mTouch.y, mImageBounds.right, mTouch.y, paint); } else { canvas.drawLine(mTouch.x, mImageBounds.top, mTouch.x, mImageBounds.bottom, paint); } Rect bounds = new Rect(); paint.setAntiAlias(true); paint.setTextSize(mOriginalTextSize); paint.getTextBounds(mOriginalText, 0, mOriginalText.length(), bounds); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3); canvas.drawText(mOriginalText, mImageBounds.left + mOriginalTextMargin, mImageBounds.top + bounds.height() + mOriginalTextMargin, paint); paint.setStyle(Paint.Style.FILL); paint.setStrokeWidth(1); paint.setColor(Color.WHITE); canvas.drawText(mOriginalText, mImageBounds.left + mOriginalTextMargin, mImageBounds.top + bounds.height() + mOriginalTextMargin, paint); } canvas.restore(); }
From source file:com.example.SmartBoard.DrawingView.java
public void onDrawLine(Canvas canvas) { Paint paint = drawPaint;/*from w ww. ja va 2s . co m*/ if (points[1] == null) //point4 null when user did not touch and move on screen. return; //draw stroke paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); if (finished) { JSONObject objectProperties = new JSONObject(); String key = UUID.randomUUID().toString(); try { objectProperties.put("id", key); objectProperties.put("clientId", client.getClientId()); objectProperties.put("type", "Line"); objectProperties.put("color", drawPaint.getColor()); objectProperties.put("size", 5); objectProperties.put("startx", linePath.startx()); objectProperties.put("starty", linePath.starty()); objectProperties.put("stopx", linePath.stopx()); objectProperties.put("stopy", linePath.stopy()); objectProperties.put("gradient", linePath.getGradient()); objectProperties.put("length", linePath.length()); } catch (JSONException e) { e.printStackTrace(); } objectDrawables.put(key, objectProperties); mqtt.publishLine(objectProperties); //reset to start drawing again points = new Point[4]; colorballs.clear(); return; } canvas.drawLine(linePath.startx(), linePath.starty(), linePath.stopx(), linePath.stopy(), paint); // draw the balls on the canvas paint.setTextSize(18); paint.setStrokeWidth(0); for (int i = 0; i < colorballs.size(); i++) { ColorBall ball = colorballs.get(i); canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), paint); canvas.drawText("" + (i + 1), ball.getX(), ball.getY(), paint); } }
From source file:com.jjoe64.graphview.GridLabelRenderer.java
/** * draws the vertical steps/*from w w w .j av a2 s. c o m*/ * horizontal lines and vertical labels * * @param canvas canvas */ protected void drawVerticalSteps(Canvas canvas) { // draw vertical steps (horizontal lines and vertical labels) float startLeft = mGraphView.getGraphContentLeft(); mPaintLabel.setColor(getVerticalLabelsColor()); mPaintLabel.setTextAlign(getVerticalLabelsAlign()); for (Map.Entry<Integer, Double> e : mStepsVertical.entrySet()) { // draw line if (mStyles.highlightZeroLines) { if (e.getValue() == 0d) { mPaintLine.setStrokeWidth(5); } else { mPaintLine.setStrokeWidth(0); } } if (mStyles.gridStyle.drawHorizontal()) { canvas.drawLine(startLeft, e.getKey(), startLeft + mGraphView.getGraphContentWidth(), e.getKey(), mPaintLine); } // draw label if (isVerticalLabelsVisible()) { int labelsWidth = mLabelVerticalWidth; int labelsOffset = 0; if (getVerticalLabelsAlign() == Paint.Align.RIGHT) { labelsOffset = labelsWidth; labelsOffset -= mStyles.labelsSpace; } else if (getVerticalLabelsAlign() == Paint.Align.CENTER) { labelsOffset = labelsWidth / 2; } labelsOffset += mStyles.padding + getVerticalAxisTitleWidth(); float y = e.getKey(); String label = mLabelFormatter.formatLabel(e.getValue(), false); if (label == null) { label = ""; } String[] lines = label.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:com.lyue.aw_jitpack_aar.app.zxing.zxing.view.ViewfinderView.java
@Override public void onDraw(Canvas canvas) { //CameraManager Rect frame = CameraManager.get().getFramingRect(); if (frame == null) { return;/*from w w w . j a va2 s . c om*/ } // if (!isFirst) { isFirst = true; slideTop = frame.top; slideBottom = frame.bottom; } // int width = canvas.getWidth(); int height = canvas.getHeight(); paint.setColor(resultBitmap != null ? resultColor : maskColor); // // canvas.drawRect(0, 0, width, frame.top, paint); canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); canvas.drawRect(0, frame.bottom + 1, width, height, paint); if (resultBitmap != null) { // Draw the opaque result bitmap over the scanning rectangle paint.setAlpha(OPAQUE); canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint); } else { //8 paint.setColor(Color.GREEN); canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate, frame.top + CORNER_WIDTH, paint); canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top + ScreenRate, paint); canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right, frame.top + CORNER_WIDTH, paint); canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top + ScreenRate, paint); canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left + ScreenRate, frame.bottom, paint); canvas.drawRect(frame.left, frame.bottom - ScreenRate, frame.left + CORNER_WIDTH, frame.bottom, paint); canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH, frame.right, frame.bottom, paint); canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate, frame.right, frame.bottom, paint); //,SPEEN_DISTANCE slideTop += SPEEN_DISTANCE; if (slideTop >= frame.bottom) { slideTop = frame.top; } Rect lineRect = new Rect(); lineRect.left = frame.left; lineRect.right = frame.right; lineRect.top = slideTop; lineRect.bottom = slideTop + 18; canvas.drawBitmap( ((BitmapDrawable) (getResources().getDrawable(R.drawable.aar_qrcode_scan_line))).getBitmap(), null, lineRect, paint); // paint.setColor(Color.WHITE); paint.setTextSize(TEXT_SIZE * density); paint.setAlpha(0x40); paint.setTypeface(Typeface.create("System", Typeface.BOLD)); String text = getResources().getString(R.string.scan_text); float textWidth = paint.measureText(text); canvas.drawText(text, (width - textWidth) / 2, (float) (frame.bottom + (float) TEXT_PADDING_TOP * density), paint); Collection<ResultPoint> currentPossible = possibleResultPoints; Collection<ResultPoint> currentLast = lastPossibleResultPoints; if (currentPossible.isEmpty()) { lastPossibleResultPoints = null; } else { possibleResultPoints = new HashSet<ResultPoint>(5); lastPossibleResultPoints = currentPossible; paint.setAlpha(OPAQUE); paint.setColor(resultPointColor); for (ResultPoint point : currentPossible) { canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 6.0f, paint); } } if (currentLast != null) { paint.setAlpha(OPAQUE / 2); paint.setColor(resultPointColor); for (ResultPoint point : currentLast) { canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 3.0f, paint); } } // postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom); } }
From source file:io.doist.datetimepicker.time.RadialTimePickerView.java
private void drawDebug(Canvas canvas) { // Draw outer numbers circle final float outerRadius = mCircleRadius[HOURS] * mNumbersRadiusMultiplier[HOURS]; canvas.drawCircle(mXCenter, mYCenter, outerRadius, mPaintDebug); // Draw inner numbers circle final float innerRadius = mCircleRadius[HOURS] * mNumbersRadiusMultiplier[HOURS_INNER]; canvas.drawCircle(mXCenter, mYCenter, innerRadius, mPaintDebug); // Draw outer background circle canvas.drawCircle(mXCenter, mYCenter, mCircleRadius[HOURS], mPaintDebug); // Draw outer rectangle for circles float left = mXCenter - outerRadius; float top = mYCenter - outerRadius; float right = mXCenter + outerRadius; float bottom = mYCenter + outerRadius; canvas.drawRect(left, top, right, bottom, mPaintDebug); // Draw outer rectangle for background left = mXCenter - mCircleRadius[HOURS]; top = mYCenter - mCircleRadius[HOURS]; right = mXCenter + mCircleRadius[HOURS]; bottom = mYCenter + mCircleRadius[HOURS]; canvas.drawRect(left, top, right, bottom, mPaintDebug); // Draw outer view rectangle canvas.drawRect(0, 0, getWidth(), getHeight(), mPaintDebug); // Draw selected time final String selected = String.format("%02d:%02d", getCurrentHour(), getCurrentMinute()); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); TextView tv = new TextView(getContext()); tv.setLayoutParams(lp);/*from w w w. j a va 2 s. c o m*/ tv.setText(selected); tv.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); Paint paint = tv.getPaint(); paint.setColor(DEBUG_TEXT_COLOR); final int width = tv.getMeasuredWidth(); float height = paint.descent() - paint.ascent(); float x = mXCenter - width / 2; float y = mYCenter + 1.5f * height; canvas.drawText(selected, x, y, paint); }
From source file:com.chrynan.guitarchords.view.GuitarChordView.java
@Override protected void onDraw(Canvas canvas) { //This method handles a lot of work, possibly too much for an onDraw method, but since I'm not animating //or updating the view often, it should be capable of performing the tasks without too much effort. //I can optimize this later. //First draw the strings and fret markers //Fret markers; not worrying about whether or not to show the bridge nut now (since that wasn't calculated //in to the drawing size) //TODO need to take into account whether or not to show bridgeNut int fretCount = chord.getFretCount(); fretCount = (fretCount < 4) ? 4 : fretCount; for (int i = 0; i < fretCount + 1; i++) { canvas.drawLine(drawingBounds.left + fretNumberBounds.width(), (drawingBounds.top + stringMarkerBounds.height()) + (i * fretSize) + (i * fretMarkerSize), drawingBounds.right - stringSize, (drawingBounds.top + stringMarkerBounds.height()) + (i * fretSize) + (i * fretMarkerSize), fretMarkerPaint);//from w ww . j a va 2 s . co m } //Strings for (int i = 0; i < stringCount; i++) { canvas.drawLine((stringMarkerBounds.left) + (i * stringDistance) + (i * stringSize), drawingBounds.top + stringMarkerBounds.height(), (stringMarkerBounds.left) + (i * stringDistance) + (i * stringSize), drawingBounds.bottom - fretMarkerSize, stringPaint); } //Next draw the fret numbers and string markers //Fret numbers; check if we are showing them or not if (showFretNumbers) { for (int i = 0; i < fretCount; i++) { canvas.drawText(String.valueOf(i + 1), drawingBounds.left + fretNumberBounds.width() / 2, getVerticalCenterTextPosition( stringMarkerBounds.bottom + (i * fretMarkerSize) + (i * fretSize) + (fretSize / 2), String.valueOf(i + 1), fretNumberPaint), fretNumberPaint); } } //String markers for (int i : chord.getMutedStrings()) { canvas.drawText(mutedText, (drawingBounds.left + fretNumberBounds.width()) + ((stringCount - i) * stringDistance) + ((stringCount - i) * stringSize), getVerticalCenterTextPosition(drawingBounds.top + (stringMarkerBounds.height() / 2), mutedText, stringMarkerPaint), stringMarkerPaint); } for (int i : chord.getOpenStrings()) { canvas.drawText(openStringText, (drawingBounds.left + fretNumberBounds.width()) + ((stringCount - i) * stringDistance) + ((stringCount - i) * stringSize), getVerticalCenterTextPosition(drawingBounds.top + (stringMarkerBounds.height() / 2), openStringText, stringMarkerPaint), stringMarkerPaint); } //Finally, draw all the notes and the note text //Bars float startCenterX; float startCenterY; for (ChordMarker cm : chord.getBars()) { startCenterX = (drawingBounds.left + fretNumberBounds.width()) + ((stringCount - cm.getStartString()) * stringDistance) + ((stringCount - cm.getStartString()) * stringSize); startCenterY = stringMarkerBounds.bottom + (((cm.getFret() * fretSize) + (cm.getFret() * fretMarkerSize)) - (fretSize / 2)); float endCenterX = (drawingBounds.left + fretNumberBounds.width()) + ((stringCount - cm.getEndString()) * stringDistance) + ((stringCount - cm.getEndString()) * stringSize); float endCenterY = startCenterY; canvas.drawCircle(startCenterX, startCenterY, noteSize / 2, notePaint); canvas.drawCircle(endCenterX, endCenterY, noteSize / 2, notePaint); if (showFingerNumbers) { canvas.drawText(String.valueOf(cm.getFinger()), startCenterX, getVerticalCenterTextPosition( startCenterY, String.valueOf(cm.getFinger()), noteNumberPaint), noteNumberPaint); canvas.drawText(String.valueOf(cm.getFinger()), endCenterX, getVerticalCenterTextPosition(endCenterY, String.valueOf(cm.getFinger()), noteNumberPaint), noteNumberPaint); } barLinePath = new Path(); barLinePath.moveTo(startCenterX, startCenterY - (noteSize / 2)); barLinePath.quadTo((startCenterX + endCenterX) / 2, (startCenterY + endCenterY - (noteSize / 2)) / 4, endCenterX, endCenterY - (noteSize / 2)); canvas.drawPath(barLinePath, barLinePaint); } //Individual notes for (ChordMarker cm : chord.getIndividualNotes()) { startCenterX = (drawingBounds.left + fretNumberBounds.width()) + ((stringCount - cm.getStartString()) * stringDistance) + ((stringCount - cm.getStartString()) * stringSize); startCenterY = stringMarkerBounds.bottom + (((cm.getFret() * fretSize) + (cm.getFret() * fretMarkerSize)) - (fretSize / 2)); canvas.drawCircle(startCenterX, startCenterY, noteSize / 2, notePaint); if (showFingerNumbers) { canvas.drawText(String.valueOf(cm.getFinger()), startCenterX, getVerticalCenterTextPosition( startCenterY, String.valueOf(cm.getFinger()), noteNumberPaint), noteNumberPaint); } } }
From source file:am.widget.indicatortabstrip.IndicatorTabStrip.java
/** * Tag// ww w.ja v a 2 s . c o m * * @param canvas * @param position ??? * @param itemWidth ? * @param itemHeight ? */ @SuppressWarnings("unused") protected void drawTag(Canvas canvas, int position, int itemWidth, int itemHeight) { if (mAdapter == null || !mAdapter.isTagEnable(position)) return; String text = mAdapter.getTag(position) == null ? "" : mAdapter.getTag(position); mTextPaint.setTextSize(mTagTextSize); mTextPaint.setColor(mTagTextColor); mTextPaint.getTextBounds(text, 0, text.length(), mTextMeasureBounds); final int textWidth = mTextMeasureBounds.width(); final int textHeight = mTextMeasureBounds.height(); final int tagBackgroundWidth = mTagBackground == null ? 0 : mTagBackground.getIntrinsicWidth(); final int tagBackgroundHeight = mTagBackground == null ? 0 : mTagBackground.getIntrinsicHeight(); int tagWidth; int tagHeight; switch (mTagMinSizeMode) { default: case TAG_MIN_SIZE_MODE_HAS_TEXT: if ("".equals(text)) { tagWidth = Math.min(mTagMinWidth, tagBackgroundWidth); tagHeight = Math.min(mTagMinHeight, tagBackgroundHeight); break; } case TAG_MIN_SIZE_MODE_ALWAYS: tagWidth = Math.max(textWidth + mTagLocation.getPaddingLeft() + mTagLocation.getPaddingRight(), Math.max(mTagMinWidth, tagBackgroundWidth)); tagHeight = Math.max(textHeight + mTagLocation.getPaddingTop() + mTagLocation.getPaddingBottom(), Math.max(mTagMinHeight, tagBackgroundHeight)); break; } final int rightTabX = position == getItemCount() - 1 ? getWidth() - ViewCompat.getPaddingEnd(this) : ViewCompat.getPaddingStart(this) + (itemWidth + getIntervalWidth()) * position + itemWidth; final int rightTagX = rightTabX - mTagLocation.getMarginRight(); final int tagY = getPaddingTop() + mTagLocation.getMarginTop(); final int leftTagX = rightTagX - tagWidth; final float tagCenterX = leftTagX + tagWidth * 0.5f; final float tagCenterY = tagY + tagWidth * 0.5f; canvas.save(); if (mTagBackground != null) { mTagBackground.setBounds(0, 0, tagWidth, tagHeight); canvas.translate(leftTagX, tagY); mTagBackground.draw(canvas); if (!"".equals(text)) { canvas.translate(tagWidth * 0.5f, tagHeight * 0.5f + mTagTextDesc); canvas.drawText(text, 0, 0, mTextPaint); } } else { canvas.translate(tagCenterX, tagCenterY + mTagTextDesc); canvas.drawText(text, 0, 0, mTextPaint); } canvas.restore(); }
From source file:com.android.launcher3.ViewPager.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); width = canvas.getWidth() / 2;/*from w w w . j av a2 s . co m*/ float currentPageSize = width / 10; float size = width / 10; float margin = width / 10; currentPage.setTextSize(currentPageSize); currentPage.setColor(Color.GREEN); p.setTextSize(size); p.setColor(Color.BLUE); width = width + (2 * width - 180) * off + 2 * (mPosition - initialization) * width; if (hideTitle) { canvas.drawText("", width - currentPageSize, 70, currentPage); return; } // Draw the margin drawable between pages if needed. //Log.i("ViewPager", off + "-" + mPosition + "-" + initialization + "- " + width); try { switch (mPosition) { case 0: currentPageSize += currentPageSize; canvas.drawText(name.get(0), width - currentPageSize, 70, currentPage); canvas.drawText(name.get(1), width + currentPageSize + margin, 70, p); canvas.drawText(name.get(2), width + currentPageSize + margin + size * 4 + margin, 70, p); canvas.drawText(name.get(3), width + currentPageSize + margin + size * 8 + margin * 2, 70, p); canvas.drawText(name.get(4), width + currentPageSize + margin + size * 12 + margin * 3, 70, p); canvas.drawText(name.get(5), width + currentPageSize + margin + size * 16 + margin * 4, 70, p); canvas.drawText(name.get(6), width + currentPageSize + margin + size * 20 + margin * 5, 70, p); break; case 1: currentPageSize += currentPageSize; canvas.drawText(name.get(0), width - currentPageSize - margin - size * 4, 70, p); canvas.drawText(name.get(1), width - currentPageSize, 70, currentPage); canvas.drawText(name.get(2), width + currentPageSize + margin, 70, p); canvas.drawText(name.get(3), width + currentPageSize + margin + size * 4 + margin, 70, p); canvas.drawText(name.get(4), width + currentPageSize + margin + size * 8 + margin * 2, 70, p); canvas.drawText(name.get(5), width + currentPageSize + margin + size * 12 + margin * 3, 70, p); canvas.drawText(name.get(6), width + currentPageSize + margin + size * 16 + margin * 4, 70, p); break; case 2: currentPageSize += currentPageSize; canvas.drawText(name.get(0), width - currentPageSize - 2 * (margin + size * 4), 70, p); canvas.drawText(name.get(1), width - currentPageSize - margin - size * 4, 70, p); canvas.drawText(name.get(2), width - currentPageSize, 70, currentPage); canvas.drawText(name.get(3), width + currentPageSize + margin, 70, p); canvas.drawText(name.get(4), width + currentPageSize + margin + size * 4 + margin, 70, p); canvas.drawText(name.get(5), width + currentPageSize + margin + size * 8 + margin * 2, 70, p); canvas.drawText(name.get(6), width + currentPageSize + margin + size * 12 + margin * 3, 70, p); break; case 3: currentPageSize += currentPageSize; canvas.drawText(name.get(0), width - currentPageSize - 3 * (margin + size * 4), 70, p); canvas.drawText(name.get(1), width - currentPageSize - 2 * (margin + size * 4), 70, p); canvas.drawText(name.get(2), width - currentPageSize - margin - size * 4, 70, p); canvas.drawText(name.get(3), width - currentPageSize, 70, currentPage); canvas.drawText(name.get(4), width + currentPageSize + margin, 70, p); canvas.drawText(name.get(5), width + currentPageSize + margin + size * 4 + margin, 70, p); canvas.drawText(name.get(6), width + currentPageSize + margin + size * 8 + margin * 2, 70, p); break; case 4: currentPageSize += currentPageSize; canvas.drawText(name.get(0), width - currentPageSize - 4 * (margin + size * 4), 70, p); canvas.drawText(name.get(1), width - currentPageSize - 3 * (margin + size * 4), 70, p); canvas.drawText(name.get(2), width - currentPageSize - 2 * (margin + size * 4), 70, p); canvas.drawText(name.get(3), width - currentPageSize - margin - size * 4, 70, p); canvas.drawText(name.get(4), width - currentPageSize, 70, currentPage); canvas.drawText(name.get(5), width + currentPageSize + margin, 70, p); canvas.drawText(name.get(6), width + currentPageSize + margin + size * 4 + margin, 70, p); break; case 5: currentPageSize += currentPageSize; canvas.drawText(name.get(0), width - currentPageSize - 5 * (margin + size * 4), 70, p); canvas.drawText(name.get(1), width - currentPageSize - 4 * (margin + size * 4), 70, p); canvas.drawText(name.get(2), width - currentPageSize - 3 * (margin + size * 4), 70, p); canvas.drawText(name.get(3), width - currentPageSize - 2 * (margin + size * 4), 70, p); canvas.drawText(name.get(4), width - currentPageSize - margin - size * 4, 70, p); canvas.drawText(name.get(5), width - currentPageSize, 70, currentPage); canvas.drawText(name.get(6), width + currentPageSize + margin, 70, p); break; case 6: currentPageSize += currentPageSize; canvas.drawText(name.get(0), width - currentPageSize - 6 * (margin + size * 4), 70, p); canvas.drawText(name.get(1), width - currentPageSize - 5 * (margin + size * 4), 70, p); canvas.drawText(name.get(2), width - currentPageSize - 4 * (margin + size * 4), 70, p); canvas.drawText(name.get(3), width - currentPageSize - 3 * (margin + size * 4), 70, p); canvas.drawText(name.get(4), width - currentPageSize - 2 * (margin + size * 4), 70, p); canvas.drawText(name.get(5), width - currentPageSize - margin - size * 4, 70, p); canvas.drawText(name.get(6), width - currentPageSize, 70, currentPage); break; default: break; } } catch (IndexOutOfBoundsException e) { } if (mPageMargin > 0 && mMarginDrawable != null && mItems.size() > 0 && mAdapter != null) { final int scrollX = getScrollX(); final int width = getWidth(); final float marginOffset = (float) mPageMargin / width; int itemIndex = 0; ItemInfo ii = mItems.get(0); float offset = ii.offset; final int itemCount = mItems.size(); final int firstPos = ii.position; final int lastPos = mItems.get(itemCount - 1).position; for (int pos = firstPos; pos < lastPos; pos++) { while (pos > ii.position && itemIndex < itemCount) { ii = mItems.get(++itemIndex); } float drawAt; if (pos == ii.position) { drawAt = (ii.offset + ii.widthFactor) * width; offset = ii.offset + ii.widthFactor + marginOffset; } else { float widthFactor = mAdapter.getPageWidth(pos); drawAt = (offset + widthFactor) * width; offset += widthFactor + marginOffset; } if (drawAt + mPageMargin > scrollX) { mMarginDrawable.setBounds((int) drawAt, mTopPageBounds, (int) (drawAt + mPageMargin + 0.5f), mBottomPageBounds); mMarginDrawable.draw(canvas); } if (drawAt > scrollX + width) { break; // No more visible, no sense in continuing } } } }