如何按位图宽度分割文本?

时间:2019-03-05 17:00:13

标签: android

这是我的功能
我需要在ImageView上绘制文本,但是文本用完了请帮我。

public static Bitmap textAsBitmap2(String text, float textSize, int 
    textColor, Typeface typeface, ImageView imageView) {
            float textOffset;
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            String[] str = text.split("\n");
            paint.setTextSize(textSize);
            float baseline = -paint.ascent();
            int height = (int) (baseline + paint.descent() + 2.0f);
            int width = (int) (paint.measureText(text) + 2.0f); // round
            if (!text.isEmpty()) {
            Bitmap image = Bitmap.createBitmap(1015, (height * (str.length)-(str.length)*10)+1, Bitmap.Config.ARGB_8888);
               Canvas canvas = new Canvas(image);
                for ( int i = 0; i < str.length; i++) {
                    height=(height)/str.length;
                    textOffset = (canvas.getWidth() - paint.measureText(str[i])) / 2;
                    canvas.drawText(str[i], textOffset, ((i + 1) * baseline), paint);

                }
            }

            imageView.draw(canvas);

            return image;
        }

0 个答案:

没有答案