Search in sources :

Example 91 with Paint

use of android.graphics.Paint in project WordPress-Android by wordpress-mobile.

the class RippleToggleButton method init.

private void init() {
    if (isInEditMode()) {
        return;
    }
    int rippleColor = getResources().getColor(R.color.format_bar_ripple_animation);
    mFillPaint = new Paint();
    mFillPaint.setAntiAlias(true);
    mFillPaint.setColor(rippleColor);
    mFillPaint.setStyle(Paint.Style.FILL);
    mFillPaint.setAlpha(FILL_INITIAL_OPACITY);
    mStrokePaint = new Paint();
    mStrokePaint.setAntiAlias(true);
    mStrokePaint.setColor(rippleColor);
    mStrokePaint.setStyle(Paint.Style.STROKE);
    mStrokePaint.setStrokeWidth(2);
    mStrokePaint.setAlpha(STROKE_INITIAL_OPACITY);
    setWillNotDraw(false);
}
Also used : Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 92 with Paint

use of android.graphics.Paint in project XobotOS by xamarin.

the class StaticLayout method generate.

/* package */
void generate(CharSequence source, int bufStart, int bufEnd, TextPaint paint, int outerWidth, Alignment align, TextDirectionHeuristic textDir, float spacingmult, float spacingadd, boolean includepad, boolean trackpad, float ellipsizedWidth, TextUtils.TruncateAt ellipsize) {
    mLineCount = 0;
    int v = 0;
    boolean needMultiply = (spacingmult != 1 || spacingadd != 0);
    Paint.FontMetricsInt fm = mFontMetricsInt;
    int[] chooseHtv = null;
    MeasuredText measured = mMeasured;
    Spanned spanned = null;
    if (source instanceof Spanned)
        spanned = (Spanned) source;
    // XXX
    int DEFAULT_DIR = DIR_LEFT_TO_RIGHT;
    int paraEnd;
    for (int paraStart = bufStart; paraStart <= bufEnd; paraStart = paraEnd) {
        paraEnd = TextUtils.indexOf(source, CHAR_NEW_LINE, paraStart, bufEnd);
        if (paraEnd < 0)
            paraEnd = bufEnd;
        else
            paraEnd++;
        int firstWidthLineLimit = mLineCount + 1;
        int firstWidth = outerWidth;
        int restWidth = outerWidth;
        LineHeightSpan[] chooseHt = null;
        if (spanned != null) {
            LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd, LeadingMarginSpan.class);
            for (int i = 0; i < sp.length; i++) {
                LeadingMarginSpan lms = sp[i];
                firstWidth -= sp[i].getLeadingMargin(true);
                restWidth -= sp[i].getLeadingMargin(false);
                // paragraph.
                if (lms instanceof LeadingMarginSpan2) {
                    LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms;
                    int lmsFirstLine = getLineForOffset(spanned.getSpanStart(lms2));
                    firstWidthLineLimit = lmsFirstLine + lms2.getLeadingMarginLineCount();
                }
            }
            chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class);
            if (chooseHt.length != 0) {
                if (chooseHtv == null || chooseHtv.length < chooseHt.length) {
                    chooseHtv = new int[ArrayUtils.idealIntArraySize(chooseHt.length)];
                }
                for (int i = 0; i < chooseHt.length; i++) {
                    int o = spanned.getSpanStart(chooseHt[i]);
                    if (o < paraStart) {
                        // starts in this layout, before the
                        // current paragraph
                        chooseHtv[i] = getLineTop(getLineForOffset(o));
                    } else {
                        // starts in this paragraph
                        chooseHtv[i] = v;
                    }
                }
            }
        }
        measured.setPara(source, paraStart, paraEnd, textDir);
        char[] chs = measured.mChars;
        float[] widths = measured.mWidths;
        byte[] chdirs = measured.mLevels;
        int dir = measured.mDir;
        boolean easy = measured.mEasy;
        int width = firstWidth;
        float w = 0;
        int here = paraStart;
        int ok = paraStart;
        float okWidth = w;
        int okAscent = 0, okDescent = 0, okTop = 0, okBottom = 0;
        int fit = paraStart;
        float fitWidth = w;
        int fitAscent = 0, fitDescent = 0, fitTop = 0, fitBottom = 0;
        boolean hasTabOrEmoji = false;
        boolean hasTab = false;
        TabStops tabStops = null;
        for (int spanStart = paraStart, spanEnd = spanStart, nextSpanStart; spanStart < paraEnd; spanStart = nextSpanStart) {
            if (spanStart == spanEnd) {
                if (spanned == null)
                    spanEnd = paraEnd;
                else
                    spanEnd = spanned.nextSpanTransition(spanStart, paraEnd, MetricAffectingSpan.class);
                int spanLen = spanEnd - spanStart;
                if (spanned == null) {
                    measured.addStyleRun(paint, spanLen, fm);
                } else {
                    MetricAffectingSpan[] spans = spanned.getSpans(spanStart, spanEnd, MetricAffectingSpan.class);
                    spans = TextUtils.removeEmptySpans(spans, spanned, MetricAffectingSpan.class);
                    measured.addStyleRun(paint, spans, spanLen, fm);
                }
            }
            nextSpanStart = spanEnd;
            int fmTop = fm.top;
            int fmBottom = fm.bottom;
            int fmAscent = fm.ascent;
            int fmDescent = fm.descent;
            for (int j = spanStart; j < spanEnd; j++) {
                char c = chs[j - paraStart];
                if (c == CHAR_NEW_LINE) {
                // intentionally left empty
                } else if (c == CHAR_TAB) {
                    if (hasTab == false) {
                        hasTab = true;
                        hasTabOrEmoji = true;
                        if (spanned != null) {
                            // First tab this para, check for tabstops
                            TabStopSpan[] spans = getParagraphSpans(spanned, paraStart, paraEnd, TabStopSpan.class);
                            if (spans.length > 0) {
                                tabStops = new TabStops(TAB_INCREMENT, spans);
                            }
                        }
                    }
                    if (tabStops != null) {
                        w = tabStops.nextTab(w);
                    } else {
                        w = TabStops.nextDefaultStop(w, TAB_INCREMENT);
                    }
                } else if (c >= CHAR_FIRST_HIGH_SURROGATE && c <= CHAR_LAST_LOW_SURROGATE && j + 1 < spanEnd) {
                    int emoji = Character.codePointAt(chs, j - paraStart);
                    if (emoji >= MIN_EMOJI && emoji <= MAX_EMOJI) {
                        Bitmap bm = EMOJI_FACTORY.getBitmapFromAndroidPua(emoji);
                        if (bm != null) {
                            Paint whichPaint;
                            if (spanned == null) {
                                whichPaint = paint;
                            } else {
                                whichPaint = mWorkPaint;
                            }
                            float wid = bm.getWidth() * -whichPaint.ascent() / bm.getHeight();
                            w += wid;
                            hasTabOrEmoji = true;
                            j++;
                        } else {
                            w += widths[j - paraStart];
                        }
                    } else {
                        w += widths[j - paraStart];
                    }
                } else {
                    w += widths[j - paraStart];
                }
                if (w <= width) {
                    fitWidth = w;
                    fit = j + 1;
                    if (fmTop < fitTop)
                        fitTop = fmTop;
                    if (fmAscent < fitAscent)
                        fitAscent = fmAscent;
                    if (fmDescent > fitDescent)
                        fitDescent = fmDescent;
                    if (fmBottom > fitBottom)
                        fitBottom = fmBottom;
                    if (c == CHAR_SPACE || c == CHAR_TAB || ((c == CHAR_DOT || c == CHAR_COMMA || c == CHAR_COLON || c == CHAR_SEMICOLON) && (j - 1 < here || !Character.isDigit(chs[j - 1 - paraStart])) && (j + 1 >= spanEnd || !Character.isDigit(chs[j + 1 - paraStart]))) || ((c == CHAR_SLASH || c == CHAR_HYPHEN) && (j + 1 >= spanEnd || !Character.isDigit(chs[j + 1 - paraStart]))) || (c >= CHAR_FIRST_CJK && isIdeographic(c, true) && j + 1 < spanEnd && isIdeographic(chs[j + 1 - paraStart], false))) {
                        okWidth = w;
                        ok = j + 1;
                        if (fitTop < okTop)
                            okTop = fitTop;
                        if (fitAscent < okAscent)
                            okAscent = fitAscent;
                        if (fitDescent > okDescent)
                            okDescent = fitDescent;
                        if (fitBottom > okBottom)
                            okBottom = fitBottom;
                    }
                } else {
                    final boolean moreChars = (j + 1 < spanEnd);
                    if (ok != here) {
                        while (ok < spanEnd && chs[ok - paraStart] == CHAR_SPACE) {
                            ok++;
                        }
                        v = out(source, here, ok, okAscent, okDescent, okTop, okBottom, v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, hasTabOrEmoji, needMultiply, paraStart, chdirs, dir, easy, ok == bufEnd, includepad, trackpad, chs, widths, paraStart, ellipsize, ellipsizedWidth, okWidth, paint, moreChars);
                        here = ok;
                    } else if (fit != here) {
                        // Log.e("text", "output fit " + here + " to " +fit);
                        v = out(source, here, fit, fitAscent, fitDescent, fitTop, fitBottom, v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, hasTabOrEmoji, needMultiply, paraStart, chdirs, dir, easy, fit == bufEnd, includepad, trackpad, chs, widths, paraStart, ellipsize, ellipsizedWidth, fitWidth, paint, moreChars);
                        here = fit;
                    } else {
                        // Log.e("text", "output one " + here + " to " +(here + 1));
                        // XXX not sure why the existing fm wasn't ok.
                        // measureText(paint, mWorkPaint,
                        //             source, here, here + 1, fm, tab,
                        //             null);
                        v = out(source, here, here + 1, fm.ascent, fm.descent, fm.top, fm.bottom, v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, hasTabOrEmoji, needMultiply, paraStart, chdirs, dir, easy, here + 1 == bufEnd, includepad, trackpad, chs, widths, paraStart, ellipsize, ellipsizedWidth, widths[here - paraStart], paint, moreChars);
                        here = here + 1;
                    }
                    if (here < spanStart) {
                        // didn't output all the text for this span
                        // we've measured the raw widths, though, so
                        // just reset the start point
                        j = nextSpanStart = here;
                    } else {
                        // continue looping
                        j = here - 1;
                    }
                    ok = fit = here;
                    w = 0;
                    fitAscent = fitDescent = fitTop = fitBottom = 0;
                    okAscent = okDescent = okTop = okBottom = 0;
                    if (--firstWidthLineLimit <= 0) {
                        width = restWidth;
                    }
                }
                if (mLineCount >= mMaximumVisibleLineCount) {
                    break;
                }
            }
        }
        if (paraEnd != here && mLineCount < mMaximumVisibleLineCount) {
            if ((fitTop | fitBottom | fitDescent | fitAscent) == 0) {
                paint.getFontMetricsInt(fm);
                fitTop = fm.top;
                fitBottom = fm.bottom;
                fitAscent = fm.ascent;
                fitDescent = fm.descent;
            }
            // Log.e("text", "output rest " + here + " to " + end);
            v = out(source, here, paraEnd, fitAscent, fitDescent, fitTop, fitBottom, v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, hasTabOrEmoji, needMultiply, paraStart, chdirs, dir, easy, paraEnd == bufEnd, includepad, trackpad, chs, widths, paraStart, ellipsize, ellipsizedWidth, w, paint, paraEnd != bufEnd);
        }
        paraStart = paraEnd;
        if (paraEnd == bufEnd)
            break;
    }
    if ((bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) && mLineCount < mMaximumVisibleLineCount) {
        // Log.e("text", "output last " + bufEnd);
        paint.getFontMetricsInt(fm);
        v = out(source, bufEnd, bufEnd, fm.ascent, fm.descent, fm.top, fm.bottom, v, spacingmult, spacingadd, null, null, fm, false, needMultiply, bufEnd, null, DEFAULT_DIR, true, true, includepad, trackpad, null, null, bufStart, ellipsize, ellipsizedWidth, 0, paint, false);
    }
}
Also used : TabStopSpan(android.text.style.TabStopSpan) LineHeightSpan(android.text.style.LineHeightSpan) Paint(android.graphics.Paint) Paint(android.graphics.Paint) LeadingMarginSpan2(android.text.style.LeadingMarginSpan.LeadingMarginSpan2) Bitmap(android.graphics.Bitmap) LeadingMarginSpan(android.text.style.LeadingMarginSpan) MetricAffectingSpan(android.text.style.MetricAffectingSpan)

Example 93 with Paint

use of android.graphics.Paint in project XobotOS by xamarin.

the class TextLine method set.

/**
     * Initializes a TextLine and prepares it for use.
     *
     * @param paint the base paint for the line
     * @param text the text, can be Styled
     * @param start the start of the line relative to the text
     * @param limit the limit of the line relative to the text
     * @param dir the paragraph direction of this line
     * @param directions the directions information of this line
     * @param hasTabs true if the line might contain tabs or emoji
     * @param tabStops the tabStops. Can be null.
     */
void set(TextPaint paint, CharSequence text, int start, int limit, int dir, Directions directions, boolean hasTabs, TabStops tabStops) {
    mPaint = paint;
    mText = text;
    mStart = start;
    mLen = limit - start;
    mDir = dir;
    mDirections = directions;
    if (mDirections == null) {
        throw new IllegalArgumentException("Directions cannot be null");
    }
    mHasTabs = hasTabs;
    mSpanned = null;
    boolean hasReplacement = false;
    if (text instanceof Spanned) {
        mSpanned = (Spanned) text;
        ReplacementSpan[] spans = mSpanned.getSpans(start, limit, ReplacementSpan.class);
        spans = TextUtils.removeEmptySpans(spans, mSpanned, ReplacementSpan.class);
        hasReplacement = spans.length > 0;
    }
    mCharsValid = hasReplacement || hasTabs || directions != Layout.DIRS_ALL_LEFT_TO_RIGHT;
    if (mCharsValid) {
        if (mChars == null || mChars.length < mLen) {
            mChars = new char[ArrayUtils.idealCharArraySize(mLen)];
        }
        TextUtils.getChars(text, start, limit, mChars, 0);
        if (hasReplacement) {
            // Handle these all at once so we don't have to do it as we go.
            // Replace the first character of each replacement run with the
            // object-replacement character and the remainder with zero width
            // non-break space aka BOM.  Cursor movement code skips these
            // zero-width characters.
            char[] chars = mChars;
            for (int i = start, inext; i < limit; i = inext) {
                inext = mSpanned.nextSpanTransition(i, limit, ReplacementSpan.class);
                ReplacementSpan[] spans = mSpanned.getSpans(i, inext, ReplacementSpan.class);
                spans = TextUtils.removeEmptySpans(spans, mSpanned, ReplacementSpan.class);
                if (spans.length > 0) {
                    // transition into a span
                    chars[i - start] = '';
                    for (int j = i - start + 1, e = inext - start; j < e; ++j) {
                        // used as ZWNBS, marks positions to skip
                        chars[j] = '';
                    }
                }
            }
        }
    }
    mTabs = tabStops;
}
Also used : ReplacementSpan(android.text.style.ReplacementSpan) Paint(android.graphics.Paint)

Example 94 with Paint

use of android.graphics.Paint in project XobotOS by xamarin.

the class TextLine method drawTextRun.

/**
     * Render a text run with the set-up paint.
     *
     * @param c the canvas
     * @param wp the paint used to render the text
     * @param start the start of the run
     * @param end the end of the run
     * @param contextStart the start of context for the run
     * @param contextEnd the end of the context for the run
     * @param runIsRtl true if the run is right-to-left
     * @param x the x position of the left edge of the run
     * @param y the baseline of the run
     */
private void drawTextRun(Canvas c, TextPaint wp, int start, int end, int contextStart, int contextEnd, boolean runIsRtl, float x, int y) {
    int flags = runIsRtl ? Canvas.DIRECTION_RTL : Canvas.DIRECTION_LTR;
    if (mCharsValid) {
        int count = end - start;
        int contextCount = contextEnd - contextStart;
        c.drawTextRun(mChars, start, count, contextStart, contextCount, x, y, flags, wp);
    } else {
        int delta = mStart;
        c.drawTextRun(mText, delta + start, delta + end, delta + contextStart, delta + contextEnd, x, y, flags, wp);
    }
}
Also used : Paint(android.graphics.Paint)

Example 95 with Paint

use of android.graphics.Paint in project XobotOS by xamarin.

the class BoringLayout method init.

/* package */
void init(CharSequence source, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, BoringLayout.Metrics metrics, boolean includepad, boolean trustWidth) {
    int spacing;
    if (source instanceof String && align == Layout.Alignment.ALIGN_NORMAL) {
        mDirect = source.toString();
    } else {
        mDirect = null;
    }
    mPaint = paint;
    if (includepad) {
        spacing = metrics.bottom - metrics.top;
    } else {
        spacing = metrics.descent - metrics.ascent;
    }
    if (spacingmult != 1 || spacingadd != 0) {
        spacing = (int) (spacing * spacingmult + spacingadd + 0.5f);
    }
    mBottom = spacing;
    if (includepad) {
        mDesc = spacing + metrics.top;
    } else {
        mDesc = spacing + metrics.ascent;
    }
    if (trustWidth) {
        mMax = metrics.width;
    } else {
        /*
             * If we have ellipsized, we have to actually calculate the
             * width because the width that was passed in was for the
             * full text, not the ellipsized form.
             */
        TextLine line = TextLine.obtain();
        line.set(paint, source, 0, source.length(), Layout.DIR_LEFT_TO_RIGHT, Layout.DIRS_ALL_LEFT_TO_RIGHT, false, null);
        mMax = (int) FloatMath.ceil(line.metrics(null));
        TextLine.recycle(line);
    }
    if (includepad) {
        mTopPadding = metrics.top - metrics.ascent;
        mBottomPadding = metrics.bottom - metrics.descent;
    }
}
Also used : Paint(android.graphics.Paint)

Aggregations

Paint (android.graphics.Paint)3611 Canvas (android.graphics.Canvas)815 Bitmap (android.graphics.Bitmap)735 RectF (android.graphics.RectF)484 Rect (android.graphics.Rect)400 Path (android.graphics.Path)281 TextPaint (android.text.TextPaint)269 PorterDuffXfermode (android.graphics.PorterDuffXfermode)232 Matrix (android.graphics.Matrix)173 SuppressLint (android.annotation.SuppressLint)148 Point (android.graphics.Point)147 TypedArray (android.content.res.TypedArray)134 BitmapShader (android.graphics.BitmapShader)117 View (android.view.View)103 Drawable (android.graphics.drawable.Drawable)99 BitmapDrawable (android.graphics.drawable.BitmapDrawable)92 ColorMatrix (android.graphics.ColorMatrix)76 ColorMatrixColorFilter (android.graphics.ColorMatrixColorFilter)66 Resources (android.content.res.Resources)65 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)57