List of usage examples for android.graphics Paint setTextSize
public void setTextSize(float textSize)
From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
private void init(Context context) { setFocusable(true);/*from w ww . j a va2 s. c o m*/ // Allow focus in touch mode so that we can do keyboard shortcuts // even after we've entered touch mode. setFocusableInTouchMode(true); setClickable(true); setOnCreateContextMenuListener(this); mFirstDayOfWeek = Time.MONDAY; mCurrentTime = new Time(DayUtils.getTimeZone(context, mTZUpdater)); long currentTime = System.currentTimeMillis(); mCurrentTime.set(currentTime); mTodayJulianDay = Time.getJulianDay(currentTime, mCurrentTime.gmtoff); mEventTextPaint.setTextSize(EVENT_TEXT_FONT_SIZE); mEventTextPaint.setTextAlign(Align.LEFT); mEventTextPaint.setAntiAlias(true); int gridLineColor = 0xff707070; Paint p = mSelectionPaint; p.setColor(gridLineColor); p.setStyle(Style.FILL); p.setAntiAlias(false); p = mPaint; p.setAntiAlias(true); // Long day names mDayStrsLong = new String[14]; // Allocate space for 2 weeks worth of weekday names so that we can // easily start the week display at any week day. mDayStrs = new String[14]; // Also create an array of 2-letter abbreviations. mDayStrs2Letter = new String[14]; for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) { int index = i - Calendar.SUNDAY; mDayStrsLong[index] = DateUtils.getDayOfWeekString(i, DateUtils.LENGTH_LONG).toUpperCase(); mDayStrsLong[index + 7] = mDayStrsLong[index]; // e.g. Tue for Tuesday mDayStrs[index] = DateUtils.getDayOfWeekString(i, DateUtils.LENGTH_MEDIUM).toUpperCase(); mDayStrs[index + 7] = mDayStrs[index]; // e.g. Tu for Tuesday mDayStrs2Letter[index] = DateUtils.getDayOfWeekString(i, DateUtils.LENGTH_SHORT).toUpperCase(); // If we don't have 2-letter day strings, fall back to 1-letter. if (mDayStrs2Letter[index].equals(mDayStrs[index])) { mDayStrs2Letter[index] = DateUtils.getDayOfWeekString(i, DateUtils.LENGTH_SHORTEST); } mDayStrs2Letter[index + 7] = mDayStrs2Letter[index]; } // Figure out how much space we need for the 3-letter abbrev names // in the worst case. p.setTextSize(DATE_HEADER_FONT_SIZE); p.setTypeface(mBold); String[] dateStrs = { " 28", " 30" }; mDateStrWidth = computeMaxStringWidth(0, dateStrs, p); Time time = new Time(); time.setJulianDay(mFirstJulianDay); String s = SimpleDateFormat.getDateInstance().format(new Date(time.toMillis(false))); mDateStrWidthLong = computeMaxStringWidth(0, new String[] { s }, p); p.setTextSize(DAY_HEADER_FONT_SIZE); mDateStrWidth += computeMaxStringWidth(0, mDayStrs, p); mDateStrWidthLong += computeMaxStringWidth(0, mDayStrsLong, p); p.setTextSize(HOURS_TEXT_SIZE); p.setTypeface(null); handleOnResume(); mAmString = DateUtils.getAMPMString(Calendar.AM).toUpperCase(); mPmString = DateUtils.getAMPMString(Calendar.PM).toUpperCase(); String[] ampm = { mAmString, mPmString }; p.setTextSize(AMPM_TEXT_SIZE); mHoursWidth = Math.max(HOURS_MARGIN, computeMaxStringWidth(mHoursWidth, ampm, p) + HOURS_RIGHT_MARGIN); mHoursWidth = Math.max(MIN_HOURS_WIDTH, mHoursWidth); LayoutInflater inflater; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); Resources.Theme dialogTheme = getResources().newTheme(); dialogTheme.applyStyle(android.R.style.Theme_Dialog, true); TypedArray ta = dialogTheme.obtainStyledAttributes(new int[] { android.R.attr.windowBackground }); // Catch long clicks for creating a new event mBaseDate = new Time(DayUtils.getTimeZone(context, mTZUpdater)); long millis = System.currentTimeMillis(); mBaseDate.set(millis); mEarliestStartHour = new int[mNumDays]; mHasAllDayEvent = new boolean[mNumDays]; // mLines is the array of points used with Canvas.drawLines() in // drawGridBackground() and drawAllDayEvents(). Its size depends // on the max number of lines that can ever be drawn by any single // drawLines() call in either of those methods. final int maxGridLines = (24 + 1) // max horizontal lines we might draw + (mNumDays + 1); // max vertical lines we might draw mLines = new float[maxGridLines * 4]; }
From source file:com.semfapp.adamdilger.semf.Take5PdfDocument.java
public PdfDocument createDocument() { //create new document PdfDocument document = new PdfDocument(); // crate a page description PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(A4_WIDTH, A4_HEIGHT, 1).create(); // start a page PdfDocument.Page page = document.startPage(pageInfo); can = page.getCanvas();//from w ww . ja va2 s . c o m Paint paint = new Paint(); paint.setStrokeWidth(0.5f); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); can.drawRect(1, 1, 594, 395, paint); /** * Page one Text Fields */ drawText("Job Reference:", 9, 21, FONT14, carlitoBold); drawText("Date:", 354, 21, FONT14, carlitoBold); drawText("Time:", 473, 21, FONT14, carlitoBold); drawText("Location:", 9, 48, FONT14, carlitoBold); drawText("Task:", 261, 48, FONT14, carlitoBold); paint.setPathEffect(new DashPathEffect(new float[] { 1.5f, 1 }, 0)); can.drawLine(85, 36, 350, 36, paint); can.drawLine(386, 36, 468, 36, paint); can.drawLine(506, 36, 585, 36, paint); can.drawLine(59, 60, 256, 60, paint); can.drawLine(291, 60, 585, 60, paint); if (mEditTextValues[0] != null) { drawText(mEditTextValues[0], 98, 19, FONT14, roboto); } if (mEditTextValues[1] != null) { drawText(mEditTextValues[1], 65, 44, FONT14, roboto); } if (mEditTextValues[2] != null) { drawText(mEditTextValues[2], 297, 44, FONT14, roboto); } drawText(mDateString, 390, 19, FONT14, roboto); drawText(mTimeString, 509, 19, FONT14, roboto); /** * Section One (Stop, step back...) */ drawHeader1(8, 69, 202, "Stop, step back and think", 1); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(0.35f); paint.setPathEffect(null); int left, top; //draw left boxes left = 177; top = 108; for (int x = 0; x < mCheckBoxSectionOne.size(); x++) { Take5Data.CheckValue isYes = mCheckBoxSectionOne.get(x).getCheckValue(); float topLoc = top + (x * 33); if (x < 1) { //allowing for different 1st row size drawBox(left, topLoc, paint, true, isYes); } else { float newTop = topLoc - 6; if (x < 3) { drawBox(left, newTop, paint, false, isYes); } else { drawBox(left, newTop, paint, true, isYes); } } } DashPathEffect pathEffect = new DashPathEffect(new float[] { 1, 1.5f }, 0); drawText(mCheckBoxSectionOne.get(0).getHeading(), 13, 113, FONT12, carlitoBold); for (int x = 1; x < mCheckBoxSectionOne.size(); x++) { int height = 132 + ((x - 1) * 33); drawText(mCheckBoxSectionOne.get(x).getHeading(), 13, height, FONT12, carlitoBold); paint.setPathEffect(pathEffect); can.drawLine(10, height - 6, 222, height - 6, paint); paint.setPathEffect(null); } /** * Section Two (Identidy the Hazards...) */ drawHeader1(230, 69, 345, "Identify the hazard(s)", 2); //draw right boxes left = 542; top = 104; for (int x = 0; x < mCheckBoxSectionTwo.size(); x++) { float topLoc = top + (x * 20.7f); Take5Data.CheckValue isYes = mCheckBoxSectionTwo.get(x).getCheckValue(); drawBox(left, topLoc, paint, false, isYes); } for (int x = 0; x < mCheckBoxSectionTwo.size(); x++) { float height = 105 + (x * 20.7f); drawText(mCheckBoxSectionTwo.get(x).getHeading(), 238, height + 3, FONT12, carlitoBold); if (x > 0) { paint.setPathEffect(pathEffect); can.drawLine(238, height - 4, 581, height - 4, paint); paint.setPathEffect(null); } } /** * draw section 3,4,5 (including checkboxes) */ drawSmallCircle(8, 331, "Assess the level of risk", 3); drawSmallCircle(202, 331, "Control the hazards", 4); drawSmallCircle(398, 331, "Proceed safely", 5); /** * Draw Page Two */ int xLoc = 7; int yLoc = 420; int height; int width = 565; int RADIUS = 14; float INNER_RADIUS = 13; int centre = yLoc + 7 + RADIUS; width = xLoc + width; int middle = width - 340; can.drawRect(1, 420, 594, 420 + 395, paint); paint.setStyle(Paint.Style.FILL); paint.setTypeface(impact); paint.setTextSize(12); can.drawCircle(xLoc + RADIUS, centre, RADIUS, paint); can.drawRect(xLoc + RADIUS, centre - RADIUS, width, centre + RADIUS, paint); can.drawCircle(width, centre, RADIUS, paint); paint.setColor(Color.WHITE); can.drawCircle(xLoc + RADIUS, centre, INNER_RADIUS, paint); can.drawCircle(width, centre, INNER_RADIUS, paint); can.drawRect(middle, centre - INNER_RADIUS, width, centre + INNER_RADIUS, paint); paint.setColor(Color.BLACK); can.drawCircle(middle, centre, RADIUS, paint); paint.setColor(Color.WHITE); can.drawText("SAFE WORK METHOD STATEMENT (SWMS)", xLoc + 31, centre + 5, paint); paint.setTextSize(16); paint.setColor(Color.BLACK); can.drawText(String.valueOf(4), xLoc + RADIUS - 4, centre + 6, paint); height = 50; drawText("What are the hazards and risks?", 25, yLoc + height, FONT12, carlitoBold); drawText("Risk\nRating", 267, yLoc + 47, FONT12, carlitoBold); drawText("How will hazards and risks be controlled?", 319, yLoc + height, FONT12, carlitoBold); paint.setPathEffect(pathEffect); can.drawLine(262, yLoc + 45, 262, yLoc + 320, paint); can.drawLine(302, yLoc + 45, 302, yLoc + 320, paint); paint.setPathEffect(null); float currentItemHeight = yLoc + 75; float padding = 5; for (int x = 0; x < mRiskElements.size(); x++) { int textHeight = (int) currentItemHeight; float totalItemHeight = drawRiskElement(textHeight, mRiskElements.get(x)); currentItemHeight += totalItemHeight + padding; } paint.setPathEffect(pathEffect); height = yLoc + 350; drawText("Name/s:", 12, height, FONT12, carlitoBold); drawText(mEditTextValues[3], 55, height - 3, FONT14, roboto); paint.setPathEffect(pathEffect); can.drawLine(50, height + 12, 580, height + 12, paint); paint.setPathEffect(null); height = yLoc + 372; drawText("Signatures:", 12, height, FONT12, carlitoBold); drawText("Date:", 468, height, FONT12, carlitoBold); drawText(mDateString, 497, height - 3, FONT14, roboto); paint.setPathEffect(pathEffect); can.drawLine(60, height + 12, 464, height + 12, paint); can.drawLine(492, height + 12, 580, height + 12, paint); paint.setPathEffect(null); // finish the page document.finishPage(page); int imagePageCount = 2; for (Take5RiskElement risk : mRiskElements) { if (risk.imagePath != null) { // crate a page description PdfDocument.PageInfo pageInfo1 = new PdfDocument.PageInfo.Builder(A4_WIDTH, A4_HEIGHT, imagePageCount).create(); PdfDocument.Page imagePage = document.startPage(pageInfo1); Canvas canvas = imagePage.getCanvas(); try { Bitmap original = BitmapFactory.decodeFile(risk.imagePath); Bitmap b = resize(original, canvas.getWidth() - 100, canvas.getHeight() - 100); canvas.drawBitmap(b, 50, 60, new Paint()); // canvas.drawText(risk.getOne(), 50, 40, new Paint()); Path textPath = new Path(); textPath.moveTo(50, 50); textPath.lineTo(canvas.getWidth() - 100, 50); canvas.drawTextOnPath(risk.getOne(), textPath, 0, 0, new Paint()); } catch (Exception e) { e.printStackTrace(); } document.finishPage(imagePage); imagePageCount++; new File(risk.imagePath).delete(); } } // add more pages return document; }
From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java
private void onBufferDraw(Canvas canvas) { if (mKeyboardChanged) { invalidateAllKeys();/*from ww w .j av a 2 s . co m*/ mKeyboardChanged = false; } canvas.getClipBounds(mDirtyRect); if (mKeyboard == null) return; final boolean drawKeyboardNameText = (mKeyboardNameTextSize > 1f) && AnyApplication.getConfig().getShowKeyboardNameText(); final boolean drawHintText = (mHintTextSize > 1) && AnyApplication.getConfig().getShowHintTextOnKeys(); // TODO: calls to AnyApplication.getConfig().getXXXXX() functions are // not yet implemented, // but need to when allowing preferences to override theme settings of // these values // right now just using what should be the default values for these // unimplemented preferences final boolean useCustomKeyTextColor = false; // TODO: final boolean useCustomKeyTextColor = // AnyApplication.getConfig().getUseCustomTextColorOnKeys(); final ColorStateList keyTextColor = useCustomKeyTextColor ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFF6666FF }) : mKeyTextColor; // TODO: ? AnyApplication.getConfig().getCustomKeyTextColorOnKeys() : // mKeyTextColor; final boolean useCustomHintColor = drawHintText && false; // TODO: final boolean useCustomHintColor = drawHintText && // AnyApplication.getConfig().getUseCustomHintColorOnKeys(); final ColorStateList hintColor = useCustomHintColor ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFFFF6666 }) : mHintTextColor; // TODO: ? AnyApplication.getConfig().getCustomHintColorOnKeys() : // mHintTextColor; // allow preferences to override theme settings for hint text position final boolean useCustomHintAlign = drawHintText && AnyApplication.getConfig().getUseCustomHintAlign(); final int hintAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintAlign() : mHintLabelAlign; final int hintVAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintVAlign() : mHintLabelVAlign; final Paint paint = mPaint; final Drawable keyBackground = mKeyBackground; final Rect clipRegion = mClipRegion; final int kbdPaddingLeft = getPaddingLeft(); final int kbdPaddingTop = getPaddingTop(); final Key[] keys = mKeys; final Key invalidKey = mInvalidatedKey; boolean drawSingleKey = false; if (invalidKey != null && canvas.getClipBounds(clipRegion)) { // TODO we should use Rect.inset and Rect.contains here. // Is clipRegion completely contained within the invalidated key? if (invalidKey.x + kbdPaddingLeft - 1 <= clipRegion.left && invalidKey.y + kbdPaddingTop - 1 <= clipRegion.top && invalidKey.x + invalidKey.width + kbdPaddingLeft + 1 >= clipRegion.right && invalidKey.y + invalidKey.height + kbdPaddingTop + 1 >= clipRegion.bottom) { drawSingleKey = true; } } final int keyCount = keys.length; for (int i = 0; i < keyCount; i++) { final AnyKey key = (AnyKey) keys[i]; final boolean keyIsSpace = isSpaceKey(key); if (drawSingleKey && (invalidKey != key)) { continue; } if (!mDirtyRect.intersects(key.x + kbdPaddingLeft, key.y + kbdPaddingTop, key.x + key.width + kbdPaddingLeft, key.y + key.height + kbdPaddingTop)) { continue; } int[] drawableState = key.getCurrentDrawableState(mDrawableStatesProvider); if (keyIsSpace) paint.setColor(mKeyboardNameTextColor.getColorForState(drawableState, 0xFF000000)); else paint.setColor(keyTextColor.getColorForState(drawableState, 0xFF000000)); keyBackground.setState(drawableState); // Switch the character to uppercase if shift is pressed CharSequence label = key.label == null ? null : adjustCase(key).toString(); final Rect bounds = keyBackground.getBounds(); if ((key.width != bounds.right) || (key.height != bounds.bottom)) { keyBackground.setBounds(0, 0, key.width, key.height); } canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop); keyBackground.draw(canvas); if (TextUtils.isEmpty(label)) { Drawable iconToDraw = getIconToDrawForKey(key, false); if (iconToDraw != null/* && shouldDrawIcon */) { //http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getCurrent() //http://stackoverflow.com/a/103600/1324235 final boolean is9Patch = iconToDraw.getCurrent() instanceof NinePatchDrawable; // Special handing for the upper-right number hint icons final int drawableWidth; final int drawableHeight; final int drawableX; final int drawableY; drawableWidth = is9Patch ? key.width : iconToDraw.getIntrinsicWidth(); drawableHeight = is9Patch ? key.height : iconToDraw.getIntrinsicHeight(); drawableX = (key.width + mKeyBackgroundPadding.left - mKeyBackgroundPadding.right - drawableWidth) / 2; drawableY = (key.height + mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom - drawableHeight) / 2; canvas.translate(drawableX, drawableY); iconToDraw.setBounds(0, 0, drawableWidth, drawableHeight); iconToDraw.draw(canvas); canvas.translate(-drawableX, -drawableY); if (keyIsSpace && drawKeyboardNameText) { // now a little hack, I'll set the label now, so it get // drawn. label = mKeyboardName; } } else { // ho... no icon. // I'll try to guess the text label = guessLabelForKey(key.codes[0]); if (TextUtils.isEmpty(label)) { Log.w(TAG, "That's unfortunate, for key " + key.codes[0] + " at (" + key.x + ", " + key.y + ") there is no icon nor label. Action ID is " + mKeyboardActionType); } } } if (label != null) { // For characters, use large font. For labels like "Done", use // small font. final FontMetrics fm; if (keyIsSpace) { paint.setTextSize(mKeyboardNameTextSize); paint.setTypeface(Typeface.DEFAULT_BOLD); if (mKeyboardNameFM == null) mKeyboardNameFM = paint.getFontMetrics(); fm = mKeyboardNameFM; } else if (label.length() > 1 && key.codes.length < 2) { paint.setTextSize(mLabelTextSize); paint.setTypeface(Typeface.DEFAULT_BOLD); if (mLabelFM == null) mLabelFM = paint.getFontMetrics(); fm = mLabelFM; } else { fm = setPaintToKeyText(paint); } final float labelHeight = -fm.top; // Draw a drop shadow for the text paint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, mShadowColor); // (+)This is the trick to get RTL/LTR text correct // no matter what: StaticLayout // this should be in the top left corner of the key float textWidth = paint.measureText(label, 0, label.length()); // I'm going to try something if the key is too small for the // text: // 1) divide the text size by 1.5 // 2) if still too large, divide by 2.5 // 3) show no text if (textWidth > key.width) { Log.d(TAG, "Label '" + label + "' is too large for the key. Reducing by 1.5."); paint.setTextSize(mKeyTextSize / 1.5f); textWidth = paint.measureText(label, 0, label.length()); if (textWidth > key.width) { Log.d(TAG, "Label '" + label + "' is too large for the key. Reducing by 2.5."); paint.setTextSize(mKeyTextSize / 2.5f); textWidth = paint.measureText(label, 0, label.length()); if (textWidth > key.width) { Log.d(TAG, "Label '" + label + "' is too large for the key. Showing no text."); paint.setTextSize(0f); textWidth = paint.measureText(label, 0, label.length()); } } } // the center of the drawable space, which is value used // previously for vertically // positioning the key label final float centerY = mKeyBackgroundPadding.top + ((key.height - mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom) / (keyIsSpace ? 3 : 2));// the label on the space is a bit higher // the X coordinate for the center of the main label text is // unaffected by the hints final float centerX = mKeyBackgroundPadding.left + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2; final float textX = centerX; final float textY; // Some devices (mostly pre-Honeycomb, have issues with RTL text // drawing. // Of course, there is no issue with a single character :) // so, we'll use the RTL secured drawing (via StaticLayout) for // labels. if (label.length() > 1 && !AnyApplication.getConfig().workaround_alwaysUseDrawText()) { // calculate Y coordinate of top of text based on center // location textY = centerY - ((labelHeight - paint.descent()) / 2); canvas.translate(textX, textY); Log.d(TAG, "Using RTL fix for key draw '" + label + "'"); // RTL fix. But it costs, let do it when in need (more than // 1 character) StaticLayout labelText = new StaticLayout(label, new TextPaint(paint), (int) textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); labelText.draw(canvas); } else { // to get Y coordinate of baseline from center of text, // first add half the height (to get to // bottom of text), then subtract the part below the // baseline. Note that fm.top is negative. textY = centerY + ((labelHeight - paint.descent()) / 2); canvas.translate(textX, textY); canvas.drawText(label, 0, label.length(), 0, 0, paint); } canvas.translate(-textX, -textY); // (-) // Turn off drop shadow paint.setShadowLayer(0, 0, 0, 0); } if (drawHintText) { if ((key.popupCharacters != null && key.popupCharacters.length() > 0) || (key.popupResId != 0) || (key.longPressCode != 0)) { Paint.Align oldAlign = paint.getTextAlign(); String hintText = null; if (key.hintLabel != null && key.hintLabel.length() > 0) { hintText = key.hintLabel.toString(); // it is the responsibility of the keyboard layout // designer to ensure that they do // not put too many characters in the hint label... } else if (key.longPressCode != 0) { if (Character.isLetterOrDigit(key.longPressCode)) hintText = Character.toString((char) key.longPressCode); } else if (key.popupCharacters != null) { final String hintString = key.popupCharacters.toString(); final int hintLength = hintString.length(); if (hintLength <= 3) hintText = hintString; } // if hintText is still null, it means it didn't fit one of // the above // cases, so we should provide the hint using the default if (hintText == null) { if (mHintOverflowLabel != null) hintText = mHintOverflowLabel.toString(); else { // theme does not provide a defaultHintLabel // use if hints are above, ... if hints are // below // (to avoid being too close to main label/icon) if (hintVAlign == Gravity.TOP) hintText = ""; else hintText = "..."; } } if (mKeyboard.isShifted()) hintText = hintText.toUpperCase(); // now draw hint paint.setTypeface(Typeface.DEFAULT); paint.setColor(hintColor.getColorForState(drawableState, 0xFF000000)); paint.setTextSize(mHintTextSize); // get the hint text font metrics so that we know the size // of the hint when // we try to position the main label (to try to make sure // they don't overlap) if (mHintTextFM == null) { mHintTextFM = paint.getFontMetrics(); } final float hintX; final float hintY; // the (float) 0.5 value is added or subtracted to just give // a little more room // in case the theme designer didn't account for the hint // label location if (hintAlign == Gravity.LEFT) { // left paint.setTextAlign(Paint.Align.LEFT); hintX = mKeyBackgroundPadding.left + (float) 0.5; } else if (hintAlign == Gravity.CENTER) { // center paint.setTextAlign(Paint.Align.CENTER); hintX = mKeyBackgroundPadding.left + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2; } else { // right paint.setTextAlign(Paint.Align.RIGHT); hintX = key.width - mKeyBackgroundPadding.right - (float) 0.5; } if (hintVAlign == Gravity.TOP) { // above hintY = mKeyBackgroundPadding.top - mHintTextFM.top + (float) 0.5; } else { // below hintY = key.height - mKeyBackgroundPadding.bottom - mHintTextFM.bottom - (float) 0.5; } canvas.drawText(hintText, hintX, hintY, paint); paint.setTextAlign(oldAlign); } } canvas.translate(-key.x - kbdPaddingLeft, -key.y - kbdPaddingTop); } mInvalidatedKey = null; // Overlay a dark rectangle to dim the keyboard if (mMiniKeyboard != null && mMiniKeyboardVisible) { paint.setColor((int) (mBackgroundDimAmount * 0xFF) << 24); canvas.drawRect(0, 0, getWidth(), getHeight(), paint); } if (FeaturesSet.DEBUG_LOG) { if (mShowTouchPoints) { for (PointerTracker tracker : mPointerTrackers) { int startX = tracker.getStartX(); int startY = tracker.getStartY(); int lastX = tracker.getLastX(); int lastY = tracker.getLastY(); paint.setAlpha(128); paint.setColor(0xFFFF0000); canvas.drawCircle(startX, startY, 3, paint); canvas.drawLine(startX, startY, lastX, lastY, paint); paint.setColor(0xFF0000FF); canvas.drawCircle(lastX, lastY, 3, paint); paint.setColor(0xFF00FF00); canvas.drawCircle((startX + lastX) / 2, (startY + lastY) / 2, 2, paint); } } } mDrawPending = false; mDirtyRect.setEmpty(); }
From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardViewBase.java
@CallSuper protected void onBufferDraw(Canvas canvas, final Paint paint) { if (mKeyboardChanged) { invalidateAllKeys();/* w w w. j ava 2 s . c o m*/ mKeyboardChanged = false; } canvas.getClipBounds(mDirtyRect); if (mKeyboard == null) return; final boolean drawKeyboardNameText = (mKeyboardNameTextSize > 1f) && AnyApplication.getConfig().getShowKeyboardNameText(); final boolean drawHintText = (mHintTextSize > 1) && AnyApplication.getConfig().getShowHintTextOnKeys(); final boolean useCustomKeyTextColor = false; // TODO: final boolean useCustomKeyTextColor = // AnyApplication.getConfig().getUseCustomTextColorOnKeys(); final ColorStateList keyTextColor = useCustomKeyTextColor ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFF6666FF }) : mKeyTextColor; final boolean useCustomHintColor = drawHintText && false; // TODO: final boolean useCustomHintColor = drawHintText && final ColorStateList hintColor = useCustomHintColor ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFFFF6666 }) : mHintTextColor; // allow preferences to override theme settings for hint text position final boolean useCustomHintAlign = drawHintText && AnyApplication.getConfig().getUseCustomHintAlign(); final int hintAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintAlign() : mHintLabelAlign; final int hintVAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintVAlign() : mHintLabelVAlign; final Drawable keyBackground = mKeyBackground; final Rect clipRegion = mClipRegion; final int kbdPaddingLeft = getPaddingLeft(); final int kbdPaddingTop = getPaddingTop(); final Key[] keys = mKeys; final Key invalidKey = mInvalidatedKey; boolean drawSingleKey = false; if (invalidKey != null && canvas.getClipBounds(clipRegion)) { // TODO we should use Rect.inset and Rect.contains here. // Is clipRegion completely contained within the invalidated key? if (invalidKey.x + kbdPaddingLeft - 1 <= clipRegion.left && invalidKey.y + kbdPaddingTop - 1 <= clipRegion.top && invalidKey.x + invalidKey.width + kbdPaddingLeft + 1 >= clipRegion.right && invalidKey.y + invalidKey.height + kbdPaddingTop + 1 >= clipRegion.bottom) { drawSingleKey = true; } } for (Key keyBase : keys) { final AnyKey key = (AnyKey) keyBase; final boolean keyIsSpace = isSpaceKey(key); if (drawSingleKey && (invalidKey != key)) { continue; } if (!mDirtyRect.intersects(key.x + kbdPaddingLeft, key.y + kbdPaddingTop, key.x + key.width + kbdPaddingLeft, key.y + key.height + kbdPaddingTop)) { continue; } int[] drawableState = key.getCurrentDrawableState(mDrawableStatesProvider); if (keyIsSpace) paint.setColor(mKeyboardNameTextColor); else paint.setColor(keyTextColor.getColorForState(drawableState, 0xFF000000)); keyBackground.setState(drawableState); // Switch the character to uppercase if shift is pressed CharSequence label = key.label == null ? null : adjustLabelToShiftState(key); final Rect bounds = keyBackground.getBounds(); if ((key.width != bounds.right) || (key.height != bounds.bottom)) { keyBackground.setBounds(0, 0, key.width, key.height); } canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop); keyBackground.draw(canvas); if (TextUtils.isEmpty(label)) { Drawable iconToDraw = getIconToDrawForKey(key, false); if (iconToDraw != null/* && shouldDrawIcon */) { //http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getCurrent() //http://stackoverflow.com/a/103600/1324235 final boolean is9Patch = iconToDraw.getCurrent() instanceof NinePatchDrawable; // Special handing for the upper-right number hint icons final int drawableWidth; final int drawableHeight; final int drawableX; final int drawableY; drawableWidth = is9Patch ? key.width : iconToDraw.getIntrinsicWidth(); drawableHeight = is9Patch ? key.height : iconToDraw.getIntrinsicHeight(); drawableX = (key.width + mKeyBackgroundPadding.left - mKeyBackgroundPadding.right - drawableWidth) / 2; drawableY = (key.height + mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom - drawableHeight) / 2; canvas.translate(drawableX, drawableY); iconToDraw.setBounds(0, 0, drawableWidth, drawableHeight); iconToDraw.draw(canvas); canvas.translate(-drawableX, -drawableY); if (keyIsSpace && drawKeyboardNameText) { // now a little hack, I'll set the label now, so it get // drawn. label = mKeyboardName; } } else { // ho... no icon. // I'll try to guess the text label = guessLabelForKey(key.getPrimaryCode()); } } if (label != null) { // For characters, use large font. For labels like "Done", use // small font. final FontMetrics fm; if (keyIsSpace) { paint.setTextSize(mKeyboardNameTextSize); paint.setTypeface(Typeface.DEFAULT_BOLD); if (mKeyboardNameFontMetrics == null) mKeyboardNameFontMetrics = paint.getFontMetrics(); fm = mKeyboardNameFontMetrics; } else if (label.length() > 1 && key.getCodesCount() < 2) { setPaintForLabelText(paint); if (mLabelFontMetrics == null) mLabelFontMetrics = paint.getFontMetrics(); fm = mLabelFontMetrics; } else { setPaintToKeyText(paint); if (mTextFontMetrics == null) mTextFontMetrics = paint.getFontMetrics(); fm = mTextFontMetrics; } if (isLabelOfPictographic(label)) { paint.setTextSize(2f * paint.getTextSize()); } final float labelHeight = -fm.top; // Draw a drop shadow for the text paint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, mShadowColor); final float textWidth = adjustTextSizeForLabel(paint, label, key.width); // the center of the drawable space, which is value used // previously for vertically // positioning the key label final float centerY = mKeyBackgroundPadding.top + ((key.height - mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom) / (keyIsSpace ? 3 : 2));// the label on the space is a bit higher // the X coordinate for the center of the main label text is // unaffected by the hints final float textX = mKeyBackgroundPadding.left + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2; final float textY; // Some devices (mostly pre-Honeycomb, have issues with RTL text // drawing. // Of course, there is no issue with a single character :) // so, we'll use the RTL secured drawing (via StaticLayout) for // labels. if (label.length() > 1 && !AnyApplication.getConfig().workaround_alwaysUseDrawText()) { // calculate Y coordinate of top of text based on center // location textY = centerY - ((labelHeight - paint.descent()) / 2); canvas.translate(textX, textY); // RTL fix. But it costs, let do it when in need (more than // 1 character) StaticLayout labelText = new StaticLayout(label, new TextPaint(paint), (int) textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); labelText.draw(canvas); } else { // to get Y coordinate of baseline from center of text, // first add half the height (to get to // bottom of text), then subtract the part below the // baseline. Note that fm.top is negative. textY = centerY + ((labelHeight - paint.descent()) / 2); canvas.translate(textX, textY); canvas.drawText(label, 0, label.length(), 0, 0, paint); } canvas.translate(-textX, -textY); // (-) // Turn off drop shadow paint.setShadowLayer(0, 0, 0, 0); } if (drawHintText) { if ((key.popupCharacters != null && key.popupCharacters.length() > 0) || (key.popupResId != 0) || (key.longPressCode != 0)) { Align oldAlign = paint.getTextAlign(); String hintText = null; if (key.hintLabel != null && key.hintLabel.length() > 0) { hintText = key.hintLabel.toString(); // it is the responsibility of the keyboard layout // designer to ensure that they do // not put too many characters in the hint label... } else if (key.longPressCode != 0) { if (Character.isLetterOrDigit(key.longPressCode)) hintText = Character.toString((char) key.longPressCode); } else if (key.popupCharacters != null) { final String hintString = key.popupCharacters.toString(); final int hintLength = hintString.length(); if (hintLength <= 3) hintText = hintString; } // if hintText is still null, it means it didn't fit one of // the above // cases, so we should provide the hint using the default if (hintText == null) { if (mHintOverflowLabel != null) hintText = mHintOverflowLabel; else { // theme does not provide a defaultHintLabel // use if hints are above, ... if hints are // below // (to avoid being too close to main label/icon) if (hintVAlign == Gravity.TOP) hintText = ""; else hintText = "..."; } } if (mKeyboard.isShifted()) hintText = hintText.toUpperCase(getKeyboard().getLocale()); // now draw hint paint.setTypeface(Typeface.DEFAULT); paint.setColor(hintColor.getColorForState(drawableState, 0xFF000000)); paint.setTextSize(mHintTextSize); // get the hint text font metrics so that we know the size // of the hint when // we try to position the main label (to try to make sure // they don't overlap) if (mHintTextFontMetrics == null) { mHintTextFontMetrics = paint.getFontMetrics(); } final float hintX; final float hintY; // the (float) 0.5 value is added or subtracted to just give // a little more room // in case the theme designer didn't account for the hint // label location if (hintAlign == Gravity.START) { // left paint.setTextAlign(Align.LEFT); hintX = mKeyBackgroundPadding.left + 0.5f; } else if (hintAlign == Gravity.CENTER) { // center paint.setTextAlign(Align.CENTER); hintX = mKeyBackgroundPadding.left + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2; } else { // right paint.setTextAlign(Align.RIGHT); hintX = key.width - mKeyBackgroundPadding.right - 0.5f; } if (hintVAlign == Gravity.TOP) { // above hintY = mKeyBackgroundPadding.top - mHintTextFontMetrics.top + 0.5f; } else { // below hintY = key.height - mKeyBackgroundPadding.bottom - mHintTextFontMetrics.bottom - 0.5f; } canvas.drawText(hintText, hintX, hintY, paint); paint.setTextAlign(oldAlign); } } canvas.translate(-key.x - kbdPaddingLeft, -key.y - kbdPaddingTop); } mInvalidatedKey = null; mDirtyRect.setEmpty(); }
From source file:com.yek.keyboard.keyboards.views.AnyKeyboardViewBase.java
@CallSuper protected void onBufferDraw(Canvas canvas, final Paint paint) { if (mKeyboardChanged) { invalidateAllKeys();//from ww w .j a v a 2 s . c om mKeyboardChanged = false; } canvas.getClipBounds(mDirtyRect); if (mKeyboard == null) return; final boolean drawKeyboardNameText = (mKeyboardNameTextSize > 1f) && AnyApplication.getConfig().getShowKeyboardNameText(); final boolean drawHintText = (mHintTextSize > 1) && AnyApplication.getConfig().getShowHintTextOnKeys(); final boolean useCustomKeyTextColor = false; // TODO: final boolean useCustomKeyTextColor = // AnyApplication.getConfig().getUseCustomTextColorOnKeys(); final ColorStateList keyTextColor = useCustomKeyTextColor ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFF6666FF }) : mKeyTextColor; final boolean useCustomHintColor = drawHintText && false; // TODO: final boolean useCustomHintColor = drawHintText && final ColorStateList hintColor = useCustomHintColor ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFFFF6666 }) : mHintTextColor; // allow preferences to override theme settings for hint text position final boolean useCustomHintAlign = drawHintText && AnyApplication.getConfig().getUseCustomHintAlign(); final int hintAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintAlign() : mHintLabelAlign; final int hintVAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintVAlign() : mHintLabelVAlign; final Drawable keyBackground = mKeyBackground; final Rect clipRegion = mClipRegion; final int kbdPaddingLeft = getPaddingLeft(); final int kbdPaddingTop = getPaddingTop(); final Keyboard.Key[] keys = mKeys; final Keyboard.Key invalidKey = mInvalidatedKey; boolean drawSingleKey = false; if (invalidKey != null && canvas.getClipBounds(clipRegion)) { // TODO we should use Rect.inset and Rect.contains here. // Is clipRegion completely contained within the invalidated key? if (invalidKey.x + kbdPaddingLeft - 1 <= clipRegion.left && invalidKey.y + kbdPaddingTop - 1 <= clipRegion.top && invalidKey.x + invalidKey.width + kbdPaddingLeft + 1 >= clipRegion.right && invalidKey.y + invalidKey.height + kbdPaddingTop + 1 >= clipRegion.bottom) { drawSingleKey = true; } } for (Keyboard.Key keyBase : keys) { final AnyKeyboard.AnyKey key = (AnyKeyboard.AnyKey) keyBase; final boolean keyIsSpace = isSpaceKey(key); if (drawSingleKey && (invalidKey != key)) { continue; } if (!mDirtyRect.intersects(key.x + kbdPaddingLeft, key.y + kbdPaddingTop, key.x + key.width + kbdPaddingLeft, key.y + key.height + kbdPaddingTop)) { continue; } int[] drawableState = key.getCurrentDrawableState(mDrawableStatesProvider); if (keyIsSpace) paint.setColor(mKeyboardNameTextColor); else paint.setColor(keyTextColor.getColorForState(drawableState, 0xFF000000)); keyBackground.setState(drawableState); // Switch the character to uppercase if shift is pressed CharSequence label = key.label == null ? null : adjustLabelToShiftState(key); final Rect bounds = keyBackground.getBounds(); if ((key.width != bounds.right) || (key.height != bounds.bottom)) { keyBackground.setBounds(0, 0, key.width, key.height); } canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop); keyBackground.draw(canvas); if (TextUtils.isEmpty(label)) { Drawable iconToDraw = getIconToDrawForKey(key, false); if (iconToDraw != null/* && shouldDrawIcon */) { //http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getCurrent() //http://stackoverflow.com/a/103600/1324235 final boolean is9Patch = iconToDraw.getCurrent() instanceof NinePatchDrawable; // Special handing for the upper-right number hint icons final int drawableWidth; final int drawableHeight; final int drawableX; final int drawableY; drawableWidth = is9Patch ? key.width : iconToDraw.getIntrinsicWidth(); drawableHeight = is9Patch ? key.height : iconToDraw.getIntrinsicHeight(); drawableX = (key.width + mKeyBackgroundPadding.left - mKeyBackgroundPadding.right - drawableWidth) / 2; drawableY = (key.height + mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom - drawableHeight) / 2; canvas.translate(drawableX, drawableY); iconToDraw.setBounds(0, 0, drawableWidth, drawableHeight); iconToDraw.draw(canvas); canvas.translate(-drawableX, -drawableY); if (keyIsSpace && drawKeyboardNameText) { // now a little hack, I'll set the label now, so it get // drawn. label = mKeyboardName; } } else { // ho... no icon. // I'll try to guess the text label = guessLabelForKey(key.getPrimaryCode()); } } if (label != null) { // For characters, use large font. For labels like "Done", use // small font. final FontMetrics fm; if (keyIsSpace) { paint.setTextSize(mKeyboardNameTextSize); paint.setTypeface(Typeface.DEFAULT_BOLD); if (mKeyboardNameFM == null) mKeyboardNameFM = paint.getFontMetrics(); fm = mKeyboardNameFM; } else if (label.length() > 1 && key.getCodesCount() < 2) { setPaintForLabelText(paint); if (mLabelFM == null) mLabelFM = paint.getFontMetrics(); fm = mLabelFM; } else { setPaintToKeyText(paint); if (mTextFM == null) mTextFM = paint.getFontMetrics(); fm = mTextFM; } if (isLabelOfPictographic(label)) { paint.setTextSize(2f * paint.getTextSize()); } final float labelHeight = -fm.top; // Draw a drop shadow for the text paint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, mShadowColor); final float textWidth = adjustTextSizeForLabel(paint, label, key.width); // the center of the drawable space, which is value used // previously for vertically // positioning the key label final float centerY = mKeyBackgroundPadding.top + ((key.height - mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom) / (keyIsSpace ? 3 : 2));// the label on the space is a bit higher // the X coordinate for the center of the main label text is // unaffected by the hints final float textX = mKeyBackgroundPadding.left + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2; final float textY; // Some devices (mostly pre-Honeycomb, have issues with RTL text // drawing. // Of course, there is no issue with a single character :) // so, we'll use the RTL secured drawing (via StaticLayout) for // labels. if (label.length() > 1 && !AnyApplication.getConfig().workaround_alwaysUseDrawText()) { // calculate Y coordinate of top of text based on center // location textY = centerY - ((labelHeight - paint.descent()) / 2); canvas.translate(textX, textY); // RTL fix. But it costs, let do it when in need (more than // 1 character) StaticLayout labelText = new StaticLayout(label, new TextPaint(paint), (int) textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); labelText.draw(canvas); } else { // to get Y coordinate of baseline from center of text, // first add half the height (to get to // bottom of text), then subtract the part below the // baseline. Note that fm.top is negative. textY = centerY + ((labelHeight - paint.descent()) / 2); canvas.translate(textX, textY); canvas.drawText(label, 0, label.length(), 0, 0, paint); } canvas.translate(-textX, -textY); // (-) // Turn off drop shadow paint.setShadowLayer(0, 0, 0, 0); } if (drawHintText) { if ((key.popupCharacters != null && key.popupCharacters.length() > 0) || (key.popupResId != 0) || (key.longPressCode != 0)) { Align oldAlign = paint.getTextAlign(); String hintText = null; if (key.hintLabel != null && key.hintLabel.length() > 0) { hintText = key.hintLabel.toString(); // it is the responsibility of the keyboard layout // designer to ensure that they do // not put too many characters in the hint label... } else if (key.longPressCode != 0) { if (Character.isLetterOrDigit(key.longPressCode)) hintText = Character.toString((char) key.longPressCode); } else if (key.popupCharacters != null) { final String hintString = key.popupCharacters.toString(); final int hintLength = hintString.length(); if (hintLength <= 3) hintText = hintString; } // if hintText is still null, it means it didn't fit one of // the above // cases, so we should provide the hint using the default if (hintText == null) { if (mHintOverflowLabel != null) hintText = mHintOverflowLabel; else { // theme does not provide a defaultHintLabel // use if hints are above, ... if hints are // below // (to avoid being too close to main label/icon) if (hintVAlign == Gravity.TOP) hintText = ""; else hintText = "..."; } } if (mKeyboard.isShifted()) hintText = hintText.toUpperCase(getKeyboard().getLocale()); // now draw hint paint.setTypeface(Typeface.DEFAULT); paint.setColor(hintColor.getColorForState(drawableState, 0xFF000000)); paint.setTextSize(mHintTextSize); // get the hint text font metrics so that we know the size // of the hint when // we try to position the main label (to try to make sure // they don't overlap) if (mHintTextFM == null) { mHintTextFM = paint.getFontMetrics(); } final float hintX; final float hintY; // the (float) 0.5 value is added or subtracted to just give // a little more room // in case the theme designer didn't account for the hint // label location if (hintAlign == Gravity.START) { // left paint.setTextAlign(Align.LEFT); hintX = mKeyBackgroundPadding.left + 0.5f; } else if (hintAlign == Gravity.CENTER) { // center paint.setTextAlign(Align.CENTER); hintX = mKeyBackgroundPadding.left + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2; } else { // right paint.setTextAlign(Align.RIGHT); hintX = key.width - mKeyBackgroundPadding.right - 0.5f; } if (hintVAlign == Gravity.TOP) { // above hintY = mKeyBackgroundPadding.top - mHintTextFM.top + 0.5f; } else { // below hintY = key.height - mKeyBackgroundPadding.bottom - mHintTextFM.bottom - 0.5f; } canvas.drawText(hintText, hintX, hintY, paint); paint.setTextAlign(oldAlign); } } canvas.translate(-key.x - kbdPaddingLeft, -key.y - kbdPaddingTop); } mInvalidatedKey = null; mDirtyRect.setEmpty(); }
From source file:com.codename1.impl.android.AndroidImplementation.java
/** * Loads a native font based on a lookup for a font name and attributes. * Font lookup values can be separated by commas and thus allow fallback if * the primary font isn't supported by the platform. * * @param lookup string describing the font * @return the native font object/*from w w w .j a v a2 s. c o m*/ */ public Object loadNativeFont(String lookup) { try { lookup = lookup.split(";")[0]; int typeface = Typeface.NORMAL; String familyName = lookup.substring(0, lookup.indexOf("-")); String style = lookup.substring(lookup.indexOf("-") + 1, lookup.lastIndexOf("-")); String size = lookup.substring(lookup.lastIndexOf("-") + 1, lookup.length()); if (style.equals("bolditalic")) { typeface = Typeface.BOLD_ITALIC; } else if (style.equals("italic")) { typeface = Typeface.ITALIC; } else if (style.equals("bold")) { typeface = Typeface.BOLD; } Paint font = new CodenameOneTextPaint(Typeface.create(familyName, typeface)); font.setAntiAlias(true); font.setTextSize(Integer.parseInt(size)); return new NativeFont(0, 0, 0, font); } catch (Exception err) { return null; } }
From source file:com.codename1.impl.android.AndroidImplementation.java
@Override public Object createFont(int face, int style, int size) { Typeface typeface = null;/* ww w. j a v a 2 s . co m*/ switch (face) { case Font.FACE_MONOSPACE: typeface = Typeface.MONOSPACE; break; default: typeface = Typeface.DEFAULT; break; } int fontstyle = Typeface.NORMAL; if ((style & Font.STYLE_BOLD) != 0) { fontstyle |= Typeface.BOLD; } if ((style & Font.STYLE_ITALIC) != 0) { fontstyle |= Typeface.ITALIC; } int height = this.defaultFontHeight; int diff = height / 3; switch (size) { case Font.SIZE_SMALL: height -= diff; break; case Font.SIZE_LARGE: height += diff; break; } Paint font = new CodenameOneTextPaint(Typeface.create(typeface, fontstyle)); font.setAntiAlias(true); font.setUnderlineText((style & Font.STYLE_UNDERLINED) != 0); font.setTextSize(height); return new NativeFont(face, style, size, font); }
From source file:com.zoffcc.applications.zanavi.Navit.java
synchronized static int find_max_letters_for_width_and_fontsize(String max_length_text, int width, int max_font_size, int padding_in_dp) { int ret_max_letters = 10; int padding_in_px = 0; if (padding_in_dp > 0) { //System.out.println("aaa2:l:1:x:" + padding_in_dp); padding_in_px = NavitGraphics.dp_to_px(padding_in_dp); //System.out.println("aaa2:l:1:x:2=" + padding_in_dp + " " + padding_in_px + " " + NavitGraphics.dp_to_px(padding_in_dp) + " " + NavitGraphics.Global_dpi_factor_better); }//from ww w .ja va 2s . c o m //System.out.println("aaa2:l:1:x:1=" + padding_in_px); int c = 0; Paint p = new Paint(); p.setTextSize(max_font_size); String s = max_length_text; if ((s == null) || (s.equals(""))) { s = "Mlox3miwoMlox3miwoMlox3miwoMlox3miwoMlox3miwoMlox3miwoMlox3miwoMlox3miwoMlox3miwoMlox3miwo"; } int l = s.length(); ret_max_letters = l; float mt = p.measureText(s); //System.out.println("aaa2:l:1:" + l + " " + s); //System.out.println("aaa2:l:22:." + mt + " padding_in_px=" + padding_in_px + " " + width); while (mt + padding_in_px > width) { //System.out.println("aaa2:l:22:+" + mt + " " + padding_in_px + " " + width); c++; if (c == l) { // no more letters //System.out.println("aaa2:l:1:no more letters:" + l + " " + s); break; } s = s.substring(0, s.length() - 1); mt = p.measureText(s); ret_max_letters--; } //System.out.println("aaa2:l:1:res=" + ret_max_letters); return ret_max_letters; }
From source file:com.zoffcc.applications.zanavi.Navit.java
synchronized static int find_max_font_size_for_width(String sample_text, int width, int max_font_size, int padding_in_dp) { final String s = sample_text; int bh = 0;/*w ww . j a v a 2 s. c om*/ Paint p = new Paint(); Rect bounds = new Rect(); p.setTextSize(max_font_size); p.getTextBounds(s, 0, s.length(), bounds); int ret_font_size = max_font_size; int loop_counter_max = 400; int loop_counter = 0; int padding_in_px = 0; if (padding_in_dp > 0) { padding_in_px = NavitGraphics.dp_to_px(padding_in_dp); } bh = bounds.width(); while ((bh + padding_in_px) > width) { loop_counter++; if (loop_counter > loop_counter_max) { break; } ret_font_size--; p.setTextSize(ret_font_size); p.getTextBounds(s, 0, s.length(), bounds); bh = bounds.width(); // mt = p.measureText(s); } return ret_font_size; }
From source file:com.zoffcc.applications.zanavi.Navit.java
synchronized static int find_max_font_size_for_height(String sample_text, int height, int max_font_size, int padding_in_dp) { String s = sample_text;/*from ww w .jav a2 s . c om*/ int bh = 0; Paint p = new Paint(); Rect bounds = new Rect(); p.setTextSize(max_font_size); // p.measureText(s); p.getTextBounds(s, 0, s.length(), bounds); int ret_font_size = max_font_size; int loop_counter_max = 400; int loop_counter = 0; int padding_in_px = 0; if (padding_in_dp > 0) { padding_in_px = NavitGraphics.dp_to_px(padding_in_dp); } bh = bounds.height(); //System.out.println("bh(1)=" + bh); while ((bh + padding_in_px) > height) { loop_counter++; if (loop_counter > loop_counter_max) { break; } ret_font_size--; p.setTextSize(ret_font_size); // p.measureText(s); p.getTextBounds(s, 0, s.length(), bounds); bh = bounds.height(); } return ret_font_size; }