How to adjust the text auto-resize & when it moves to the 2nd line

29
March 16, 2019, at 10:20 AM

I am building a meme generator. The issue that I'm having is with the texts that are drawn on the Canvas. 2 things happen:

  • The text shrinks to a really small size until it moves onto the next line and that's because I've been relying on TextView's auto resize functionality. What I end up with is a really long and thin string.

  • The bottom text is bigger than the top text and words move into the 2nd line a lot sooner. This shouldn't happen because I'm setting the text size the exact same thing for both of them (I'm getting the text size from TextViews, which is 50sp for both). The 2 texts (top & bottom) are the same, btw. They both read This is going to be a really long string because I'm trying to address a problem

Here is what I'm talking about:

The idea of I want to do is to set a specific margin on the Canvas which the text can extent to and needs to move to the 2nd line if the text is longer than the margin specified. I want the text size to remain the same while it is within that margin (which means its only 1 line) and decrease the text size only when it moves into a new line each time.

Here is my code that deals with the text size:

Canvas canvas = new Canvas(mutableBitmap);
TextPaint topFillPaint = new TextPaint();
TextPaint bottomFillPaint = new TextPaint();
TextPaint topStrokePaint = new TextPaint();
TextPaint bottomStrokePaint = new TextPaint();
Typeface typeface = getResources().getFont(R.font.impact);
topFillPaint.setColor(Color.WHITE);
topFillPaint.setTextSize(topTextView.getTextSize());
topFillPaint.setTypeface(typeface);
topStrokePaint.setStyle(Paint.Style.STROKE);
topStrokePaint.setStrokeWidth(8);
topStrokePaint.setTextSize(topTextView.getTextSize());
topStrokePaint.setColor(Color.BLACK);
topStrokePaint.setTypeface(typeface);
bottomFillPaint.setColor(Color.WHITE);
bottomFillPaint.setTextSize(bottomTextView.getTextSize());
bottomFillPaint.setTypeface(typeface);
bottomStrokePaint.setStyle(Paint.Style.STROKE);
bottomStrokePaint.setStrokeWidth(8);
bottomStrokePaint.setColor(Color.BLACK);
bottomStrokePaint.setTextSize(bottomTextView.getTextSize());
bottomStrokePaint.setTypeface(typeface);
StaticLayout topFillLayout = new StaticLayout(topText, topFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
        1.0f, 0.0f, false);
StaticLayout topStrokeLayout = new StaticLayout(topText, topStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
        1.0f, 0.0f, false);
StaticLayout bottomFillLayout = new StaticLayout(bottomText, bottomFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
        1.0f, 0.0f, false);
StaticLayout bottomStrokeLayout = new StaticLayout(bottomText, bottomStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
        1.0f, 0.0f, false);
topFillLayout.draw(canvas);
topStrokeLayout.draw(canvas);
canvas.translate(0, canvas.getHeight() - 210);
bottomFillLayout.draw(canvas);
bottomStrokeLayout.draw(canvas);
READ ALSO
How can get android.developer site offline? [on hold]

How can get android.developer site offline? [on hold]

I want to download all documentation of this site: https://developerandroid

23
When a WindowManager android.view.WindowLeaked is thrown, is memory recovered?

When a WindowManager android.view.WindowLeaked is thrown, is memory recovered?

In certain rare situations, I get an error like E/WindowManager: androidview

25
androidx.RecyclerView ListAdapter very slow

androidx.RecyclerView ListAdapter very slow

I am using the ListAdapter with RecyclerView and under certain circumstances the app becomes extremely slow -- it freezes for 10 seconds with a list of 1000 items

18