3

Is there a method to draw text with in a specified rectangle? I am drawing directly to a canvas(ImageView) using

canvas.drawText(text,x,y,paint) 

But this drew the entire text in a single line. I want to wrap the text with in the specified (x,y) ,(x1,y1) limit. I don't want to use textviews or any other views.

I just want to draw the text over an image.

Is there any method to do this?

Thanks in Advance

Loktar
  • 34,764
  • 7
  • 90
  • 104
Devu Soman
  • 2,246
  • 13
  • 36
  • 57

1 Answers1

4

Firstly you have to determine the text size. The width of each character could be get by getTextWidths(), the height is same with text size. Try to estimate a initial text size, and use the height and width of text to adjust the final value.

Secondly you need to break lines. Paint.getTextWidths() or Paint.breakText() can all achieve this target.

Edit: add the code example.

public static class RectTextView extends View {
    private int mWidth = 200;
    private int mHeight = 100;
    private String mText = "Hello world. Don't you know why, why you and I.";
    private Paint mPaint;
    private List<Integer> mTextBreakPoints;

    public RectTextView(Context context) {
        this(context, null);
    }

    public RectTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
        setSuitableTextSize();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int start = 0;
        int x = 0;
        int y = 0;
        for (int point : mTextBreakPoints) {
            y += mPaint.getTextSize();
            canvas.drawText(mText, start, point, x, y, mPaint);
            start = point;
        }
    }

    private void setSuitableTextSize() {
        int textSize = getEstimateTextSize();
        for (; textSize > 0; textSize--) {
            if (isTextSizeSuitable(textSize))
                return;
        }
    }

    private boolean isTextSizeSuitable(int size) {
        mTextBreakPoints = new ArrayList<Integer>();
        mPaint.setTextSize(size);
        int start = 0;
        int end = mText.length();
        while (start < end) {
            int len = mPaint.breakText(mText, start, end, true, mWidth,
                    null);
            start += len;
            mTextBreakPoints.add(start);
        }
        return mTextBreakPoints.size() * size < mHeight;
    }

    private int getEstimateTextSize() {
        return (int) Math.sqrt(mWidth * mHeight / mText.length() * 2);
    }
}
faylon
  • 7,360
  • 1
  • 30
  • 28
  • I don't want to use any views.I just want to draw the text over an image.Is there any method like drawText(..)? – Devu Soman Nov 27 '12 at 08:58
  • There is not such a method. Use new Canvas(Bitmap bitmap) to make a canvas, and draw the text in the Bitmap. – faylon Nov 27 '12 at 09:01