List of usage examples for android.graphics Typeface DEFAULT
Typeface DEFAULT
To view the source code for android.graphics Typeface DEFAULT.
Click Source Link
From source file:com.android.mail.browse.ConversationItemView.java
private void calculateCoordinates() { startTimer(PERF_TAG_CALCULATE_COORDINATES); sPaint.setTextSize(mCoordinates.dateFontSize); sPaint.setTypeface(Typeface.DEFAULT); final boolean isRtl = ViewUtils.isViewRtl(this); mDateWidth = (int) sPaint.measureText(mHeader.dateText != null ? mHeader.dateText.toString() : ""); if (mHeader.infoIcon != null) { mInfoIconX = (isRtl) ? mCoordinates.infoIconX : mCoordinates.infoIconXRight - mHeader.infoIcon.getWidth(); // If we have an info icon, we start drawing the date text: // At the end of the date TextView minus the width of the date text // In RTL mode, we just use dateX mDateX = (isRtl) ? mCoordinates.dateX : mCoordinates.dateXRight - mDateWidth; } else {/*from ww w. j a va 2 s. co m*/ // If there is no info icon, we start drawing the date text: // At the end of the info icon ImageView minus the width of the date text // We use the info icon ImageView for positioning, since we want the date text to be // at the right, since there is no info icon // In RTL, we just use infoIconX mDateX = (isRtl) ? mCoordinates.infoIconX : mCoordinates.infoIconXRight - mDateWidth; } // The paperclip is drawn starting at the start of the date text minus // the width of the paperclip and the date padding. // In RTL mode, it is at the end of the date (mDateX + mDateWidth) plus the // start date padding. mPaperclipX = (isRtl) ? mDateX + mDateWidth + mCoordinates.datePaddingStart : mDateX - ATTACHMENT.getWidth() - mCoordinates.datePaddingStart; // In normal mode, the senders x and width is based // on where the date/attachment icon start. final int dateAttachmentStart; // Have this end near the paperclip or date, not the folders. if (mHeader.paperclip != null) { // If there is a paperclip, the date/attachment start is at the start // of the paperclip minus the paperclip padding. // In RTL, it is at the end of the paperclip plus the paperclip padding. dateAttachmentStart = (isRtl) ? mPaperclipX + ATTACHMENT.getWidth() + mCoordinates.paperclipPaddingStart : mPaperclipX - mCoordinates.paperclipPaddingStart; } else { // If no paperclip, just use the start of the date minus the date padding start. // In RTL mode, this is just the paperclipX. dateAttachmentStart = (isRtl) ? mPaperclipX : mDateX - mCoordinates.datePaddingStart; } // Senders width is the dateAttachmentStart - sendersX. // In RTL, it is sendersWidth + sendersX - dateAttachmentStart. mSendersWidth = (isRtl) ? mCoordinates.sendersWidth + mCoordinates.sendersX - dateAttachmentStart : dateAttachmentStart - mCoordinates.sendersX; mSendersX = (isRtl) ? dateAttachmentStart : mCoordinates.sendersX; // Second pass to layout each fragment. sPaint.setTextSize(mCoordinates.sendersFontSize); sPaint.setTypeface(Typeface.DEFAULT); // First pass to calculate width of each fragment. if (mSendersWidth < 0) { mSendersWidth = 0; } // sendersDisplayText is only set when preserveSendersText is true. if (mHeader.preserveSendersText) { mHeader.sendersDisplayLayout = new StaticLayout(mHeader.sendersDisplayText, sPaint, mSendersWidth, Alignment.ALIGN_NORMAL, 1, 0, true); } else { final SpannableStringBuilder participantText = elideParticipants(mHeader.styledNames); layoutParticipantText(participantText); } pauseTimer(PERF_TAG_CALCULATE_COORDINATES); }
From source file:com.arlib.floatingsearchview.FloatingSearchView.java
private int getSuggestionItemHeight(SearchSuggestion suggestion) { int leftRightMarginsWidth = Util.dpToPx(124); //todo improve efficiency TextView textView = new TextView(getContext()); textView.setTypeface(Typeface.DEFAULT); textView.setText(suggestion.getBody(), TextView.BufferType.SPANNABLE); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mSuggestionsTextSizePx); int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(mSuggestionsList.getWidth() - leftRightMarginsWidth, View.MeasureSpec.AT_MOST); int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); textView.measure(widthMeasureSpec, heightMeasureSpec); int heightPlusPadding = textView.getMeasuredHeight() + Util.dpToPx(8); int minHeight = Util.dpToPx(48); int height = heightPlusPadding >= minHeight ? heightPlusPadding : minHeight; return heightPlusPadding >= minHeight ? heightPlusPadding : minHeight; }
From source file:com.maskyn.fileeditorpro.activity.MainActivity.java
void aPreferenceValueWasChanged(List<PreferenceChangeType> types) { if (types.contains(PreferenceChangeType.THEME_CHANGE)) { ThemeUtils.setWindowsBackground(this); AccessoryView accessoryView = (AccessoryView) findViewById(R.id.accessoryView); accessoryView.updateTextColors(); }/*from w w w .j a v a2s .c o m*/ if (types.contains(PreferenceChangeType.WRAP_CONTENT)) { if (PreferenceHelper.getWrapContent(this)) { horizontalScroll.removeView(mEditor); verticalScroll.removeView(horizontalScroll); verticalScroll.addView(mEditor); } else { verticalScroll.removeView(mEditor); verticalScroll.addView(horizontalScroll); horizontalScroll.addView(mEditor); } } else if (types.contains(PreferenceChangeType.LINE_NUMERS)) { mEditor.disableTextChangedListener(); mEditor.replaceTextKeepCursor(null); mEditor.enableTextChangedListener(); mEditor.updatePadding(); } else if (types.contains(PreferenceChangeType.SYNTAX)) { mEditor.disableTextChangedListener(); mEditor.replaceTextKeepCursor(mEditor.getText().toString()); mEditor.enableTextChangedListener(); } else if (types.contains(PreferenceChangeType.MONOSPACE)) { if (PreferenceHelper.getUseMonospace(this)) mEditor.setTypeface(Typeface.MONOSPACE); else mEditor.setTypeface(Typeface.DEFAULT); } else if (types.contains(PreferenceChangeType.THEME_CHANGE)) { if (PreferenceHelper.getLightTheme(this)) { mEditor.setTextColor(getResources().getColor(R.color.textColorInverted)); } else { mEditor.setTextColor(getResources().getColor(R.color.textColor)); } } else if (types.contains(PreferenceChangeType.TEXT_SUGGESTIONS) || types.contains(PreferenceChangeType.READ_ONLY)) { if (PreferenceHelper.getReadOnly(this)) { mEditor.setReadOnly(true); } else { mEditor.setReadOnly(false); if (PreferenceHelper.getSuggestionActive(this)) { mEditor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE); } else { mEditor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE); } } // sometimes it becomes monospace after setting the input type if (PreferenceHelper.getUseMonospace(this)) mEditor.setTypeface(Typeface.MONOSPACE); else mEditor.setTypeface(Typeface.DEFAULT); } else if (types.contains(PreferenceChangeType.FONT_SIZE)) { mEditor.updatePadding(); mEditor.setTextSize(PreferenceHelper.getFontSize(this)); } else if (types.contains(PreferenceChangeType.ACCESSORY_VIEW)) { HorizontalScrollView parentAccessoryView = (HorizontalScrollView) findViewById( R.id.parent_accessory_view); ViewUtils.setVisible(parentAccessoryView, PreferenceHelper.getUseAccessoryView(this)); mEditor.updatePadding(); } else if (types.contains(PreferenceChangeType.ENCODING)) { String oldEncoding, newEncoding; oldEncoding = currentEncoding; newEncoding = PreferenceHelper.getEncoding(this); try { final byte[] oldText = mEditor.getText().toString().getBytes(oldEncoding); mEditor.disableTextChangedListener(); mEditor.replaceTextKeepCursor(new String(oldText, newEncoding)); mEditor.enableTextChangedListener(); currentEncoding = newEncoding; } catch (UnsupportedEncodingException ignored) { try { final byte[] oldText = mEditor.getText().toString().getBytes(oldEncoding); mEditor.disableTextChangedListener(); mEditor.replaceTextKeepCursor(new String(oldText, "UTF-16")); mEditor.enableTextChangedListener(); } catch (UnsupportedEncodingException ignored2) { } } } }
From source file:org.cryptsecure.Utility.java
/** * Cut text into one line.//from ww w. j a va 2s. co m * * @param text * the text * @param maxWidth * the max width * @param textSize * the text size * @return the string */ public static String cutTextIntoOneLine(String text, int maxWidth, int textSize) { try { Paint paint = new Paint(); Rect bounds = new Rect(); int textWidth = 0; paint.setTypeface(Typeface.DEFAULT);// your preference here paint.setTextSize(textSize);// have this the same as your text size String outText = text; String outText2 = text; boolean modified = false; boolean cutDown = false; while (true) { if (modified) { paint.getTextBounds(outText2, 0, outText2.length(), bounds); } else { paint.getTextBounds(outText, 0, outText.length(), bounds); } textWidth = bounds.width(); if (textWidth <= maxWidth) { break; } else { modified = true; if (!cutDown) { cutDown = true; int estimatedLen = (outText.length() * maxWidth) / textWidth; estimatedLen += 20; // be carefull! if (estimatedLen > outText.length()) { estimatedLen = outText.length(); } outText = outText.substring(0, estimatedLen); outText2 = outText + "..."; } else { // reduce by one character outText = outText.substring(0, outText.length() - 1); outText2 = outText + "..."; } } } if (modified) { return outText2; } else { return outText; } } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:com.android.mail.browse.ConversationItemView.java
@Override protected void onDraw(Canvas canvas) { if (mCoordinates == null) { LogUtils.e(LOG_TAG, "null coordinates in ConversationItemView#onDraw"); return;/*from w w w . j a va 2 s. com*/ } Utils.traceBeginSection("CIVC.draw"); // Contact photo if (mGadgetMode == ConversationItemViewCoordinates.GADGET_CONTACT_PHOTO) { canvas.save(); Utils.traceBeginSection("draw senders image"); drawSendersImage(canvas); Utils.traceEndSection(); canvas.restore(); } // Senders. boolean isUnread = mHeader.unread; // Old style senders; apply text colors/ sizes/ styling. canvas.save(); if (mHeader.sendersDisplayLayout != null) { sPaint.setTextSize(mCoordinates.sendersFontSize); sPaint.setTypeface(SendersView.getTypeface(isUnread)); sPaint.setColor(sSendersTextColor); canvas.translate(mSendersX, mCoordinates.sendersY + mHeader.sendersDisplayLayout.getTopPadding()); mHeader.sendersDisplayLayout.draw(canvas); } else { drawSenders(canvas); } canvas.restore(); // Subject. sPaint.setTypeface(Typeface.DEFAULT); canvas.save(); drawSubject(canvas); canvas.restore(); canvas.save(); drawSnippet(canvas); canvas.restore(); // Folders. if (mConfig.areFoldersVisible()) { mHeader.folderDisplayer.drawFolders(canvas, mCoordinates, ViewUtils.isViewRtl(this)); } // If this folder has a color (combined view/Email), show it here if (mConfig.isColorBlockVisible()) { sFoldersPaint.setColor(mHeader.conversation.color); sFoldersPaint.setStyle(Paint.Style.FILL); canvas.drawRect(mCoordinates.colorBlockX, mCoordinates.colorBlockY, mCoordinates.colorBlockX + mCoordinates.colorBlockWidth, mCoordinates.colorBlockY + mCoordinates.colorBlockHeight, sFoldersPaint); } // Draw the reply state. Draw nothing if neither replied nor forwarded. if (mConfig.isReplyStateVisible()) { if (mHeader.hasBeenRepliedTo && mHeader.hasBeenForwarded) { canvas.drawBitmap(STATE_REPLIED_AND_FORWARDED, mCoordinates.replyStateX, mCoordinates.replyStateY, null); } else if (mHeader.hasBeenRepliedTo) { canvas.drawBitmap(STATE_REPLIED, mCoordinates.replyStateX, mCoordinates.replyStateY, null); } else if (mHeader.hasBeenForwarded) { canvas.drawBitmap(STATE_FORWARDED, mCoordinates.replyStateX, mCoordinates.replyStateY, null); } else if (mHeader.isInvite) { canvas.drawBitmap(STATE_CALENDAR_INVITE, mCoordinates.replyStateX, mCoordinates.replyStateY, null); } } if (mConfig.isPersonalIndicatorVisible()) { canvas.drawBitmap(mHeader.personalLevelBitmap, mCoordinates.personalIndicatorX, mCoordinates.personalIndicatorY, null); } // Info icon if (mHeader.infoIcon != null) { canvas.drawBitmap(mHeader.infoIcon, mInfoIconX, mCoordinates.infoIconY, sPaint); } // Date. sPaint.setTextSize(mCoordinates.dateFontSize); sPaint.setTypeface(isUnread ? SANS_SERIF_BOLD : SANS_SERIF_LIGHT); sPaint.setColor(isUnread ? sDateTextColorUnread : sDateTextColorRead); drawText(canvas, mHeader.dateText, mDateX, mCoordinates.dateYBaseline, sPaint); // Paper clip icon. if (mHeader.paperclip != null) { canvas.drawBitmap(mHeader.paperclip, mPaperclipX, mCoordinates.paperclipY, sPaint); } // Star. if (mStarEnabled) { canvas.drawBitmap(getStarBitmap(), mCoordinates.starX, mCoordinates.starY, sPaint); } // Divider. if (mDividerEnabled) { final int dividerBottomY = getHeight(); final int dividerTopY = dividerBottomY - sDividerHeight; canvas.drawRect(0, dividerTopY, getWidth(), dividerBottomY, sDividerPaint); } // The focused bar final SwipeableListView listView = getListView(); if (listView != null && listView.isConversationSelected(getConversation())) { final int w = FOCUSED_CONVERSATION_HIGHLIGHT.getIntrinsicWidth(); final boolean isRtl = ViewUtils.isViewRtl(this); // This bar is on the right side of the conv list if it's RTL FOCUSED_CONVERSATION_HIGHLIGHT.setBounds((isRtl) ? getWidth() - w : 0, 0, (isRtl) ? getWidth() : w, getHeight()); FOCUSED_CONVERSATION_HIGHLIGHT.draw(canvas); } Utils.traceEndSection(); }
From source file:com.yek.keyboard.keyboards.views.AnyKeyboardViewBase.java
@CallSuper protected void onBufferDraw(Canvas canvas, final Paint paint) { if (mKeyboardChanged) { invalidateAllKeys();/* w w w. j av a2s .com*/ 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.anysoftkeyboard.keyboards.views.AnyKeyboardViewBase.java
@CallSuper protected void onBufferDraw(Canvas canvas, final Paint paint) { if (mKeyboardChanged) { invalidateAllKeys();//from w w w .j a v a2 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 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:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
private void setupHourTextPaint(Paint p) { p.setColor(mCalendarHourLabelColor); p.setTextSize(HOURS_TEXT_SIZE);/*from w w w . jav a2 s . co m*/ p.setTypeface(Typeface.DEFAULT); p.setTextAlign(Align.RIGHT); p.setAntiAlias(true); }
From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
private void drawDayHeader(String dayStr, int day, String dateNumStr, Canvas canvas, Paint p) { int x;/*from w w w . j a v a 2s . c o m*/ p.setAntiAlias(true); int todayIndex = mTodayJulianDay - mFirstJulianDay; // Draw day of the month if (mNumDays > 1) { float y = DAY_HEADER_HEIGHT - DAY_HEADER_BOTTOM_MARGIN; // Draw day of the month x = computeDayLeftPosition(day + 1) - DAY_HEADER_RIGHT_MARGIN; p.setTextAlign(Align.RIGHT); p.setTextSize(DATE_HEADER_FONT_SIZE); p.setTypeface(todayIndex == day ? mBold : Typeface.DEFAULT); canvas.drawText(dateNumStr, x, y, p); // Draw day of the week x -= p.measureText(" " + dateNumStr); p.setTextSize(DAY_HEADER_FONT_SIZE); p.setTypeface(Typeface.DEFAULT); canvas.drawText(dayStr, x, y, p); } else { float y = DAY_HEADER_HEIGHT - DAY_HEADER_ONE_DAY_BOTTOM_MARGIN; p.setTextAlign(Align.LEFT); // Draw day of the week x = computeDayLeftPosition(day) + DAY_HEADER_ONE_DAY_LEFT_MARGIN; x = x + ((mCellWidth - mDateStrWidthLong) / 2); p.setTextSize(DAY_HEADER_FONT_SIZE); p.setTypeface(Typeface.DEFAULT); canvas.drawText(dayStr, x, y, p); // Draw day of the month x += p.measureText(dayStr) + DAY_HEADER_ONE_DAY_RIGHT_MARGIN; p.setTextSize(DATE_HEADER_FONT_SIZE); p.setTypeface(todayIndex == day ? mBold : Typeface.DEFAULT); canvas.drawText(dateNumStr, x, y, p); } }
From source file:com.tct.mail.browse.ConversationItemView.java
private void calculateCoordinates() { startTimer(PERF_TAG_CALCULATE_COORDINATES); sPaint.setTextSize(mCoordinates.dateFontSize); sPaint.setTypeface(Typeface.DEFAULT); final boolean isRtl = ViewUtils.isViewRtl(this); mDateWidth = (int) sPaint.measureText(mHeader.dateText != null ? mHeader.dateText.toString() : ""); if (mHeader.infoIcon != null) { mInfoIconX = (isRtl) ? mCoordinates.infoIconX : mCoordinates.infoIconXRight - mHeader.infoIcon.getWidth(); // If we have an info icon, we start drawing the date text: // At the end of the date TextView minus the width of the date text // In RTL mode, we just use dateX mDateX = (isRtl) ? mCoordinates.dateX : mCoordinates.dateXRight - mDateWidth; } else {/*ww w.j a v a2 s.co m*/ // If there is no info icon, we start drawing the date text: // At the end of the info icon ImageView minus the width of the date text // We use the info icon ImageView for positioning, since we want the date text to be // at the right, since there is no info icon // In RTL, we just use infoIconX mDateX = (isRtl) ? mCoordinates.infoIconX : mCoordinates.infoIconXRight - mDateWidth; } // TS: zhaotianyong 2014-12-15 EMAIL BUGFIX_859814 MOD_S // The paperclip is drawn starting at the start of the date text minus // the width of the paperclip and the date padding. // In RTL mode, it is at the end of the date (mDateX + mDateWidth) plus the // start date padding. mPaperclipX = (isRtl) ? mDateX + mDateWidth + mCoordinates.datePaddingStart : mDateX - ATTACHMENT.getWidth() - mCoordinates.datePaddingStart; //TS: junwei-xu 2015-06-01 EMAIL BUGFIX_989906 MOD_S Bitmap priorityBitmap = sNormalPriorityIcon; mPriority = mHeader.conversation.flagPriority; if (mPriority == HIGH_PRIORITY) { priorityBitmap = sHighPriorityIcon; } else if (mPriority == LOW_PRIORITY) { priorityBitmap = sLowPriorityIcon; } //[FEATURE]-Add-BEGIN by TSCD.chao zhang,04/17/2014,FR 631895(porting from FR514398) /* if (mHeader.paperclip != null) { mPriorityX = mPaperclipX - priorityBitmap.getWidth(); } else { mPriorityX = mDateX - priorityBitmap.getWidth(); } */ if (mHeader.paperclip != null) { mPriorityX = (isRtl) ? mPaperclipX + ATTACHMENT.getWidth() + mCoordinates.paperclipPaddingStart : mPaperclipX - priorityBitmap.getWidth(); } else { mPriorityX = (isRtl) ? mDateX + mDateWidth + mCoordinates.datePaddingStart : mDateX - priorityBitmap.getWidth(); } //[FEATURE]-Add-END by TSCD.chao zhang //TS: junwei-xu 2015-06-01 EMAIL BUGFIX_989906 MOD_E // TS: zhaotianyong 2014-12-15 EMAIL BUGFIX_859814 MOD_E // In normal mode, the senders x and width is based // on where the date/attachment icon start. // Have this end near the paperclip or date, not the folders. //TS: junwei-xu 2015-06-01 EMAIL BUGFIX_989906 MOD_S if (mHeader.paperclip != null) { // If there is a paperclip, the date/attachment start is at the start // of the paperclip minus the paperclip padding. // In RTL, it is at the end of the paperclip plus the paperclip padding. dateAttachmentStart = (isRtl) ? mPaperclipX + ATTACHMENT.getWidth() + mCoordinates.paperclipPaddingStart + priorityBitmap.getWidth() : mPaperclipX - mCoordinates.paperclipPaddingStart; } else { // If no paperclip, just use the start of the date minus the date padding start. // In RTL mode, this is just the paperclipX. dateAttachmentStart = (isRtl) ? mPaperclipX + priorityBitmap.getWidth() : mDateX - mCoordinates.datePaddingStart; } //TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD_S // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_S // NOTE:use measureText to get the status's length. and we only want draw the status textView when in OUTBOX // that in other box,user can see more subject. int statusWdithWithPanding = 0; if (mDisplayedFolder != null && mDisplayedFolder.isOutbox()) { String status = mHeader.conversation.getMaxLengthStatusDisplay(mContext); mStatusWidth = (int) mStatusTextView.getPaint().measureText(status); statusWdithWithPanding = mStatusWidth + mCoordinates.statusPaddingStart + mCoordinates.statusPanddingEnd; } // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_E // Subject width is the dateAttachmentStart - subjectX. // In RTL, it is subjectWidth + subjectX - dateAttachmentStart. mSubjectWidth = (isRtl) ? mCoordinates.subjectWidth + mCoordinates.subjectX - dateAttachmentStart : mPriorityX - mCoordinates.subjectX - statusWdithWithPanding; mSubjectX = (isRtl) ? dateAttachmentStart : mCoordinates.subjectX; //TS: junwei-xu 2015-06-01 EMAIL BUGFIX_989906 MOD_E // Second pass to layout each fragment. sPaint.setTextSize(mCoordinates.subjectFontSize); sPaint.setTypeface(Typeface.DEFAULT); if (mHeader.styledNames != null) { final SpannableStringBuilder participantText = elideParticipants(mHeader.styledNames); layoutParticipantText(participantText); } else { // First pass to calculate width of each fragment. if (mSubjectWidth < 0) { mSubjectWidth = 0; } mHeader.subjectDisplayLayout = new StaticLayout(mHeader.subjectDisplayText, sPaint, mSubjectWidth, Alignment.ALIGN_NORMAL, 1, 0, true); } if (mSubjectWidth < 0) { mSubjectWidth = 0; } //TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD_E // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_S //NOTE: currently,use calculateStatus after subject created. //calculateStatus(isRtl); // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_E pauseTimer(PERF_TAG_CALCULATE_COORDINATES); }