/** * Draws value and label layout. * * @param canvas the canvas for drawing */ private void drawValue(Canvas canvas) { valuePaint.setColor(valueTextColor); valuePaint.drawableState = getDrawableState(); labelPaint.setColor(labelTextColor); labelPaint.drawableState = getDrawableState(); Rect bounds = new Rect(); itemsLayout.getLineBounds(visibleItems / 2, bounds); // draw label if (labelLayout != null) { canvas.save(); canvas.translate(itemsLayout.getWidth() + LABEL_OFFSET, bounds.top); labelLayout.draw(canvas); canvas.restore(); } // draw current value if (valueLayout != null) { canvas.save(); canvas.translate(0, bounds.top + scrollingOffset); valueLayout.draw(canvas); canvas.restore(); } }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (currentMessageObject == null) { return; } if (currentMessageObject.isOutOwner()) { timePaint.setColor(theme.ChatMusicCell_timePaint_color); } else { timePaint.setColor(theme.ChatMusicCell_timePaint_color_2); } radialProgress.draw(canvas); canvas.save(); canvas.translate(timeX + titleX, AndroidUtilities.dp(12) + namesOffset); titleLayout.draw(canvas); canvas.restore(); canvas.save(); if (MediaController.getInstance().isPlayingAudio(currentMessageObject)) { canvas.translate(seekBarX, seekBarY); seekBar.draw(canvas); } else { canvas.translate(timeX + authorX, AndroidUtilities.dp(32) + namesOffset); authorLayout.draw(canvas); } canvas.restore(); canvas.save(); canvas.translate(timeX, AndroidUtilities.dp(52) + namesOffset); timeLayout.draw(canvas); canvas.restore(); }
@Override public void draw(Canvas canvas) { canvas.save(); canvas.translate(minX, minY); layout.draw(canvas); canvas.restore(); }
private Bitmap drawTextToBitmap(String gText) { Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); bitmap.eraseColor(Color.TRANSPARENT); bitmap.setHasAlpha(true); // bitmap.setHasAlpha(true); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.TRANSPARENT); TextPaint textPaint = new TextPaint(); textPaint.setColor(Color.GREEN); textPaint.setTextSize(35); RectF rect = new RectF(0, 0, 300, 300); StaticLayout sl = new StaticLayout( gText, textPaint, (int) rect.width(), Layout.Alignment.ALIGN_NORMAL, 1, 1, false); canvas.save(); sl.draw(canvas); canvas.restore(); // Flip the image. Matrix m = new Matrix(); m.preScale(-1, 1); Bitmap output = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, false); output.setDensity(DisplayMetrics.DENSITY_DEFAULT); return output; }
@Override public void onDraw(Canvas canvas) { // Log.d("TFV", "Font metrics float: "+paint.getFontMetrics(null) * textLines.length +" font // metrics int: "+paint.getFontMetricsInt(null)*textLines.length+" layout height: // "+layout.getHeight()+" layout line bottom: "+layout.getLineBottom(layout.getLineCount() - // 1)+" pad top: "+layout.getTopPadding()+" pad bottom: "+layout.getBottomPadding()+" attempt: // "+(paint.getFontMetricsInt(null)*textLines.length - layout.getTopPadding() + // layout.getBottomPadding())); // canvas.drawColor(backgroundColor); // wipe canvas (just in case)-- doesn't seem necessary but // try this in case of bugs! // Just in case (do not remove!): if (layout == null) updateLayout(); // Calculate new height offset for vertical text centering -- want to start drawing text at // (container height - text height) /2 = (container height - (text box height - (negative // padding)) / 2 = (container height - text box height + negative padding) / 2 float heightOffset = 0.5f * (this.getHeight() - layout.getHeight() + layout.getTopPadding() /* this is negative! */); // Shift canvas downwards and rightwards so that the text will be vertically and horizontally // centred: canvas.translate(this.getPaddingLeft(), heightOffset); // Draw text on shifted canvas: layout.draw(canvas); // Undo translate: canvas.restore(); }
public static void sendTextBitmap( LiveViewAdapter liveView, int pluginId, String text, int bitmapSizeX, int fontSize, int bitmapSize) { // Empty bitmap and link the canvas to it Bitmap bitmap = null; try { bitmap = Bitmap.createBitmap(bitmapSizeX, bitmapSize, Bitmap.Config.RGB_565); } catch (IllegalArgumentException e) { return; } Canvas canvas = new Canvas(bitmap); // Set the text properties in the canvas TextPaint textPaint = new TextPaint(); textPaint.setTextSize(fontSize); textPaint.setColor(Color.WHITE); // Create the text layout and draw it to the canvas StaticLayout textLayout = new StaticLayout(text, textPaint, bitmapSizeX, Layout.Alignment.ALIGN_CENTER, 1, 1, false); textLayout.draw(canvas); try { liveView.sendImageAsBitmap(pluginId, centerX(bitmap), centerY(bitmap), bitmap); } catch (Exception e) { Log.d(PluginConstants.LOG_TAG, "Failed to send bitmap", e); } }
@Override protected void onDraw(Canvas canvas) { if (currentMessageObject == null) { return; } Drawable backgroundDrawable; if (ApplicationLoader.isCustomTheme()) { backgroundDrawable = ResourceLoader.backgroundBlack; } else { backgroundDrawable = ResourceLoader.backgroundBlue; } backgroundDrawable.setBounds( textX - AndroidUtilities.dp(5), AndroidUtilities.dp(5), textX + textWidth + AndroidUtilities.dp(5), AndroidUtilities.dp(9) + textHeight); backgroundDrawable.draw(canvas); if (currentMessageObject.type == 11) { imageReceiver.draw(canvas); } if (textLayout != null) { canvas.save(); canvas.translate(textXLeft, textY); textLayout.draw(canvas); canvas.restore(); } }
protected void drawCenterText(Canvas c) { SpannableString centerText = mChart.getCenterText(); if (mChart.isDrawCenterTextEnabled() && centerText != null) { PointF center = mChart.getCenterCircleBox(); float innerRadius = mChart.isDrawHoleEnabled() && mChart.isHoleTransparent() ? mChart.getRadius() * (mChart.getHoleRadius() / 100f) : mChart.getRadius(); RectF holeRect = mRectBuffer[0]; holeRect.left = center.x - innerRadius; holeRect.top = center.y - innerRadius; holeRect.right = center.x + innerRadius; holeRect.bottom = center.y + innerRadius; RectF boundingRect = mRectBuffer[1]; boundingRect.set(holeRect); float radiusPercent = mChart.getCenterTextRadiusPercent(); if (radiusPercent > 0.0) { boundingRect.inset( (boundingRect.width() - boundingRect.width() * radiusPercent) / 2.f, (boundingRect.height() - boundingRect.height() * radiusPercent) / 2.f); } if (!centerText.equals(mCenterTextLastValue) || !boundingRect.equals(mCenterTextLastBounds)) { // Next time we won't recalculate StaticLayout... mCenterTextLastBounds.set(boundingRect); mCenterTextLastValue = centerText; float width = mCenterTextLastBounds.width(); // If width is 0, it will crash. Always have a minimum of 1 mCenterTextLayout = new StaticLayout( centerText, 0, centerText.length(), mCenterTextPaint, (int) Math.max(Math.ceil(width), 1.f), Layout.Alignment.ALIGN_CENTER, 1.f, 0.f, false); } float layoutHeight = mCenterTextLayout.getHeight(); c.save(); c.translate( boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2.f); mCenterTextLayout.draw(c); c.restore(); } }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (currentMessageObject == null) { return; } avatarImage.draw(canvas); if (nameLayout != null) { canvas.save(); canvas.translate( avatarImage.getImageX() + avatarImage.getImageWidth() + AndroidUtilities.dp(9), AndroidUtilities.dp(10) + namesOffset); namePaint.setColor( AvatarDrawable.getColorForId(currentMessageObject.messageOwner.media.user_id)); nameLayout.draw(canvas); canvas.restore(); } if (phoneLayout != null) { canvas.save(); canvas.translate( avatarImage.getImageX() + avatarImage.getImageWidth() + AndroidUtilities.dp(9), AndroidUtilities.dp(31) + namesOffset); phoneLayout.draw(canvas); canvas.restore(); } if (drawAddButton) { Drawable addContactDrawable; if (currentMessageObject.isOutOwner()) { addContactDrawable = addContactDrawableOut; } else { addContactDrawable = addContactDrawableIn; } setDrawableBounds( addContactDrawable, avatarImage.getImageX() + namesWidth + AndroidUtilities.dp(78), AndroidUtilities.dp(13) + namesOffset); addContactDrawable.draw(canvas); } }
// Set the text size of the text paint object and use a static layout to // render text off screen before measuring private int getTextWidth(CharSequence source, TextPaint paint, int width, float textSize) { // Update the text paint object paint.setTextSize(textSize); // Draw using a static layout StaticLayout layout = new StaticLayout( source, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true); layout.draw(sTextResizeCanvas); return layout.getWidth(); }
/** * Draws items. * * @param canvas the canvas for drawing */ private void drawItems(Canvas canvas) { canvas.save(); int top = itemsLayout.getLineTop(1); canvas.translate(0, -top + scrollingOffset); itemsPaint.setColor(ITEMS_TEXT_COLOR); itemsPaint.drawableState = getDrawableState(); itemsLayout.draw(canvas); canvas.restore(); }
@Override protected void onDraw(Canvas canvas) { int x = (canvas.getWidth() - width) / 2; int y = AndroidUtilities.dp(4); ResourceLoader.backgroundMediaDrawableIn.setBounds(x, y, width + x, height + y); ResourceLoader.backgroundMediaDrawableIn.draw(canvas); canvas.save(); canvas.translate( textX = AndroidUtilities.dp(2 + 9) + x, textY = AndroidUtilities.dp(2 + 9) + y); if (pressedLink != null) { canvas.drawPath(urlPath, urlPaint); } textLayout.draw(canvas); canvas.restore(); }
public Bitmap update(Context context, boolean preview, int watchType) { TextPaint paintSmall = new TextPaint(); paintSmall.setColor(Color.BLACK); paintSmall.setTextSize(FontCache.instance(context).Small.size); paintSmall.setTypeface(FontCache.instance(context).Small.face); TextPaint paintSmallOutline = new TextPaint(); paintSmallOutline.setColor(Color.WHITE); paintSmallOutline.setTextSize(FontCache.instance(context).Small.size); paintSmallOutline.setTypeface(FontCache.instance(context).Small.face); TextPaint paintLarge = new TextPaint(); paintLarge.setColor(Color.BLACK); paintLarge.setTextSize(FontCache.instance(context).Large.size); paintLarge.setTypeface(FontCache.instance(context).Large.face); TextPaint paintLargeOutline = new TextPaint(); paintLargeOutline.setColor(Color.WHITE); paintLargeOutline.setTextSize(FontCache.instance(context).Large.size); paintLargeOutline.setTypeface(FontCache.instance(context).Large.face); MediaControl.TrackInfo lastTrack = MediaControl.getLastTrack(); if (watchType == WatchType.DIGITAL) { Bitmap bitmap = Bitmap.createBitmap(96, 96, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); if (lastTrack.isEmpty()) { canvas.drawBitmap(Utils.getBitmap(context, "media_player_idle.png"), 0, 0, null); } else { canvas.drawBitmap(Utils.getBitmap(context, "media_player.png"), 0, 0, null); StringBuilder lowerText = new StringBuilder(); if (!lastTrack.artist.equals("")) { lowerText.append(lastTrack.artist); } if (!lastTrack.album.equals("")) { if (lowerText.length() > 0) lowerText.append("\n\n"); lowerText.append(lastTrack.album); } Utils.autoText( context, canvas, lastTrack.track, 0, 8, 96, 35, Layout.Alignment.ALIGN_CENTER, Color.BLACK); Utils.autoText( context, canvas, lowerText.toString(), 0, 54, 96, 35, Layout.Alignment.ALIGN_CENTER, Color.BLACK); } drawDigitalAppSwitchIcon(context, canvas, preview); return bitmap; } else if (watchType == WatchType.ANALOG) { Bitmap bitmap = Bitmap.createBitmap(80, 32, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); if (lastTrack.isEmpty()) { canvas.drawBitmap(Utils.getBitmap(context, "media_player_idle_oled.png"), 0, 0, null); } else { canvas.drawBitmap(Utils.getBitmap(context, "media_player_oled.png"), 0, 0, null); TextPaint tp = null; if (paintLarge.measureText(lastTrack.track) < 75) { tp = paintLarge; } else { tp = paintSmall; } canvas.save(); StaticLayout layout = new StaticLayout( lastTrack.track, tp, 75, Layout.Alignment.ALIGN_CENTER, 1.2f, 0, false); int height = layout.getHeight(); int textY = 8 - (height / 2); if (textY < 0) { textY = 0; } canvas.translate(0, textY); // position the text canvas.clipRect(0, 0, 75, 16); layout.draw(canvas); canvas.restore(); canvas.save(); StringBuilder lowerText = new StringBuilder(); if (!lastTrack.artist.equals("")) { lowerText.append(lastTrack.artist); } if (!lastTrack.album.equals("")) { if (lowerText.length() > 0) lowerText.append("\n\n"); lowerText.append(lastTrack.album); } if (paintLarge.measureText(lowerText.toString()) < 75) { tp = paintLarge; } else { tp = paintSmall; } layout = new StaticLayout( lowerText.toString(), tp, 75, Layout.Alignment.ALIGN_CENTER, 1.0f, 0, false); height = layout.getHeight(); textY = 24 - (height / 2); if (textY < 16) { textY = 16; } canvas.translate(0, textY); // position the text canvas.clipRect(0, 0, 75, 16); layout.draw(canvas); canvas.restore(); } return bitmap; } return null; }
private void zeichenInfos(Canvas canvas) { paint.setColor(Color.BLACK); TextPaint tp = new TextPaint(paint); tp.setStrokeWidth(getResources().getDimension(R.dimen.gui_stroke_width_2)); tp.setStyle(Style.FILL_AND_STROKE); tp.setTextSize(getResources().getDimension(R.dimen.gui_header_line1_size)); TextPaint tp2 = new TextPaint(paint); tp2.setStrokeWidth(getResources().getDimension(R.dimen.gui_stroke_width_1)); tp2.setStyle(Style.FILL_AND_STROKE); tp2.setTextSize(getResources().getDimension(R.dimen.gui_header_line2_size)); int padding_right = getResources().getDimensionPixelSize(R.dimen.gui_timegrid_padding_right); int padding_top = getResources().getDimensionPixelSize(R.dimen.gui_timegrid_padding_top); canvas.translate(0, offsettop + padding_top); for (int i = 0; i < hours; i++) { String anz = (timegrid.get(i).getName()).length() <= 2 ? (timegrid.get(i).getName()) : (timegrid.get(i).getName()).substring(0, 2); StaticLayout s = new StaticLayout( anz, tp, width - padding_right, Layout.Alignment.ALIGN_OPPOSITE, 1, 0, false); s.draw(canvas); if (landscape) { String start = timegrid.get(i).getStart().getHour() + ":" + ((timegrid.get(i).getStart().getMinute() + "").length() == 1 ? "0" + timegrid.get(i).getStart().getMinute() : timegrid.get(i).getStart().getMinute() + ""); String end = timegrid.get(i).getEnd().getHour() + ":" + ((timegrid.get(i).getEnd().getMinute() + "").length() == 1 ? "0" + timegrid.get(i).getEnd().getMinute() : timegrid.get(i).getEnd().getMinute() + ""); canvas.translate(0, getResources().getDimension(R.dimen.gui_header_line1_line1_padding)); s = new StaticLayout( start, tp2, width - padding_right, Layout.Alignment.ALIGN_OPPOSITE, 1, 0, false); s.draw(canvas); canvas.translate(0, getResources().getDimension(R.dimen.gui_timegrid_padding_line2_line3)); s = new StaticLayout( end, tp2, width - padding_right, Layout.Alignment.ALIGN_OPPOSITE, 1, 0, false); s.draw(canvas); canvas.translate( 0, -(getResources().getDimension(R.dimen.gui_header_line1_line1_padding) + getResources().getDimension(R.dimen.gui_timegrid_padding_line2_line3))); } canvas.translate(0, (height - offsettop) / hours); } }
static Bitmap smartLines( Context context, Bitmap icon, String header, String[] lines, FontCache.FontSize size) { Properties props = BitmapCache.getProperties(context, "notification.xml"); final int textTop = Integer.parseInt(props.getProperty("textTop", "24")); final int textLeft = Integer.parseInt(props.getProperty("textLeft", "3")); final int textWidth = Integer.parseInt(props.getProperty("textWidth", "90")); final int textHeight = Integer.parseInt(props.getProperty("textHeight", "69")); final int iconTop = Integer.parseInt(props.getProperty("iconTop", "0")); final int iconLeft = Integer.parseInt(props.getProperty("iconLeft", "0")); final int headerLeft = Integer.parseInt(props.getProperty("headerLeft", "20")); final int headerBaseline = Integer.parseInt(props.getProperty("headerBaseline", "15")); final int headerColor = props.getProperty("headerColor", "white").equalsIgnoreCase("black") ? Color.BLACK : Color.WHITE; final int textColor = props.getProperty("textColor", "black").equalsIgnoreCase("black") ? Color.BLACK : Color.WHITE; FontInfo font = FontCache.instance(context).Get(size); Bitmap bitmap = Bitmap.createBitmap(96, 96, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); Paint paintHead = new Paint(); paintHead.setColor(headerColor); paintHead.setTextSize(FontCache.instance(context).Large.size); paintHead.setTypeface(FontCache.instance(context).Large.face); Paint paint = new Paint(); paint.setColor(textColor); paint.setTextSize(font.size); paint.setTypeface(font.face); canvas.drawColor(Color.WHITE); canvas.drawBitmap(Utils.getBitmap(context, "notify_background.png"), 0, 0, null); int iconHeight = 16; if (icon != null) { iconHeight = Math.max(16, icon.getHeight()); // make sure the text fits // align icon to bottom of text canvas.drawBitmap(icon, iconLeft, iconTop + iconHeight - icon.getHeight(), paint); } canvas.drawText(header, headerLeft, headerBaseline, paintHead); String body = ""; for (String line : lines) { if (body.length() > 0) body += "\n\n"; body += line; } TextPaint textPaint = new TextPaint(paint); StaticLayout staticLayout = new StaticLayout( body, textPaint, textWidth, android.text.Layout.Alignment.ALIGN_CENTER, 1.3f, 0, false); int layoutHeight = staticLayout.getHeight(); int textY = textTop + (textHeight / 2) - (layoutHeight / 2); if (textY < textTop) textY = textTop; canvas.translate(textLeft, textY); // position the text staticLayout.draw(canvas); return bitmap; }
@Override protected void onDraw(Canvas canvas) { if (user == null && chat == null && encryptedChat == null) { return; } if (useSeparator) { if (LocaleController.isRTL) { canvas.drawLine( 0, getMeasuredHeight() - 1, getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1, linePaint); } else { canvas.drawLine( AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1, getMeasuredWidth(), getMeasuredHeight() - 1, linePaint); } } if (drawAlpha != 1) { canvas.saveLayerAlpha( 0, 0, canvas.getWidth(), canvas.getHeight(), (int) (255 * drawAlpha), Canvas.HAS_ALPHA_LAYER_SAVE_FLAG); } if (drawNameLock) { setDrawableBounds(lockDrawable, nameLockLeft, nameLockTop); lockDrawable.draw(canvas); } else if (drawNameGroup) { setDrawableBounds(groupDrawable, nameLockLeft, nameLockTop); groupDrawable.draw(canvas); } else if (drawNameBroadcast) { setDrawableBounds(broadcastDrawable, nameLockLeft, nameLockTop); broadcastDrawable.draw(canvas); } else if (drawNameBot) { setDrawableBounds(botDrawable, nameLockLeft, nameLockTop); botDrawable.draw(canvas); } if (nameLayout != null) { canvas.save(); canvas.translate(nameLeft, nameTop); nameLayout.draw(canvas); canvas.restore(); if (drawCheck) { if (LocaleController.isRTL) { setDrawableBounds( checkDrawable, nameLeft - AndroidUtilities.dp(4) - checkDrawable.getIntrinsicWidth(), nameLockTop); } else { setDrawableBounds( checkDrawable, nameLeft + (int) nameLayout.getLineWidth(0) + AndroidUtilities.dp(4), nameLockTop); } checkDrawable.draw(canvas); } } if (onlineLayout != null) { canvas.save(); canvas.translate(onlineLeft, AndroidUtilities.dp(40)); onlineLayout.draw(canvas); canvas.restore(); } if (countLayout != null) { if (MessagesController.getInstance().isDialogMuted(dialog_id)) { setDrawableBounds( countDrawableGrey, countLeft - AndroidUtilities.dp(5.5f), countTop, countWidth + AndroidUtilities.dp(11), countDrawableGrey.getIntrinsicHeight()); countDrawableGrey.draw(canvas); } else { setDrawableBounds( countDrawable, countLeft - AndroidUtilities.dp(5.5f), countTop, countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight()); countDrawable.draw(canvas); } canvas.save(); canvas.translate(countLeft, countTop + AndroidUtilities.dp(4)); countLayout.draw(canvas); canvas.restore(); } avatarImage.draw(canvas); }
/** * draws the description text in the center of the pie chart makes most sense when center-hole is * enabled */ protected void drawCenterText(Canvas c) { CharSequence centerText = mChart.getCenterText(); if (mChart.isDrawCenterTextEnabled() && centerText != null) { PointF center = mChart.getCenterCircleBox(); float innerRadius = mChart.isDrawHoleEnabled() ? mChart.getRadius() * (mChart.getHoleRadius() / 100f) : mChart.getRadius(); RectF holeRect = mRectBuffer[0]; holeRect.left = center.x - innerRadius; holeRect.top = center.y - innerRadius; holeRect.right = center.x + innerRadius; holeRect.bottom = center.y + innerRadius; RectF boundingRect = mRectBuffer[1]; boundingRect.set(holeRect); float radiusPercent = mChart.getCenterTextRadiusPercent() / 100f; if (radiusPercent > 0.0) { boundingRect.inset( (boundingRect.width() - boundingRect.width() * radiusPercent) / 2.f, (boundingRect.height() - boundingRect.height() * radiusPercent) / 2.f); } if (!centerText.equals(mCenterTextLastValue) || !boundingRect.equals(mCenterTextLastBounds)) { // Next time we won't recalculate StaticLayout... mCenterTextLastBounds.set(boundingRect); mCenterTextLastValue = centerText; float width = mCenterTextLastBounds.width(); // If width is 0, it will crash. Always have a minimum of 1 mCenterTextLayout = new StaticLayout( centerText, 0, centerText.length(), mCenterTextPaint, (int) Math.max(Math.ceil(width), 1.f), Layout.Alignment.ALIGN_CENTER, 1.f, 0.f, false); } // I wish we could make an ellipse clipping path on Android to clip to the hole... // If we ever find out how, this is the place to add it, based on holeRect // float layoutWidth = Utils.getStaticLayoutMaxWidth(mCenterTextLayout); float layoutHeight = mCenterTextLayout.getHeight(); c.save(); c.translate( boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2.f); mCenterTextLayout.draw(c); c.restore(); // } // // else { // // // // get all lines from the text // String[] lines = centerText.toString().split("\n"); // // float maxlineheight = 0f; // // // calc the maximum line height // for (String line : lines) { // float curHeight = Utils.calcTextHeight(mCenterTextPaint, line); // if (curHeight > maxlineheight) // maxlineheight = curHeight; // } // // float linespacing = maxlineheight * 0.25f; // // float totalheight = maxlineheight * lines.length - linespacing * // (lines.length - 1); // // int cnt = lines.length; // // float y = center.y; // // for (int i = 0; i < lines.length; i++) { // // String line = lines[lines.length - i - 1]; // // // // c.drawText(line, center.x, y // + maxlineheight * cnt - totalheight / 2f, // mCenterTextPaint); // cnt--; // y -= linespacing; // } // } } }
@Override protected void onDraw(@NonNull Canvas canvas) { int startX = getScrollX() + (iconLeftBitmaps == null ? 0 : (iconOuterWidth + iconPadding)); int endX = getScrollX() + (iconRightBitmaps == null ? getWidth() : getWidth() - iconOuterWidth - iconPadding); int lineStartY = getScrollY() + getHeight() - getPaddingBottom(); // draw the icon(s) paint.setAlpha(255); if (iconLeftBitmaps != null) { Bitmap icon = iconLeftBitmaps[!isInternalValid() ? 3 : !isEnabled() ? 2 : hasFocus() ? 1 : 0]; int iconLeft = startX - iconPadding - iconOuterWidth + (iconOuterWidth - icon.getWidth()) / 2; int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - icon.getHeight()) / 2; canvas.drawBitmap(icon, iconLeft, iconTop, paint); } if (iconRightBitmaps != null) { Bitmap icon = iconRightBitmaps[!isInternalValid() ? 3 : !isEnabled() ? 2 : hasFocus() ? 1 : 0]; int iconRight = endX + iconPadding + (iconOuterWidth - icon.getWidth()) / 2; int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - icon.getHeight()) / 2; canvas.drawBitmap(icon, iconRight, iconTop, paint); } // draw the clear button if (hasFocus() && showClearButton && !TextUtils.isEmpty(getText()) && isEnabled()) { paint.setAlpha(255); int buttonLeft; if (isRTL()) { buttonLeft = startX; } else { buttonLeft = endX - iconOuterWidth; } Bitmap clearButtonBitmap = clearButtonBitmaps[0]; buttonLeft += (iconOuterWidth - clearButtonBitmap.getWidth()) / 2; int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - clearButtonBitmap.getHeight()) / 2; canvas.drawBitmap(clearButtonBitmap, buttonLeft, iconTop, paint); } // draw the underline if (!hideUnderline) { lineStartY += bottomSpacing; if (!isInternalValid()) { // not valid paint.setColor(errorColor); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint); } else if (!isEnabled()) { // disabled paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x44000000); float interval = getPixel(1); for (float xOffset = 0; xOffset < getWidth(); xOffset += interval * 3) { canvas.drawRect( startX + xOffset, lineStartY, startX + xOffset + interval, lineStartY + getPixel(1), paint); } } else if (hasFocus()) { // focused paint.setColor(primaryColor); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint); } else { // normal paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x1E000000); canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(1), paint); } } textPaint.setTextSize(bottomTextSize); Paint.FontMetrics textMetrics = textPaint.getFontMetrics(); float relativeHeight = -textMetrics.ascent - textMetrics.descent; float bottomTextPadding = bottomTextSize + textMetrics.ascent + textMetrics.descent; // draw the characters counter if ((hasFocus() && hasCharactersCounter()) || !isCharactersCountValid()) { textPaint.setColor( isCharactersCountValid() ? (baseColor & 0x00ffffff | 0x44000000) : errorColor); String charactersCounterText = getCharactersCounterText(); canvas.drawText( charactersCounterText, isRTL() ? startX : endX - textPaint.measureText(charactersCounterText), lineStartY + bottomSpacing + relativeHeight, textPaint); } // draw the bottom text if (textLayout != null) { if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) { // error text or helper text textPaint.setColor( tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : (baseColor & 0x00ffffff | 0x44000000)); canvas.save(); if (isRTL()) { canvas.translate( endX - textLayout.getWidth(), lineStartY + bottomSpacing - bottomTextPadding); } else { canvas.translate( startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding); } textLayout.draw(canvas); canvas.restore(); } } // draw the floating label if (floatingLabelEnabled && !TextUtils.isEmpty(floatingLabelText)) { textPaint.setTextSize(floatingLabelTextSize); // calculate the text color textPaint.setColor( (Integer) focusEvaluator.evaluate( focusFraction * (isEnabled() ? 1 : 0), floatingLabelTextColor != -1 ? floatingLabelTextColor : (baseColor & 0x00ffffff | 0x44000000), primaryColor)); // calculate the horizontal position float floatingLabelWidth = textPaint.measureText(floatingLabelText.toString()); int floatingLabelStartX; if ((getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL()) { floatingLabelStartX = (int) (endX - floatingLabelWidth); } else if ((getGravity() & Gravity.LEFT) == Gravity.LEFT) { floatingLabelStartX = startX; } else { floatingLabelStartX = startX + (int) (getInnerPaddingLeft() + (getWidth() - getInnerPaddingLeft() - getInnerPaddingRight() - floatingLabelWidth) / 2); } // calculate the vertical position int distance = floatingLabelPadding; int floatingLabelStartY = (int) (innerPaddingTop + floatingLabelTextSize + floatingLabelPadding - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction) + getScrollY()); // calculate the alpha int alpha = ((int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction * (isEnabled() ? 1 : 0) + 0.26f) * (floatingLabelTextColor != -1 ? 1 : Color.alpha(floatingLabelTextColor) / 256f))); textPaint.setAlpha(alpha); // draw the floating label canvas.drawText( floatingLabelText.toString(), floatingLabelStartX, floatingLabelStartY, textPaint); } // draw the bottom ellipsis if (hasFocus() && singleLineEllipsis && getScrollX() != 0) { paint.setColor(isInternalValid() ? primaryColor : errorColor); float startY = lineStartY + bottomSpacing; int ellipsisStartX; if (isRTL()) { ellipsisStartX = endX; } else { ellipsisStartX = startX; } int signum = isRTL() ? -1 : 1; canvas.drawCircle( ellipsisStartX + signum * bottomEllipsisSize / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint); canvas.drawCircle( ellipsisStartX + signum * bottomEllipsisSize * 5 / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint); canvas.drawCircle( ellipsisStartX + signum * bottomEllipsisSize * 9 / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint); } // draw the original things super.onDraw(canvas); }
/** * Resize the text size with specified width and height * * @param width * @param height */ public void resizeText(int width, int height) { CharSequence text = getText(); // Do not resize if the view does not have dimensions or there is no // text // or if mTextSize has not been initialized if (text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) { return; } // Get the text view's paint object TextPaint textPaint = getPaint(); // Store the current text size float oldTextSize = textPaint.getTextSize(); // If there is a max text size set, use the lesser of that and the // default text size float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize; // Get the required text height int textHeight = getTextHeight(text, textPaint, width, targetTextSize); int textWidth = getTextWidth(text, textPaint, width, targetTextSize); int lineHeight = getTextHeight(text, textPaint, width * 10, targetTextSize); // Until we either fit within our text view or we had reached our min // text size, incrementally try smaller sizes while (((textHeight > height) || (textWidth > width) || (textHeight > lineHeight)) && targetTextSize > mMinTextSize) { targetTextSize = Math.max(targetTextSize - 2, mMinTextSize); textHeight = getTextHeight(text, textPaint, width, targetTextSize); textWidth = getTextWidth(text, textPaint, width, targetTextSize); lineHeight = getTextHeight(text, textPaint, width * 10, targetTextSize); } // If we had reached our minimum text size and still don't fit, append // an ellipsis if (mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) { // Draw using a static layout StaticLayout layout = new StaticLayout( text, textPaint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false); layout.draw(sTextResizeCanvas); int lastLine = layout.getLineForVertical(height) - 1; int start = layout.getLineStart(lastLine); int end = layout.getLineEnd(lastLine); float lineWidth = layout.getLineWidth(lastLine); float ellipseWidth = textPaint.measureText(mEllipsis); // Trim characters off until we have enough room to draw the // ellipsis while (width < lineWidth + ellipseWidth) { lineWidth = textPaint.measureText(text.subSequence(start, --end + 1).toString()); } setText(text.subSequence(0, end) + mEllipsis); } // Some devices try to auto adjust line spacing, so force default line // spacing // and invalidate the layout as a side effect textPaint.setTextSize(targetTextSize); setLineSpacing(mSpacingAdd, mSpacingMult); // Notify the listener if registered if (mTextResizeListener != null) { mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize); } // Reset force resize flag mNeedsResize = false; }
static Bitmap[] smartNotify(Context context, Bitmap icon, String header, String body) { Properties props = BitmapCache.getProperties(context, "notification_sticky.xml"); final int textTop = Integer.parseInt(props.getProperty("textTop", "24")); final int textLeft = Integer.parseInt(props.getProperty("textLeft", "3")); final int textWidth = Integer.parseInt(props.getProperty("textWidth", "85")); final int textHeight = Integer.parseInt(props.getProperty("textHeight", "69")); final int iconTop = Integer.parseInt(props.getProperty("iconTop", "0")); final int iconLeft = Integer.parseInt(props.getProperty("iconLeft", "0")); final int headerLeft = Integer.parseInt(props.getProperty("headerLeft", "20")); final int headerBaseline = Integer.parseInt(props.getProperty("headerBaseline", "15")); final int headerColor = props.getProperty("headerColor", "white").equalsIgnoreCase("black") ? Color.BLACK : Color.WHITE; final int textColor = props.getProperty("textColor", "black").equalsIgnoreCase("black") ? Color.BLACK : Color.WHITE; final int arrowUpLeft = Integer.parseInt(props.getProperty("arrowUpLeft", "91")); final int arrowUpTop = Integer.parseInt(props.getProperty("arrowUpTop", "23")); final int arrowDownLeft = Integer.parseInt(props.getProperty("arrowDownLeft", "91")); final int arrowDownTop = Integer.parseInt(props.getProperty("arrowDownTop", "56")); final int closeLeft = Integer.parseInt(props.getProperty("closeLeft", "91")); final int closeTop = Integer.parseInt(props.getProperty("closeTop", "89")); FontInfo font = FontCache.instance(context).Get(); List<Bitmap> bitmaps = new ArrayList<Bitmap>(); Paint paintHead = new Paint(); paintHead.setColor(headerColor); paintHead.setTextSize(FontCache.instance(context).Large.size); paintHead.setTypeface(FontCache.instance(context).Large.face); Paint paint = new Paint(); paint.setColor(textColor); paint.setTextSize(font.size); paint.setTypeface(font.face); Paint whitePaint = new Paint(); whitePaint.setColor(Color.WHITE); TextPaint textPaint = new TextPaint(paint); StaticLayout staticLayout = new StaticLayout( body, textPaint, textWidth, android.text.Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); int iconHeight = 16; int iconOffset = 0; if (icon != null) { iconHeight = Math.max(16, icon.getHeight()); iconOffset = iconHeight - icon.getHeight(); // align icon to bottom of text } int h = staticLayout.getHeight(); int y = 0; int displayHeight = textHeight + 3; int scroll = textHeight - font.size; boolean more = true; int pages = 0; while (more) { more = false; Bitmap bitmap = Bitmap.createBitmap(96, 96, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); canvas.drawBitmap(Utils.getBitmap(context, "notify_background_sticky.png"), 0, 0, null); canvas.save(); canvas.translate(textLeft, textTop - y); // position the text canvas.clipRect(0, y, textWidth, textHeight + y); staticLayout.draw(canvas); canvas.restore(); // Draw header if (icon != null) canvas.drawBitmap(icon, iconLeft, iconOffset + iconTop, paint); canvas.drawText(header, headerLeft, headerBaseline, paintHead); if (y > 0) canvas.drawBitmap(Utils.getBitmap(context, "arrow_up.bmp"), arrowUpLeft, arrowUpTop, null); if ((h - y) > (displayHeight) && pages < 10) { more = true; pages++; canvas.drawBitmap( Utils.getBitmap(context, "arrow_down.bmp"), arrowDownLeft, arrowDownTop, null); } canvas.drawBitmap(Utils.getBitmap(context, "close.bmp"), closeLeft, closeTop, null); y += scroll; bitmaps.add(bitmap); } Bitmap[] bitmapArray = new Bitmap[bitmaps.size()]; bitmaps.toArray(bitmapArray); return bitmapArray; }
private InternalWidget.WidgetData GenWidget(String widget_id) { InternalWidget.WidgetData widget = new InternalWidget.WidgetData(); widget.priority = meetingTime.equals("None") ? 0 : 1; String iconFile = "idle_calendar.bmp"; if (widget_id.equals(id_0)) { widget.id = id_0; widget.description = desc_0; widget.width = 24; widget.height = 32; } else if (widget_id.equals(id_1)) { widget.id = id_1; widget.description = desc_1; widget.width = 96; widget.height = 32; } else if (widget_id.equals(id_2)) { widget.id = id_2; widget.description = desc_2; widget.width = 16; widget.height = 16; iconFile = "idle_calendar_10.bmp"; } else if (widget_id.equals(id_3)) { widget.id = id_3; widget.description = desc_3; widget.width = 80; widget.height = 16; iconFile = "idle_calendar_10.bmp"; } else if (widget_id.equals(id_4)) { widget.id = id_4; widget.description = desc_4; widget.width = 48; widget.height = 32; iconFile = null; } else if (widget_id.equals(id_5)) { widget.id = id_5; widget.description = desc_5; widget.width = 40; widget.height = 16; iconFile = null; } Bitmap icon = iconFile == null ? null : Utils.getBitmap(context, iconFile); widget.bitmap = Bitmap.createBitmap(widget.width, widget.height, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(widget.bitmap); canvas.drawColor(Color.WHITE); if (widget.height == 16 && icon != null) { if (icon != null) canvas.drawBitmap(icon, widget.width == 16 ? 2 : 0, 0, null); if (meetingTime.equals("None")) canvas.drawText("-", widget.width == 16 ? 8 : 6, 15, paintSmallNumerals); else { // Strip out colon to make it fit; String time = meetingTime.replace(":", ""); if (widget.width == 16) { canvas.drawText(time, 8, 15, paintSmallNumerals); } else { paintSmallNumerals.setTextAlign(Align.LEFT); canvas.drawText(time, 0, 15, paintSmallNumerals); paintSmallNumerals.setTextAlign(Align.CENTER); } } } else if (icon != null) { canvas.drawBitmap(icon, 0, 3, null); if ((Preferences.displayLocationInSmallCalendarWidget) && (!meetingTime.equals("None")) && (meetingLocation != null) && (!meetingLocation.equals("---")) && (widget_id.equals(id_0)) && (meetingLocation.length() > 0) && (meetingLocation.length() <= 3)) { canvas.drawText(meetingLocation, 12, 15, paintSmall); } else { Calendar c = Calendar.getInstance(); if ((Preferences.eventDateInCalendarWidget) && (!meetingTime.equals("None"))) { c.setTimeInMillis(meetingStartTimestamp); } int dayOfMonth = c.get(Calendar.DAY_OF_MONTH); if (dayOfMonth < 10) { canvas.drawText("" + dayOfMonth, 12, 16, paintNumerals); } else { canvas.drawText("" + dayOfMonth / 10, 9, 16, paintNumerals); canvas.drawText("" + dayOfMonth % 10, 15, 16, paintNumerals); } } canvas.drawText(meetingTime, 12, 30, paintSmall); } String text = ""; if (iconFile == null) text = meetingTime; if ((meetingTitle != null)) { if (text.length() > 0) text += " : "; text += meetingTitle; } if ((meetingLocation != null) && (meetingLocation.length() > 0)) text += " - " + meetingLocation; if (widget_id.equals(id_1) || widget_id.equals(id_4)) { paintSmall.setTextAlign(Align.LEFT); int iconSpace = iconFile == null ? 0 : 25; canvas.save(); StaticLayout layout = new StaticLayout( text, paintSmall, widget.width - iconSpace, Layout.Alignment.ALIGN_CENTER, 1.2f, 0, false); int height = layout.getHeight(); int textY = 16 - (height / 2); if (textY < 0) { textY = 0; } canvas.translate(iconSpace, textY); // position the text layout.draw(canvas); canvas.restore(); paintSmall.setTextAlign(Align.CENTER); } else if (widget_id.equals(id_3) || widget_id.equals(id_5)) { paintSmall.setTextAlign(Align.LEFT); int iconSpace = iconFile == null ? 0 : 11; canvas.save(); StaticLayout layout = new StaticLayout( text, paintSmall, widget.width - iconSpace, Layout.Alignment.ALIGN_CENTER, 1.0f, 0, false); int height = layout.getHeight(); int textY = 8 - (height / 2); if (textY < 0) { textY = 0; } canvas.translate(iconSpace, textY); // position the text layout.draw(canvas); canvas.restore(); paintSmall.setTextAlign(Align.CENTER); } return widget; }
private void drawText(Canvas canvas, Bitmap bitmap, int percent, Boolean isShort) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); Boolean isGradient = prefs.getBoolean("gradientKey", true); int size = 0; int smallDummyHeight = 0; while (smallDummyHeight < ((bitmap.getHeight() / 100) * percent)) { TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG); size = size + 1; paintText.setTextSize(size); paintText.setStyle(Paint.Style.FILL_AND_STROKE); paintText.setShadowLayer(3f, 3f, 3f, Color.BLACK); Rect smallDummyRect = new Rect(); paintText.getTextBounds(gagTitle, 0, gagTitle.length(), smallDummyRect); smallDummyHeight = smallDummyRect.height(); } int pL = bitmap.getWidth() / 100; int pT = bitmap.getHeight() / 100; if (isShort) { TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG); paintText.setColor(Color.WHITE); paintText.setTextSize(size); paintText.setStyle(Paint.Style.FILL_AND_STROKE); paintText.setShadowLayer(3f, 3f, 3f, Color.BLACK); Rect rectText = new Rect(); paintText.getTextBounds(gagTitle, 0, gagTitle.length(), rectText); if (isGradient) { int GRADIENT_HEIGHT = rectText.height(); Paint paint = new Paint(); LinearGradient shader = new LinearGradient( 0, 0, 0, GRADIENT_HEIGHT, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP); paint.setShader(shader); canvas.drawRect(0, 0, newBitmap.getWidth(), GRADIENT_HEIGHT, paint); } canvas.drawText(gagTitle, pL, rectText.height() + (3 * pT) / 2, paintText); } else { TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG); paintText.setColor(Color.WHITE); paintText.setTextSize(size); paintText.setStyle(Paint.Style.FILL_AND_STROKE); paintText.setShadowLayer(3f, 3f, 3f, Color.BLACK); StaticLayout mTextLayout = new StaticLayout( gagTitle, paintText, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); if (isGradient) { int GRADIENT_HEIGHT = mTextLayout.getHeight(); Paint paint = new Paint(); LinearGradient shader = new LinearGradient( 0, 0, 0, GRADIENT_HEIGHT, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP); paint.setShader(shader); canvas.drawRect(0, 0, newBitmap.getWidth(), GRADIENT_HEIGHT, paint); } canvas.save(); canvas.translate(pL, pT); mTextLayout.draw(canvas); canvas.restore(); } }
public List<Integer> getPageOffsets(CharSequence text, boolean includePageNumbers) { if (text == null) { return new ArrayList<Integer>(); } List<Integer> pageOffsets = new ArrayList<Integer>(); TextPaint textPaint = bookView.getInnerView().getPaint(); int boundedWidth = bookView.getInnerView().getWidth(); LOG.debug("Page width: " + boundedWidth); StaticLayout layout = layoutFactory.create(text, textPaint, boundedWidth, bookView.getLineSpacing()); LOG.debug("Layout height: " + layout.getHeight()); layout.draw(new Canvas()); // Subtract the height of the top margin int pageHeight = bookView.getHeight() - bookView.getVerticalMargin(); if (includePageNumbers) { String bottomSpace = "0\n"; StaticLayout numLayout = layoutFactory.create(bottomSpace, textPaint, boundedWidth, bookView.getLineSpacing()); numLayout.draw(new Canvas()); // Subtract the height needed to show page numbers, or the // height of the margin, whichever is more pageHeight = pageHeight - Math.max(numLayout.getHeight(), bookView.getVerticalMargin()); } else { // Just subtract the bottom margin pageHeight = pageHeight - bookView.getVerticalMargin(); } LOG.debug("Got pageHeight " + pageHeight); int totalLines = layout.getLineCount(); int topLineNextPage = -1; int pageStartOffset = 0; while (topLineNextPage < totalLines - 1) { LOG.debug("Processing line " + topLineNextPage + " / " + totalLines); int topLine = layout.getLineForOffset(pageStartOffset); topLineNextPage = layout.getLineForVertical(layout.getLineTop(topLine) + pageHeight); LOG.debug("topLine " + topLine + " / " + topLineNextPage); if (topLineNextPage == topLine) { // If lines are bigger than can fit on a page topLineNextPage = topLine + 1; } int pageEnd = layout.getLineEnd(topLineNextPage - 1); LOG.debug("pageStartOffset=" + pageStartOffset + ", pageEnd=" + pageEnd); if (pageEnd > pageStartOffset) { if (text.subSequence(pageStartOffset, pageEnd).toString().trim().length() > 0) { pageOffsets.add(pageStartOffset); } pageStartOffset = layout.getLineStart(topLineNextPage); } } return pageOffsets; }