Search in sources :

Example 71 with TextPaint

use of android.text.TextPaint in project Etar-Calendar by Etar-Group.

the class MonthWeekEventsView method drawEvent.

/**
     * Attempts to draw the given event. Returns the y for the next event or the
     * original y if the event will not fit. An event is considered to not fit
     * if the event and its extras won't fit or if there are more events and the
     * more events line would not fit after drawing this event.
     *
     * @param canvas the canvas to draw on
     * @param event the event to draw
     * @param x the top left corner for this event's color chip
     * @param y the top left corner for this event's color chip
     * @param rightEdge the rightmost point we're allowed to draw on (exclusive)
     * @param moreEvents indicates whether additional events will follow this one
     * @param showTimes if set, a second line with a time range will be displayed for non-all-day
     *   events
     * @param doDraw if set, do the actual drawing; otherwise this just computes the height
     *   and returns
     * @return the y for the next event or the original y if it won't fit
     */
protected int drawEvent(Canvas canvas, Event event, int x, int y, int rightEdge, boolean moreEvents, boolean showTimes, boolean doDraw) {
    /*
         * Vertical layout:
         *   (top of box)
         * a. EVENT_Y_OFFSET_LANDSCAPE or portrait equivalent
         * b. Event title: mEventHeight for a normal event, + 2xBORDER_SPACE for all-day event
         * c. [optional] Time range (mExtrasHeight)
         * d. EVENT_LINE_PADDING
         *
         * Repeat (b,c,d) as needed and space allows.  If we have more events than fit, we need
         * to leave room for something like "+2" at the bottom:
         *
         * e. "+ more" line (mExtrasHeight)
         *
         * f. EVENT_BOTTOM_PADDING (overlaps EVENT_LINE_PADDING)
         *   (bottom of box)
         */
    // want a 1-pixel gap inside border
    final int BORDER_SPACE = EVENT_SQUARE_BORDER + 1;
    // adjust bounds for stroke width
    final int STROKE_WIDTH_ADJ = EVENT_SQUARE_BORDER / 2;
    boolean allDay = event.allDay;
    int eventRequiredSpace = mEventHeight;
    if (allDay) {
        // Add a few pixels for the box we draw around all-day events.
        eventRequiredSpace += BORDER_SPACE * 2;
    } else if (showTimes) {
        // Need room for the "1pm - 2pm" line.
        eventRequiredSpace += mExtrasHeight;
    }
    // leave a bit of room at the bottom
    int reservedSpace = EVENT_BOTTOM_PADDING;
    if (moreEvents) {
        // More events follow.  Leave a bit of space between events.
        eventRequiredSpace += EVENT_LINE_PADDING;
        // Make sure we have room for the "+ more" line.  (The "+ more" line is expected
        // to be <= the height of an event line, so we won't show "+1" when we could be
        // showing the event.)
        reservedSpace += mExtrasHeight;
    }
    if (y + eventRequiredSpace + reservedSpace > mHeight) {
        // Not enough space, return original y
        return y;
    } else if (!doDraw) {
        return y + eventRequiredSpace;
    }
    boolean isDeclined = event.selfAttendeeStatus == Attendees.ATTENDEE_STATUS_DECLINED;
    int color = event.color;
    if (isDeclined) {
        color = Utils.getDeclinedColorFromColor(color);
    }
    int textX, textY, textRightEdge;
    if (allDay) {
        // We shift the render offset "inward", because drawRect with a stroke width greater
        // than 1 draws outside the specified bounds.  (We don't adjust the left edge, since
        // we want to match the existing appearance of the "event square".)
        r.left = x;
        r.right = rightEdge - STROKE_WIDTH_ADJ;
        r.top = y + STROKE_WIDTH_ADJ;
        r.bottom = y + mEventHeight + BORDER_SPACE * 2 - STROKE_WIDTH_ADJ;
        textX = x + BORDER_SPACE;
        textY = y + mEventAscentHeight + BORDER_SPACE;
        textRightEdge = rightEdge - BORDER_SPACE;
    } else {
        r.left = x;
        r.right = x + EVENT_SQUARE_WIDTH;
        r.bottom = y + mEventAscentHeight;
        r.top = r.bottom - EVENT_SQUARE_HEIGHT;
        textX = x + EVENT_SQUARE_WIDTH + EVENT_RIGHT_PADDING;
        textY = y + mEventAscentHeight;
        textRightEdge = rightEdge;
    }
    Style boxStyle = Style.STROKE;
    boolean solidBackground = false;
    if (event.selfAttendeeStatus != Attendees.ATTENDEE_STATUS_INVITED) {
        boxStyle = Style.FILL_AND_STROKE;
        if (allDay) {
            solidBackground = true;
        }
    }
    mEventSquarePaint.setStyle(boxStyle);
    mEventSquarePaint.setColor(color);
    canvas.drawRect(r, mEventSquarePaint);
    float avail = textRightEdge - textX;
    CharSequence text = TextUtils.ellipsize(event.title, mEventPaint, avail, TextUtils.TruncateAt.END);
    Paint textPaint;
    if (solidBackground) {
        // Text color needs to contrast with solid background.
        textPaint = mSolidBackgroundEventPaint;
    } else if (isDeclined) {
        // Use "declined event" color.
        textPaint = mDeclinedEventPaint;
    } else if (allDay) {
        // Text inside frame is same color as frame.
        mFramedEventPaint.setColor(color);
        textPaint = mFramedEventPaint;
    } else {
        // Use generic event text color.
        textPaint = mEventPaint;
    }
    canvas.drawText(text.toString(), textX, textY, textPaint);
    y += mEventHeight;
    if (allDay) {
        y += BORDER_SPACE * 2;
    }
    if (showTimes && !allDay) {
        // show start/end time, e.g. "1pm - 2pm"
        textY = y + mExtrasAscentHeight;
        mStringBuilder.setLength(0);
        text = DateUtils.formatDateRange(getContext(), mFormatter, event.startMillis, event.endMillis, DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, Utils.getTimeZone(getContext(), null)).toString();
        text = TextUtils.ellipsize(text, mEventExtrasPaint, avail, TextUtils.TruncateAt.END);
        canvas.drawText(text.toString(), textX, textY, isDeclined ? mEventDeclinedExtrasPaint : mEventExtrasPaint);
        y += mExtrasHeight;
    }
    y += EVENT_LINE_PADDING;
    return y;
}
Also used : Style(android.graphics.Paint.Style) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 72 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by DirtyUnicorns.

the class BiDiTestViewDrawText method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    final int width = getWidth();
    final int height = getHeight();
    final TextPaint paint = new TextPaint();
    paint.setTextSize(mSize);
    paint.setColor(mColor);
    paint.setTextAlign(Align.CENTER);
    canvas.drawText(mText, width / 2, height * 2 / 3, paint);
}
Also used : TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint)

Example 73 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by DirtyUnicorns.

the class StaticLayoutTest method moveCursorToLeftCursorableOffset.

private void moveCursorToLeftCursorableOffset(EditorState state, TextPaint paint) {
    assertEquals("The editor has selection", state.mSelectionStart, state.mSelectionEnd);
    final Layout layout = builder().setText(state.mText.toString()).setPaint(paint).build();
    final int newOffset = layout.getOffsetToLeftOf(state.mSelectionStart);
    state.mSelectionStart = state.mSelectionEnd = newOffset;
}
Also used : TextPaint(android.text.TextPaint)

Example 74 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by DirtyUnicorns.

the class StaticLayoutTest method testEmojiOffset.

/**
     * Tests for keycap, variation selectors, flags are in CTS.
     * See {@link android.text.cts.StaticLayoutTest}.
     */
public void testEmojiOffset() {
    EditorState state = new EditorState();
    TextPaint paint = new TextPaint();
    // Odd numbered regional indicator symbols.
    // U+1F1E6 is REGIONAL INDICATOR SYMBOL LETTER A, U+1F1E8 is REGIONAL INDICATOR SYMBOL
    // LETTER C.
    state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 | U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 | U+1F1E6");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6 |");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6 |");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 | U+1F1E6");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 | U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToLeftCursorableOffset(state, paint);
    // Zero width sequence
    final String zwjSequence = "U+1F468 U+200D U+2764 U+FE0F U+200D U+1F468";
    state.setByString("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
    moveCursorToRightCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " | " + zwjSequence + " " + zwjSequence);
    moveCursorToRightCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " " + zwjSequence + " | " + zwjSequence);
    moveCursorToRightCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " " + zwjSequence + " " + zwjSequence + " |");
    moveCursorToRightCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " " + zwjSequence + " " + zwjSequence + " |");
    moveCursorToLeftCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " " + zwjSequence + " | " + zwjSequence);
    moveCursorToLeftCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " | " + zwjSequence + " " + zwjSequence);
    moveCursorToLeftCursorableOffset(state, paint);
    state.assertEquals("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
    moveCursorToLeftCursorableOffset(state, paint);
    state.assertEquals("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
    moveCursorToLeftCursorableOffset(state, paint);
    // Emoji modifiers
    // U+261D is WHITE UP POINTING INDEX, U+1F3FB is EMOJI MODIFIER FITZPATRICK TYPE-1-2.
    state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB | U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB U+261D U+1F3FB | U+261D U+1F3FB");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB |");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB |");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB U+261D U+1F3FB | U+261D U+1F3FB");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB | U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToLeftCursorableOffset(state, paint);
}
Also used : EditorState(android.text.method.EditorState) TextPaint(android.text.TextPaint)

Example 75 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by DirtyUnicorns.

the class TextLayoutTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    mString = "The quick brown fox";
    mPaint = new TextPaint();
}
Also used : TextPaint(android.text.TextPaint)

Aggregations

TextPaint (android.text.TextPaint)216 Paint (android.graphics.Paint)86 StaticLayout (android.text.StaticLayout)29 View (android.view.View)17 Bitmap (android.graphics.Bitmap)14 Typeface (android.graphics.Typeface)14 TypedArray (android.content.res.TypedArray)12 SpannableString (android.text.SpannableString)12 RectF (android.graphics.RectF)11 Spanned (android.text.Spanned)11 TextView (android.widget.TextView)11 Rect (android.graphics.Rect)8 StyleSpan (android.text.style.StyleSpan)8 ClickableSpan (android.text.style.ClickableSpan)7 PackageManager (android.content.pm.PackageManager)6 Canvas (android.graphics.Canvas)6 IBinder (android.os.IBinder)6 SpannableStringBuilder (android.text.SpannableStringBuilder)6 Context (android.content.Context)5 Resources (android.content.res.Resources)5