Search in sources :

Example 76 with TextPaint

use of android.text.TextPaint in project weex-example by KalicyZhou.

the class WXTextDomObject method truncate.

@NonNull
public String truncate(@Nullable String source, @NonNull TextPaint paint, int desired, @Nullable TextUtils.TruncateAt truncateAt) {
    if (!TextUtils.isEmpty(source)) {
        StringBuilder builder;
        Spanned spanned;
        StaticLayout layout;
        for (int i = source.length(); i > 0; i--) {
            builder = new StringBuilder(i + 1);
            builder.append(source, 0, i);
            if (truncateAt != null) {
                builder.append(ELLIPSIS);
            }
            spanned = createSpanned(builder.toString());
            layout = new StaticLayout(spanned, paint, desired, Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
            if (layout.getLineCount() <= 1) {
                return spanned.toString();
            }
        }
    }
    return "";
}
Also used : StaticLayout(android.text.StaticLayout) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) NonNull(android.support.annotation.NonNull)

Example 77 with TextPaint

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

the class MonthWeekEventsView method initView.

/**
     * Sets up the text and style properties for painting. Override this if you
     * want to use a different paint.
     */
@Override
protected void initView() {
    super.initView();
    if (!mInitialized) {
        Resources resources = getContext().getResources();
        mShowDetailsInMonth = Utils.getConfigBool(getContext(), R.bool.show_details_in_month);
        mShowTimeInMonth = Utils.getConfigBool(getContext(), R.bool.show_time_in_month);
        TEXT_SIZE_EVENT_TITLE = resources.getInteger(R.integer.text_size_event_title);
        TEXT_SIZE_MONTH_NUMBER = resources.getInteger(R.integer.text_size_month_number);
        SIDE_PADDING_MONTH_NUMBER = resources.getInteger(R.integer.month_day_number_margin);
        CONFLICT_COLOR = resources.getColor(R.color.month_dna_conflict_time_color);
        EVENT_TEXT_COLOR = resources.getColor(R.color.calendar_event_text_color);
        if (mScale != 1) {
            TOP_PADDING_MONTH_NUMBER *= mScale;
            TOP_PADDING_WEEK_NUMBER *= mScale;
            SIDE_PADDING_MONTH_NUMBER *= mScale;
            SIDE_PADDING_WEEK_NUMBER *= mScale;
            SPACING_WEEK_NUMBER *= mScale;
            TEXT_SIZE_MONTH_NUMBER *= mScale;
            TEXT_SIZE_EVENT *= mScale;
            TEXT_SIZE_EVENT_TITLE *= mScale;
            TEXT_SIZE_MORE_EVENTS *= mScale;
            TEXT_SIZE_MONTH_NAME *= mScale;
            TEXT_SIZE_WEEK_NUM *= mScale;
            DAY_SEPARATOR_OUTER_WIDTH *= mScale;
            DAY_SEPARATOR_INNER_WIDTH *= mScale;
            DAY_SEPARATOR_VERTICAL_LENGTH *= mScale;
            DAY_SEPARATOR_VERTICAL_LENGHT_PORTRAIT *= mScale;
            EVENT_X_OFFSET_LANDSCAPE *= mScale;
            EVENT_Y_OFFSET_LANDSCAPE *= mScale;
            EVENT_Y_OFFSET_PORTRAIT *= mScale;
            EVENT_SQUARE_WIDTH *= mScale;
            EVENT_SQUARE_HEIGHT *= mScale;
            EVENT_SQUARE_BORDER *= mScale;
            EVENT_LINE_PADDING *= mScale;
            EVENT_BOTTOM_PADDING *= mScale;
            EVENT_RIGHT_PADDING *= mScale;
            DNA_MARGIN *= mScale;
            DNA_WIDTH *= mScale;
            DNA_ALL_DAY_HEIGHT *= mScale;
            DNA_MIN_SEGMENT_HEIGHT *= mScale;
            DNA_SIDE_PADDING *= mScale;
            DEFAULT_EDGE_SPACING *= mScale;
            DNA_ALL_DAY_WIDTH *= mScale;
            TODAY_HIGHLIGHT_WIDTH *= mScale;
        }
        if (!mShowDetailsInMonth) {
            TOP_PADDING_MONTH_NUMBER += DNA_ALL_DAY_HEIGHT + DNA_MARGIN;
        }
        mInitialized = true;
    }
    mPadding = DEFAULT_EDGE_SPACING;
    loadColors(getContext());
    // TODO modify paint properties depending on isMini
    mMonthNumPaint = new Paint();
    mMonthNumPaint.setFakeBoldText(false);
    mMonthNumPaint.setAntiAlias(true);
    mMonthNumPaint.setTextSize(TEXT_SIZE_MONTH_NUMBER);
    mMonthNumPaint.setColor(mMonthNumColor);
    mMonthNumPaint.setStyle(Style.FILL);
    mMonthNumPaint.setTextAlign(Align.RIGHT);
    mMonthNumPaint.setTypeface(Typeface.DEFAULT);
    mMonthNumAscentHeight = (int) (-mMonthNumPaint.ascent() + 0.5f);
    mMonthNumHeight = (int) (mMonthNumPaint.descent() - mMonthNumPaint.ascent() + 0.5f);
    mEventPaint = new TextPaint();
    mEventPaint.setFakeBoldText(true);
    mEventPaint.setAntiAlias(true);
    mEventPaint.setTextSize(TEXT_SIZE_EVENT_TITLE);
    mEventPaint.setColor(mMonthEventColor);
    mSolidBackgroundEventPaint = new TextPaint(mEventPaint);
    mSolidBackgroundEventPaint.setColor(EVENT_TEXT_COLOR);
    mFramedEventPaint = new TextPaint(mSolidBackgroundEventPaint);
    mDeclinedEventPaint = new TextPaint();
    mDeclinedEventPaint.setFakeBoldText(true);
    mDeclinedEventPaint.setAntiAlias(true);
    mDeclinedEventPaint.setTextSize(TEXT_SIZE_EVENT_TITLE);
    mDeclinedEventPaint.setColor(mMonthDeclinedEventColor);
    mEventAscentHeight = (int) (-mEventPaint.ascent() + 0.5f);
    mEventHeight = (int) (mEventPaint.descent() - mEventPaint.ascent() + 0.5f);
    mEventExtrasPaint = new TextPaint();
    mEventExtrasPaint.setFakeBoldText(false);
    mEventExtrasPaint.setAntiAlias(true);
    mEventExtrasPaint.setStrokeWidth(EVENT_SQUARE_BORDER);
    mEventExtrasPaint.setTextSize(TEXT_SIZE_EVENT);
    mEventExtrasPaint.setColor(mMonthEventExtraColor);
    mEventExtrasPaint.setStyle(Style.FILL);
    mEventExtrasPaint.setTextAlign(Align.LEFT);
    mExtrasHeight = (int) (mEventExtrasPaint.descent() - mEventExtrasPaint.ascent() + 0.5f);
    mExtrasAscentHeight = (int) (-mEventExtrasPaint.ascent() + 0.5f);
    mExtrasDescent = (int) (mEventExtrasPaint.descent() + 0.5f);
    mEventDeclinedExtrasPaint = new TextPaint();
    mEventDeclinedExtrasPaint.setFakeBoldText(false);
    mEventDeclinedExtrasPaint.setAntiAlias(true);
    mEventDeclinedExtrasPaint.setStrokeWidth(EVENT_SQUARE_BORDER);
    mEventDeclinedExtrasPaint.setTextSize(TEXT_SIZE_EVENT);
    mEventDeclinedExtrasPaint.setColor(mMonthDeclinedExtrasColor);
    mEventDeclinedExtrasPaint.setStyle(Style.FILL);
    mEventDeclinedExtrasPaint.setTextAlign(Align.LEFT);
    mWeekNumPaint = new Paint();
    mWeekNumPaint.setFakeBoldText(false);
    mWeekNumPaint.setAntiAlias(true);
    mWeekNumPaint.setTextSize(TEXT_SIZE_WEEK_NUM);
    mWeekNumPaint.setColor(mWeekNumColor);
    mWeekNumPaint.setStyle(Style.FILL);
    mWeekNumPaint.setTextAlign(Align.RIGHT);
    mWeekNumAscentHeight = (int) (-mWeekNumPaint.ascent() + 0.5f);
    mDNAAllDayPaint = new Paint();
    mDNATimePaint = new Paint();
    mDNATimePaint.setColor(mMonthBusyBitsBusyTimeColor);
    mDNATimePaint.setStyle(Style.FILL_AND_STROKE);
    mDNATimePaint.setStrokeWidth(DNA_WIDTH);
    mDNATimePaint.setAntiAlias(false);
    mDNAAllDayPaint.setColor(mMonthBusyBitsConflictTimeColor);
    mDNAAllDayPaint.setStyle(Style.FILL_AND_STROKE);
    mDNAAllDayPaint.setStrokeWidth(DNA_ALL_DAY_WIDTH);
    mDNAAllDayPaint.setAntiAlias(false);
    mEventSquarePaint = new Paint();
    mEventSquarePaint.setStrokeWidth(EVENT_SQUARE_BORDER);
    mEventSquarePaint.setAntiAlias(false);
    if (DEBUG_LAYOUT) {
        Log.d("EXTRA", "mScale=" + mScale);
        Log.d("EXTRA", "mMonthNumPaint ascent=" + mMonthNumPaint.ascent() + " descent=" + mMonthNumPaint.descent() + " int height=" + mMonthNumHeight);
        Log.d("EXTRA", "mEventPaint ascent=" + mEventPaint.ascent() + " descent=" + mEventPaint.descent() + " int height=" + mEventHeight + " int ascent=" + mEventAscentHeight);
        Log.d("EXTRA", "mEventExtrasPaint ascent=" + mEventExtrasPaint.ascent() + " descent=" + mEventExtrasPaint.descent() + " int height=" + mExtrasHeight);
        Log.d("EXTRA", "mWeekNumPaint ascent=" + mWeekNumPaint.ascent() + " descent=" + mWeekNumPaint.descent());
    }
}
Also used : Resources(android.content.res.Resources) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint)

Example 78 with TextPaint

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

the class SimpleMonthView method drawDays.

/**
     * Draws the month days.
     */
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;
    final int rowHeight = mDayHeight;
    final int colWidth = mCellWidth;
    // Text is vertically centered within the row height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    int rowCenter = headerHeight + rowHeight / 2;
    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (isLayoutRtl()) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }
        int stateMask = 0;
        final boolean isDayEnabled = isDayEnabled(day);
        if (isDayEnabled) {
            stateMask |= StateSet.VIEW_STATE_ENABLED;
        }
        final boolean isDayActivated = mActivatedDay == day;
        final boolean isDayHighlighted = mHighlightedDay == day;
        if (isDayActivated) {
            stateMask |= StateSet.VIEW_STATE_ACTIVATED;
            // Adjust the circle to be centered on the row.
            final Paint paint = isDayHighlighted ? mDayHighlightSelectorPaint : mDaySelectorPaint;
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, paint);
        } else if (isDayHighlighted) {
            stateMask |= StateSet.VIEW_STATE_PRESSED;
            if (isDayEnabled) {
                // Adjust the circle to be centered on the row.
                canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDayHighlightPaint);
            }
        }
        final boolean isDayToday = mToday == day;
        final int dayTextColor;
        if (isDayToday && !isDayActivated) {
            dayTextColor = mDaySelectorPaint.getColor();
        } else {
            final int[] stateSet = StateSet.get(stateMask);
            dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
        }
        p.setColor(dayTextColor);
        canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p);
        col++;
        if (col == DAYS_IN_WEEK) {
            col = 0;
            rowCenter += rowHeight;
        }
    }
}
Also used : TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint)

Example 79 with TextPaint

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

the class SimpleMonthView method findClosestRow.

/**
     * Returns the row (0 indexed) closest to previouslyFocusedRect or center if null.
     */
private int findClosestRow(@Nullable Rect previouslyFocusedRect) {
    if (previouslyFocusedRect == null) {
        return 3;
    } else {
        int centerY = previouslyFocusedRect.centerY();
        final TextPaint p = mDayPaint;
        final int headerHeight = mMonthHeight + mDayOfWeekHeight;
        final int rowHeight = mDayHeight;
        // Text is vertically centered within the row height.
        final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
        final int rowCenter = headerHeight + rowHeight / 2;
        centerY -= rowCenter - halfLineHeight;
        int row = Math.round(centerY / (float) rowHeight);
        final int maxDay = findDayOffset() + mDaysInMonth;
        final int maxRows = (maxDay / DAYS_IN_WEEK) - ((maxDay % DAYS_IN_WEEK == 0) ? 1 : 0);
        row = MathUtils.constrain(row, 0, maxRows);
        return row;
    }
}
Also used : TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint)

Example 80 with TextPaint

use of android.text.TextPaint in project materialistic by hidroh.

the class HackerNewsItem method createAuthorSpannable.

@NonNull
private SpannableString createAuthorSpannable(boolean authorLink) {
    SpannableString bySpannable = new SpannableString(AUTHOR_SEPARATOR + by);
    if (!authorLink) {
        return bySpannable;
    }
    bySpannable.setSpan(new StyleSpan(Typeface.BOLD), AUTHOR_SEPARATOR.length(), bySpannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    ClickableSpan clickableSpan = new ClickableSpan() {

        @Override
        public void onClick(View view) {
            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW).setData(AppUtils.createUserUri(getBy())));
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    };
    bySpannable.setSpan(clickableSpan, AUTHOR_SEPARATOR.length(), bySpannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    return bySpannable;
}
Also used : SpannableString(android.text.SpannableString) StyleSpan(android.text.style.StyleSpan) Intent(android.content.Intent) ClickableSpan(android.text.style.ClickableSpan) View(android.view.View) TextPaint(android.text.TextPaint) NonNull(android.support.annotation.NonNull)

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