How to draw a text inside a box on canvas with DynamicLayout and Ellipsize

北城余情 提交于 2019-12-21 21:50:21

问题


I need to draw a text on canvas inside a specific box. I'm already using DynamicLayout to automatically calculate and break lines to fit inside the box width. Now I need to ellipsize the text automatically to fit the box height.

How can I achieve this? It doesn't necessarily need to be by the height (pixels), it could be by the max number of lines.


Example:

"This is a sample text to fit inside the box"

Actual Result:

------------
|This is a |
|text to   |
|fit inside|
------------
 the box   

Expected Result:

------------
|This is a |
|text to   |
|fit in... |
------------

I create the DynamicLayout like this:

textLayout = new DynamicLayout(mText, mTextPaint, 100, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

And then I draw it like this:

canvas.save();
canvas.translate(500, 500 - textLayout.getHeight() / 2);
textLayout.draw(canvas);
canvas.restore();

回答1:


As I know the number of lines (mMaxLines) that fit inside the box, I extended the DynamicLayout and I overrided a few methods:

@Override
public int getLineCount() {
    if (super.getLineCount() - 1 > mMaxLines) {
        return mMaxLines;
    }
    return super.getLineCount() - 1;
}

@Override
public int getEllipsisCount(int line) {
    if (line == mMaxLines - 1 && super.getLineCount() - 2 > line) {
        return 1;
    }
    return 0;
}

@Override
public int getEllipsisStart(int line) {
    if (line == mMaxLines - 1 && super.getLineCount() - 2 > line) {
        return getLineEnd(line) - getLineStart(line) - 1;
    }
    return 0;
}

Then I have the following result:

------------
|This is a |
|text to   |
|fit in... |
------------



回答2:


Please add parameter in DynamicLayout same us bellow. DynamicLayout(CharSequence base, CharSequence display, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth);




回答3:


For future readers try this StaticLayoutWithMaxLines backport:

import android.os.Build;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextDirectionHeuristic;
import android.text.TextDirectionHeuristics;
import android.text.TextPaint;
import android.text.TextUtils.TruncateAt;
import android.util.Log;

import java.lang.reflect.Constructor;

public class StaticLayoutWithMaxLines {
private static final String LOGTAG =  StaticLayoutWithMaxLines.class.getSimpleName();

private static final String TEXT_DIR_CLASS = "android.text.TextDirectionHeuristic";
private static final String TEXT_DIRS_CLASS = "android.text.TextDirectionHeuristics";
private static final String TEXT_DIR_FIRSTSTRONG_LTR = "FIRSTSTRONG_LTR";

private static boolean sInitialized;

private static Constructor<StaticLayout> sConstructor;
private static Object[] sConstructorArgs;
private static Object sTextDirection;

public static synchronized void ensureInitialized() {
    if (sInitialized) {
        return;
    }

    try {
        final Class<?> textDirClass;
        if (Build.VERSION.SDK_INT >= 18) {
            textDirClass = TextDirectionHeuristic.class;
            sTextDirection = TextDirectionHeuristics.FIRSTSTRONG_LTR;
        } else {
            final ClassLoader loader = StaticLayoutWithMaxLines.class.getClassLoader();
            textDirClass = loader.loadClass(TEXT_DIR_CLASS);

            final Class<?> textDirsClass = loader.loadClass(TEXT_DIRS_CLASS);
            sTextDirection = textDirsClass.getField(TEXT_DIR_FIRSTSTRONG_LTR)
                                          .get(textDirsClass);
        }

        final Class<?>[] signature = new Class[] {
                CharSequence.class,
                int.class,
                int.class,
                TextPaint.class,
                int.class,
                Alignment.class,
                textDirClass,
                float.class,
                float.class,
                boolean.class,
                TruncateAt.class,
                int.class,
                int.class
        };

        // Make the StaticLayout constructor with max lines public
        sConstructor = StaticLayout.class.getDeclaredConstructor(signature);
        sConstructor.setAccessible(true);
        sConstructorArgs = new Object[signature.length];
    } catch (NoSuchMethodException e) {
        Log.e(LOGTAG, "StaticLayout constructor with max lines not found.", e);
    } catch (ClassNotFoundException e) {
        Log.e(LOGTAG, "TextDirectionHeuristic class not found.", e);
    } catch (NoSuchFieldException e) {
        Log.e(LOGTAG, "TextDirectionHeuristics.FIRSTSTRONG_LTR not found.", e);
    } catch (IllegalAccessException e) {
        Log.e(LOGTAG, "TextDirectionHeuristics.FIRSTSTRONG_LTR not accessible.", e);
    } finally {
        sInitialized = true;
    }
}

public static boolean isSupported() {
    if (Build.VERSION.SDK_INT < 14) {
        return false;
    }

    ensureInitialized();
    return (sConstructor != null);
}

public static synchronized StaticLayout create(CharSequence source, int bufstart, int bufend,
                                               TextPaint paint, int outerWidth, Alignment align,
                                               float spacingMult, float spacingAdd,
                                               boolean includePad, TruncateAt ellipsize,
                                               int ellipsisWidth, int maxLines) {
    ensureInitialized();

    try {
        sConstructorArgs[0] = source;
        sConstructorArgs[1] = bufstart;
        sConstructorArgs[2] = bufend;
        sConstructorArgs[3] = paint;
        sConstructorArgs[4] = outerWidth;
        sConstructorArgs[5] = align;
        sConstructorArgs[6] = sTextDirection;
        sConstructorArgs[7] = spacingMult;
        sConstructorArgs[8] = spacingAdd;
        sConstructorArgs[9] = includePad;
        sConstructorArgs[10] = ellipsize;
        sConstructorArgs[11] = ellipsisWidth;
        sConstructorArgs[12] = maxLines;

        return sConstructor.newInstance(sConstructorArgs);
    } catch (Exception e) {
        throw new IllegalStateException("Error creating StaticLayout with max lines: " + e);
    }
}

}

code snippet from here.

And use it like this:

canvas.save();
CharSequence text = YOUR TEXT HERE;
StaticLayout textLayout = StaticLayoutWithMaxLines.create(text, 0, text.length(),
mPaintText, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false,
                    TextUtils.TruncateAt.END, canvas.getWidth(), 4);

canvas.translate(mViewCenterX, y + mItemHeight / 2 - textLayout.getHeight() / 2);
textLayout.draw(canvas);
canvas.restore();



回答4:


refer this for automatic ellipsize

http://developer.android.com/reference/android/widget/TextView.html#attr_android%3aellipsize



来源:https://stackoverflow.com/questions/17341266/how-to-draw-a-text-inside-a-box-on-canvas-with-dynamiclayout-and-ellipsize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!