Example usage for android.text DynamicLayout getWidth

List of usage examples for android.text DynamicLayout getWidth

Introduction

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

Prototype

public final int getWidth() 

Source Link

Document

Return the width of this layout.

Usage

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

private void ensureBarWidth() {
    if (!mShowFooter) {
        return;//w ww. j av a 2 s  . c  o m
    }
    if (mTickTextLayouts != null) {
        float minWidth = 0.f;
        for (SparseArray<DynamicLayout> a : mTickTextLayouts) {
            int count = a.size();
            for (int i = 0; i < count; i++) {
                DynamicLayout layout = a.valueAt(i);
                final float width = layout.getWidth();
                if (minWidth < width) {
                    minWidth = width;
                }
            }
        }
        if (minWidth > mBarItemWidth) {
            Log.w(TAG, "There is not enough space for labels. Switch BarItemWidth to " + minWidth);
            mBarItemWidth = minWidth;
        }
    }
    mBarWidth = mBarItemWidth + mBarItemSpace;
}

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  ww w . ja v  a2  s.  c  om*/
        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);
    }
}