@Override public void Draw(Canvas canvas, Paint paint) { Typeface tf = paint.getTypeface(); paint.setUnderlineText(underline); paint.setTypeface(Typeface.create(tf, style)); paint.setTextSize(size); paint.setStyle(Paint.Style.FILL); if (v.currBrushColor != -1) { // draw background rect int clr = paint.getColor(); paint.setColor(v.currBrushColor + v.Opaque); canvas.drawRect( x - 1, y, x + paint.measureText(text) + 1, y - paint.ascent() + paint.descent() / 2, paint); paint.setColor(clr); } canvas.drawText(text, x, y - paint.ascent(), paint); paint.setStyle(Paint.Style.STROKE); paint.setUnderlineText(false); paint.setTypeface(tf); }
public void onRender(Canvas canvas) { float lineTop = -textPaint.ascent(); float lineHeight = (-textPaint.ascent() + textPaint.descent()) * 1.1f; tick++; canvas.drawColor(Color.BLACK); canvas.drawText("Tick: " + tick, 0, lineTop, textPaint); canvas.drawText( "Screen = (" + width + ", " + height + ")", 0, lineTop + lineHeight * 1, textPaint); canvas.drawText("Azimuth = " + azimuth, 0, lineTop + lineHeight * 2, textPaint); canvas.drawText("Pitch = " + pitch, 0, lineTop + lineHeight * 3, textPaint); canvas.drawText("Roll = " + roll, 0, lineTop + lineHeight * 4, textPaint); // canvas.drawLine(0, 0, 80, 80, linePaint); float centerX = width / 2; float centerY = height / 2; float length = 50; double rad = Math.toRadians(pitch); float offsetX = (float) (Math.cos(rad) * length); float offsetY = (float) (Math.sin(rad) * length); canvas.drawLine(centerX, centerY, centerX + offsetX, centerY - offsetY, linePaint); canvas.drawRect(80, 133, 120, 266.7f, rectPaint); }
static int CreateTxtTexture( String txt, Typeface typeface, float font_size, boolean align_center) { Paint textPaint = new Paint(); textPaint.setTypeface(typeface); textPaint.setTextSize(font_size); textPaint.setTextAlign(align_center ? Paint.Align.CENTER : Paint.Align.LEFT); textPaint.setAntiAlias(true); textPaint.setARGB(0xff, 0xFF, 0xFF, 0xFF); int width = 0; int height = 0; int line_height = (int) Math.ceil(-textPaint.ascent() + textPaint.descent()); String[] lines = txt.split("\n"); for (String line : lines) { int line_width = (int) Math.ceil(textPaint.measureText(line)); if (line_width > width) width = line_width; height += line_height; } int power2_width = Integer.highestOneBit(width); if (power2_width < width) power2_width = power2_width << 1; int power2_height = Integer.highestOneBit(height); if (power2_height < height) power2_height = power2_height << 1; // Log.i(TAG, "CreateTxtTexture calculate w " + width + " h " + height + // " power2 w " + power2_width + " h " + power2_height); if (0 == power2_width || 0 == power2_height) return 0; Bitmap bitmap = Bitmap.createBitmap(power2_width, power2_height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); bitmap.eraseColor(0); float origin_x = align_center ? (width / 2) : 0; float origin_y = -textPaint.ascent(); for (String line : lines) { canvas.drawText(line, origin_x, origin_y, textPaint); origin_y += line_height; } // canvas.setBitmap(null); // Log.i(TAG, "CreateTxtTexture " + txt + " use " + font_name + " " + font_size); // Use the Android GLUtils to specify a two-dimensional texture image // from our bitmap GLUtils.texImage2D(GL11.GL_TEXTURE_2D, 0, bitmap, 0); // bitmap.recycle(); return width + (height << 16); }
private void drawLeftAxisLabel(Canvas canvas, int position) { Paint axisLabelPaint = labelPaints[LEFT]; String text = getLeftLabel(position); if (position == 0) { canvas.drawText(text, 0, -axisLabelPaint.ascent(), axisLabelPaint); } else if (position == HORIZONTAL_LINE_NUMBER) { float y = chartView.getContentHeight() + (axisLabelPaint.ascent() + axisLabelPaint.descent()) / 2; canvas.drawText(text, 0, y, axisLabelPaint); } else { float y = position / (float) HORIZONTAL_LINE_NUMBER * chartView.getContentHeight(); y = y - (axisLabelPaint.ascent() + axisLabelPaint.descent()) / 2; canvas.drawText(text, 0, y, axisLabelPaint); } }
@Override protected synchronized void onDraw(Canvas canvas) { canvas.drawArc(mCircleBounds, 0, 360, false, mBackgroundColorPaint); int prog = getProgress(); float scale = getMax() > 0 ? (float) prog / getMax() * 360 : 0; if (mHasShadow) mProgressColorPaint.setShadowLayer(3, 0, 1, mShadowColor); canvas.drawArc(mCircleBounds, 270, scale, false, mProgressColorPaint); if (!TextUtils.isEmpty(mTitle)) { int xPos = (int) (getMeasuredWidth() / 2 - mTitlePaint.measureText(mTitle) / 2); int yPos = (int) (getMeasuredHeight() / 2); float titleHeight = Math.abs(mTitlePaint.descent() + mTitlePaint.ascent()); if (TextUtils.isEmpty(mSubTitle)) { yPos += titleHeight / 2; } canvas.drawText(mTitle, xPos, yPos, mTitlePaint); yPos += titleHeight; xPos = (int) (getMeasuredWidth() / 2 - mSubtitlePaint.measureText(mSubTitle) / 2); canvas.drawText(mSubTitle, xPos, yPos, mSubtitlePaint); } super.onDraw(canvas); }
public void chooseHeight( CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm, TextPaint paint) { int size = mSize; if (paint != null) { size *= paint.density; } if (fm.bottom - fm.top < size) { fm.top = fm.bottom - size; fm.ascent = fm.ascent - size; } else { if (sProportion == 0) { /* * Calculate what fraction of the nominal ascent the height * of a capital letter actually is, so that we won't reduce * the ascent to less than that unless we absolutely have * to. */ Paint p = new Paint(); p.setTextSize(100); Rect r = new Rect(); p.getTextBounds("ABCDEFG", 0, 7, r); sProportion = (r.top) / p.ascent(); } int need = (int) Math.ceil(-fm.top * sProportion); if (size - fm.descent >= need) { /* * It is safe to shrink the ascent this much. */ fm.top = fm.bottom - size; fm.ascent = fm.descent - size; } else if (size >= need) { /* * We can't show all the descent, but we can at least show * all the ascent. */ fm.top = fm.ascent = -need; fm.bottom = fm.descent = fm.top + size; } else { /* * Show as much of the ascent as we can, and no descent. */ fm.top = fm.ascent = -size; fm.bottom = fm.descent = 0; } } }
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) { 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(); } }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int height = 0; if (mBitmap != null) { height += mBitmap.getHeight(); bx = ((right - left) - mBitmap.getWidth()) / 2; } if (message != null) { Rect bounds = new Rect(); mPaint.getTextBounds(message, 0, message.length() - 1, bounds); int tw = bounds.right - bounds.left; int th = bounds.bottom - bounds.top; height += th; tx = ((right - left) - tw) / 2; } by = (bottom - top - height) / 2; if (mBitmap != null) { ty = by + mBitmap.getHeight(); } else { ty = by; } if (message != null) { ty += (-mPaint.ascent()); } }
public static int GetMinItemHeight(int iPaddingTop, int iPaddingBottom) { Paint pt = new Paint(); pt.setAntiAlias(true); pt.setTextSize(fTextSize); float iHeight = (int) (-pt.ascent() + pt.descent()); return iPaddingTop + iMargin + (int) iHeight + iMargin + iMargin + iPaddingBottom; }
/** * Sets the icon for the marker. Can be changed at any time. * * @param icon if null, the default osmdroid marker is used. */ public void setIcon(Drawable icon) { if (ENABLE_TEXT_LABELS_WHEN_NO_IMAGE && icon == null && this.mTitle != null && this.mTitle.length() > 0) { Paint background = new Paint(); background.setColor(mTextLabelBackgroundColor); Paint p = new Paint(); p.setTextSize(mTextLabelFontSize); p.setColor(mTextLabelForegroundColor); p.setAntiAlias(true); p.setTypeface(Typeface.DEFAULT_BOLD); p.setTextAlign(Paint.Align.LEFT); int width = (int) (p.measureText(getTitle()) + 0.5f); float baseline = (int) (-p.ascent() + 0.5f); int height = (int) (baseline + p.descent() + 0.5f); Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(image); c.drawPaint(background); c.drawText(getTitle(), 0, baseline, p); mIcon = new BitmapDrawable(resource, image); } if (!ENABLE_TEXT_LABELS_WHEN_NO_IMAGE && icon != null) this.mIcon = icon; // there's still an edge case here, title label no defined, icon is null and textlabel is // enabled if (this.mIcon == null) mIcon = mDefaultIcon; }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (firstTimeLaunched) { float scalewidth = (float) viewWidth / (float) imgBackgnd.getWidth(); float scaleheight = (float) viewHeight / (float) imgBackgnd.getHeight(); scaleFinal = scaleheight > scalewidth ? scalewidth : scaleheight; matrixBackgnd.setScale(scaleFinal, scaleFinal); firstTimeLaunched = false; } // (maxValue-value)*(maxPix-MinPix)/(maxValue-MinValue) + minPix float altitudePos = (4000 - value) * (470 - 70) / (4000) + 70; canvas.drawBitmap(imgBackgnd, matrixBackgnd, paint); canvas.concat(matrixBackgnd); float height = ((paint.descent() + paint.ascent()) / 2); int center = 70; canvas.drawLine(center, altitudePos, center + 15, altitudePos, paint); canvas.drawText(value + " m", center + 17, altitudePos - height, paint); canvas.drawRect(center, altitudePos, center + 10, 471, paint); }
private void calc() { textSize = mHTextView.getTextSize(); paint.setTextSize(textSize); for (int i = 0; i < mText.length(); i++) { gaps[i] = paint.measureText(mText.charAt(i) + ""); } oldPaint.setTextSize(textSize); for (int i = 0; i < mOldText.length(); i++) { oldGaps[i] = oldPaint.measureText(mOldText.charAt(i) + ""); } oldStartX = (mHTextView.getWidth() - oldPaint.measureText(mOldText.toString())) / 2f; startX = (mHTextView.getWidth() - paint.measureText(mText.toString())) / 2f; startY = (int) ((mHTextView.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)); differentList.clear(); differentList.addAll(CharacterUtils.diff(mOldText, mText)); Rect bounds = new Rect(); paint.getTextBounds(mText.toString(), 0, mText.length(), bounds); upDistance = bounds.height(); }
private LayoutInfo layout() { LayoutInfo info = new LayoutInfo(); info.labelHeight = labelPaint.descent() - labelPaint.ascent(); float maxMarkerSize = Float.MIN_VALUE; float maxLength = Float.MIN_VALUE; for (Entry entry : entryList) { float length = labelPaint.measureText(entry.label); if (maxLength < length) maxLength = length; float markerSize = entry.painter.getDrawConfig().getMarkerSize(); if (markerSize > maxMarkerSize) maxMarkerSize = markerSize; } info.markerWidth = maxMarkerSize; info.frame.left = 0; info.frame.top = 0; info.frame.right = 2 * info.margin + info.markerWidth + info.markerXSpacing + maxLength + 2 * info.markerLineSpace; info.frame.bottom = 2 * info.margin + entryList.size() * (info.labelHeight + info.labelSpacing); return info; }
/** * Calculate the bounds for a view's title * * @param index * @param paint * @return */ private RectF calcBounds(int index, Paint paint) { // Calculate the text bounds RectF bounds = new RectF(); bounds.right = paint.measureText(mTitleProvider.getTitle(index)); bounds.bottom = paint.descent() - paint.ascent(); return bounds; }
private void generateASCIIFont(AsyncTask task) { String ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; Paint p = new Paint(); p.setTextAlign(Align.CENTER); Typeface tf = Fonts.getTypeface(configuration.getModel()); p.setTypeface(tf); p.setTextScaleX(1.0f); p.setColor(configuration.getCharacterColorAsRGB()); p.setAntiAlias(true); setFontSize(p); int xPos = trsCharWidth / 2; int yPos = (int) ((trsCharHeight / 2) - ((p.descent() + p.ascent()) / 2)); for (int i = 0; i < ascii.length(); i++) { if (task.isCancelled()) { return; } Bitmap b = Bitmap.createBitmap(trsCharWidth, trsCharHeight, Bitmap.Config.RGB_565); Canvas c = new Canvas(b); c.drawColor(configuration.getScreenColorAsRGB()); c.drawText(ascii.substring(i, i + 1), xPos, yPos, p); font[i + 32] = b; } // Use space for all other characters for (int i = 0; i < font.length; i++) { if (font[i] == null) { font[i] = font[32]; } } }
/** * Using the trigonometric Unit Circle, calculate the positions that the text will need to be * drawn at based on the specified circle radius. Place the values in the textGridHeights and * textGridWidths parameters. */ private void calculateGridSizes( float numbersRadius, float xCenter, float yCenter, float textSize, float[] textGridHeights, float[] textGridWidths) { /* * The numbers need to be drawn in a 7x7 grid, representing the points on the Unit Circle. */ float offset1 = numbersRadius; // cos(30) = a / r => r * cos(30) = a => r * √3/2 = a float offset2 = numbersRadius * ((float) Math.sqrt(3)) / 2f; // sin(30) = o / r => r * sin(30) = o => r / 2 = a float offset3 = numbersRadius / 2f; mPaint.setTextSize(textSize); // We'll need yTextBase to be slightly lower to account for the text's baseline. yCenter -= (mPaint.descent() + mPaint.ascent()) / 2; textGridHeights[0] = yCenter - offset1; textGridWidths[0] = xCenter - offset1; textGridHeights[1] = yCenter - offset2; textGridWidths[1] = xCenter - offset2; textGridHeights[2] = yCenter - offset3; textGridWidths[2] = xCenter - offset3; textGridHeights[3] = yCenter; textGridWidths[3] = xCenter; textGridHeights[4] = yCenter + offset3; textGridWidths[4] = xCenter + offset3; textGridHeights[5] = yCenter + offset2; textGridWidths[5] = xCenter + offset2; textGridHeights[6] = yCenter + offset1; textGridWidths[6] = xCenter + offset1; }
/** * Calculate the bounds for a view's title * * @param index * @param paint * @return */ private Rect calcBounds(int index, Paint paint) { // Calculate the text bounds Rect bounds = new Rect(); CharSequence title = getTitle(index); bounds.right = (int) paint.measureText(title, 0, title.length()); bounds.bottom = (int) (paint.descent() - paint.ascent()); return bounds; }
/** * Calculate the bounds for a view's title * * @param index * @param paint * @return */ private Rect calcBounds(int index, Paint paint) { // Get the title String title = getTitle(index); // Calculate the text bounds Rect bounds = new Rect(); bounds.right = (int) paint.measureText(title); bounds.bottom = (int) (paint.descent() - paint.ascent()); return bounds; }
public Cell(int dayOfMon, Rect rect, float textSize, boolean bold) { mDayOfMonth = dayOfMon; mBound = rect; mPaint.setTextSize(textSize /*26f*/); mPaint.setColor(Color.BLACK); if (bold) mPaint.setFakeBoldText(true); dx = (int) mPaint.measureText(String.valueOf(mDayOfMonth)) / 2; dy = (int) (-mPaint.ascent() + mPaint.descent()) / 2; }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mInnerRectF.set(0, 0, canvas.getWidth(), canvas.getHeight()); final int halfBorder = (int) (mStrokePaint.getStrokeWidth() / 2f + 0.5f); mInnerRectF.inset(halfBorder, halfBorder); canvas.drawArc(mInnerRectF, 0, 360, true, mBackgroundPaint); switch (mProgressFillType) { case FILL_TYPE_RADIAL: float sweepAngle = 360 * mProgress / mMax; canvas.drawArc(mInnerRectF, mStartAngle, sweepAngle, true, mProgressPaint); break; case FILL_TYPE_CENTER: float centerX = canvas.getWidth() / 2; float centerY = canvas.getHeight() / 2; float radius = (canvas.getWidth() / 2) * ((float) mProgress / mMax); canvas.drawCircle( centerX, centerY, radius + 0.5f - mStrokePaint.getStrokeWidth(), mProgressPaint); break; default: throw new IllegalArgumentException("Invalid Progress Fill = " + mProgressFillType); } if (!TextUtils.isEmpty(mText) && mShowText) { if (!TextUtils.isEmpty(mTypeface)) { Typeface typeface = sTypefaceCache.get(mTypeface); if (null == typeface && null != getResources()) { AssetManager assets = getResources().getAssets(); if (null != assets) { typeface = Typeface.createFromAsset(assets, mTypeface); sTypefaceCache.put(mTypeface, typeface); } } mTextPaint.setTypeface(typeface); } int xPos = canvas.getWidth() / 2; int yPos = (int) ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)); canvas.drawText(mText, xPos, yPos, mTextPaint); } if (null != mImage && mShowImage) { int drawableSize = mImage.getIntrinsicWidth(); mImageRect.set(0, 0, drawableSize, drawableSize); mImageRect.offset((getWidth() - drawableSize) / 2, (getHeight() - drawableSize) / 2); mImage.setBounds(mImageRect); mImage.draw(canvas); } if (mShowStroke) { canvas.drawOval(mInnerRectF, mStrokePaint); } }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float startAngle = 360 - arcAngle / 2f; float finishedSweepAngle = progress / (float) getMax() * arcAngle; float finishedStartAngle = startAngle; paint.setColor(unfinishedStrokeColor); canvas.drawArc(rectF, startAngle, arcAngle, false, paint); canvas.save(); canvas.rotate(90, getWidth() / 2, getHeight() / 2); paint.setColor(finishedStrokeColor); canvas.drawArc(rectF, finishedStartAngle, finishedSweepAngle, false, paint); canvas.restore(); String text = String.valueOf(getProgress()); if (!TextUtils.isEmpty(text)) { textPaint.setColor(textColor); textPaint.setTextSize(textSize); float textHeight = textPaint.descent() + textPaint.ascent(); float textBaseline = (getHeight() - textHeight) / 2.0f; canvas.drawText( text, (getWidth() - textPaint.measureText(text)) / 2.0f, textBaseline, textPaint); textPaint.setTextSize(suffixTextSize); float suffixHeight = textPaint.descent() + textPaint.ascent(); canvas.drawText( suffixText, getWidth() / 2.0f + textPaint.measureText(text) + suffixTextPadding, textBaseline + textHeight - suffixHeight, textPaint); } if (!TextUtils.isEmpty(getBottomText())) { textPaint.setTextSize(bottomTextSize); float bottomTextBaseline = getHeight() - arcBottomHeight - (textPaint.descent() + textPaint.ascent()) / 2; canvas.drawText( getBottomText(), (getWidth() - textPaint.measureText(getBottomText())) / 2.0f, bottomTextBaseline, textPaint); } }
private void drawTextWhenFill(int size, float degrees, Canvas canvas, boolean isTop) { canvas.save(); canvas.rotate(degrees, size / 2f, size / 2f); float delta = isTop ? -size / 4 : size / 4; float textBaseY = size / 2 - (mTextPaint.descent() + mTextPaint.ascent()) / 2 + delta; canvas.drawText( mTextAllCaps ? mTextContent.toUpperCase() : mTextContent, getPaddingLeft() + (size - getPaddingLeft() - getPaddingRight()) / 2, textBaseY, mTextPaint); canvas.restore(); }
public Bitmap makeOverlay(String text) { Rect b = new Rect(); paint.getTextBounds(text, 0, text.length(), b); int bwidth = b.width() + 2; int bheight = b.height() + 2; Bitmap bm = Bitmap.createBitmap(bwidth, bheight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bm); paint.setColor(overlayBackgroundColor); c.drawRect(0, 0, bwidth, bheight, paint); paint.setColor(overlayTextColor); c.drawText( text, -b.left + 1, (bheight / 2) - ((paint.ascent() + paint.descent()) / 2) + 1, paint); return bm; }
public Cell(int year, int month, int dayOfMon, Rect rect, float textSize, boolean bold) { this.year = year; this.month = month; mDayOfMonth = dayOfMon; mBound = rect; mPaint = new Paint(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); mPaint.setTextSize(textSize); mPaint.setColor(Color.WHITE); if (bold) { mPaint.setFakeBoldText(true); } dx = (int) mPaint.measureText(String.valueOf(mDayOfMonth)) / 2; dy = (int) (-mPaint.ascent() + mPaint.descent()) / 2; }
/** * Calculate the bounds for a view's title * * @param index * @param paint * @return */ private Rect calcBounds(int index, Paint paint) { // Calculate the text bounds Rect bounds = new Rect(); CharSequence title = getTitle(index); bounds.right = (int) paint.measureText(title, 0, title.length()); bounds.bottom = (int) (paint.descent() - paint.ascent()); Drawable icon = getIcon(index); if (icon != null) { Rect iconBounds = icon.getBounds(); bounds.right += iconBounds.width() + mIconPadding; } return bounds; }
public boolean exportBitmap(String strPath, int nWidth, int nHeight) { Bitmap bitmap = null; if (null == mBitmap) { bitmap = BitmapFactory.decodeFile(mstrBackground); } else { bitmap = Bitmap.createBitmap(nWidth, nHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); draw(canvas); if (null != mstrText) { // canvas.drawText(mstrText, 40, 40, mPaint); // new antialised Paint Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // text color - #3D3D3D paint.setColor(Color.rgb(61, 61, 61)); // text size in pixels paint.setTextSize(24); // text shadow paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(mstrText, 0, mstrText.length(), bounds); int x = (bitmap.getWidth() - bounds.width()) / 2; int y = (bitmap.getHeight() + bounds.height()) / 2; canvas.drawText(mstrText, x, y, paint); for (String line : mstrText.split("\n")) { canvas.drawText(line, x, y, paint); y += -paint.ascent() + paint.descent(); } } } FileOutputStream out; try { out = new FileOutputStream(strPath); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); bitmap.recycle(); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
@Override protected synchronized void onDraw(Canvas canvas) { canvas.save(); canvas.translate(getPaddingLeft(), getHeight() / 2); // 是否需要绘制默认进度条 boolean noNeedUnReach = false; String text = getProgress() + "%"; // 计算文本的宽度 int textWidth = (int) mPaint.measureText(text); /** draw reach */ // 当前进度比例 float radio = getProgress() * 1.0f / getMax(); float progressX = radio * mRealWidth; // 当前进度加文本宽度超过最大宽度时,不再绘制默认进度 if (progressX + textWidth > mRealWidth) { progressX = mRealWidth - textWidth; noNeedUnReach = true; } float endX = progressX - mTextOffset / 2; if (endX > 0) { mPaint.setColor(mReachColor); mPaint.setStrokeWidth(mReachHeight); canvas.drawLine(0, 0, endX, 0, mPaint); } /** draw text */ mPaint.setColor(mTextColor); int y = (int) (-(mPaint.descent() + mPaint.ascent()) / 2); canvas.drawText(text, progressX, y, mPaint); /** draw unReach */ if (!noNeedUnReach) { float start = progressX + mTextOffset / 2 + textWidth; mPaint.setColor(mUnReachColor); mPaint.setStrokeWidth(mUnReachHeight); canvas.drawLine(start, 0, mRealWidth, 0, mPaint); } canvas.restore(); }
/** * Determines the height of this view * * @param measureSpec A measureSpec packed into an int * @return The height of the view, honoring constraints from measureSpec */ private int measureHeight(int measureSpec) { float result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { // We were told how big to be result = specSize; } else { // Calculate the text bounds RectF bounds = new RectF(); bounds.bottom = mPaintText.descent() - mPaintText.ascent(); result = bounds.bottom - bounds.top + mFooterLineHeight + mFooterPadding + mTopPadding; if (mFooterIndicatorStyle != IndicatorStyle.None) { result += mFooterIndicatorHeight; } } return (int) result; }
/** 设置未读消息偏移,原点为文字的右上角.当控件高度固定,消息提示位置易控制,显示效果佳 */ public void setMsgMargin(int position, float leftPadding, float bottomPadding) { if (position >= mTabCount) { position = mTabCount - 1; } View tabView = mTabsContainer.getChildAt(position); MsgView tipView = (MsgView) tabView.findViewById(R.id.rtv_msg_tip); if (tipView != null) { TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title); mTextPaint.setTextSize(mTextsize); float textWidth = mTextPaint.measureText(tv_tab_title.getText().toString()); float textHeight = mTextPaint.descent() - mTextPaint.ascent(); MarginLayoutParams lp = (MarginLayoutParams) tipView.getLayoutParams(); lp.leftMargin = mTabWidth >= 0 ? (int) (mTabWidth / 2 + textWidth / 2 + dp2px(leftPadding)) : (int) (mTabPadding + textWidth + dp2px(leftPadding)); lp.topMargin = mHeight > 0 ? (int) (mHeight - textHeight) / 2 - dp2px(bottomPadding) : 0; tipView.setLayoutParams(lp); } }
/** * Determines the height of this view * * @param measureSpec A measureSpec packed into an int * @return The height of the view, honoring constraints from measureSpec */ private int measureHeight(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); // We were told how big to be if (specMode == MeasureSpec.EXACTLY) { result = specSize; } // Measure the height else { // Calculate the text bounds Rect bounds = new Rect(); bounds.bottom = (int) (paintText.descent() - paintText.ascent()); result = bounds.bottom - bounds.top + (int) footerTriangleHeight + (int) footerLineHeight + 10; return result; } return result; }