Example usage for android.text DynamicLayout draw

List of usage examples for android.text DynamicLayout draw

Introduction

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

Prototype

public void draw(Canvas c) 

Source Link

Document

Draw this Layout on the specified Canvas.

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);// w w w .  j av a 2  s. co  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);
    }
}