Example usage for android.text DynamicLayout DynamicLayout

List of usage examples for android.text DynamicLayout DynamicLayout

Introduction

In this page you can find the example usage for android.text DynamicLayout DynamicLayout.

Prototype

@Deprecated
public DynamicLayout(@NonNull CharSequence base, @NonNull TextPaint paint, @IntRange(from = 0) int width,
        @NonNull Alignment align, @FloatRange(from = 0.0) float spacingmult, float spacingadd,
        boolean includepad) 

Source Link

Usage

From source file:com.ruesga.timelinechart.TimelineChartView.java

private void drawTickLabels(Canvas c, LongSparseArray<Pair<double[], int[]>> data) {
    final float alphaVariation = MAX_ZOOM_OUT - MIN_ZOOM_OUT;
    final float alpha = MAX_ZOOM_OUT - mCurrentZoom;
    mTickLabelFgPaint.setAlpha((int) ((alpha * 255) / alphaVariation));

    final int size = data.size() - 1;
    final float cx = mGraphArea.left + (mGraphArea.width() / 2);
    for (int i = mItemsOnScreen[1]; i >= mItemsOnScreen[0]; i--) {
        // Update the dynamic layout
        long timestamp = data.keyAt(i);
        final int tickFormat = getTickLabelFormat(timestamp);
        mTickDate.setTime(timestamp);//from   w w w . j  av  a 2  s .  c o  m
        final String text = mTickFormatter[tickFormat].format(mTickDate).replace(".", "")
                .toUpperCase(Locale.getDefault());
        DynamicSpannableString spannable = mTickTextSpannables[tickFormat].get(text.length());
        if (spannable == null) {
            // If we don't have an spannable for the text length, create a new one
            // that allow to use it now and in the future. Doing this here (on draw)
            // is not the best, but it supposed to only be performed one time per
            // different tick text length
            spannable = createSpannableTick(tickFormat, text);
            mTickTextSpannables[tickFormat].put(text.length(), spannable);
        }
        spannable.update(text);

        DynamicLayout layout = mTickTextLayouts[tickFormat].get(text.length());
        if (layout == null) {
            // Update the layout as well
            layout = new DynamicLayout(spannable, mTickLabelFgPaint, (int) mBarItemWidth,
                    Layout.Alignment.ALIGN_CENTER, 1.0f, 1.0f, false);
            mTickTextLayouts[tickFormat].put(text.length(), layout);
        }

        // Calculate the x position and draw the layout
        final float x = cx + mCurrentOffset - (mBarWidth * (size - i)) - (layout.getWidth() / 2);
        final int restoreCount = c.save();
        c.translate(x, mFooterArea.top + (mFooterArea.height() / 2 - mTickLabelMinHeight / 2));
        layout.draw(c);
        c.restoreToCount(restoreCount);
    }
}

From source file:com.ruesga.timelinechart.TimelineChartView.java

@SuppressWarnings("unchecked")
private void setupTickLabels() {
    synchronized (mLock) {
        mTickCalendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());

        mTextSizeFactor = mFooterBarHeight / mDefFooterBarHeight;
        mTickLabelFgPaint.setTextSize((int) (mSize8 * mTextSizeFactor));

        mTickDate = new Date();

        int count = mTickFormats.length;
        mTickTextLayouts = new SparseArray[count];
        mTickFormatter = new SimpleDateFormat[count];
        mTickTextSpannables = new SparseArray[count];
        for (int i = 0; i < count; i++) {
            mTickFormatter[i] = new SimpleDateFormat(mTickFormats[i], Locale.getDefault());
            mTickDate.setTime(Long.valueOf(mTickLabels[i]));
            final String text = mTickFormatter[i].format(mTickDate).replace(".", "")
                    .toUpperCase(Locale.getDefault());
            mTickTextSpannables[i] = new SparseArray<>();
            mTickTextLayouts[i] = new SparseArray<>();

            // Store spannable in memory based in its length, so we don't have to rebuild
            // a every time, just only in case they are needed (normally never)
            DynamicSpannableString spannable = createSpannableTick(i, text);
            mTickTextLayouts[i].put(text.length(), new DynamicLayout(spannable, mTickLabelFgPaint,
                    (int) mBarItemWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 1.0f, false));

            // Save min height
            mTickLabelMinHeight = Math.max(mTickLabelMinHeight,
                    mTickTextLayouts[i].get(text.length()).getHeight());
        }/*from  w  ww.j ava2 s . c  om*/
    }
}