Erik
Erik

Reputation: 5119

android (Advanced)Saving a Bitmap Canvas with DrawText but textsize calculation needed

I have a Canvas that i draw text on.
(this is quite advanced i think hope you can follow me)

See attached image below.

The functionality is that I can tap on the screen to change the textsize.
Tap left screen = smaller Font.
Tap right Screen = bigger Font.
I can then also move the text on the screen.

When textsize is ok and i have moved the text where i want it,
Then I want to save it back to the original Bitmap.

I use options.inSampleSize = 4; to initially load the Bitmap

The ImageView that have the Bitmap is of course smaller then the original Image.
Some kind of calculation is needed. This tends to be quite difficult to do.

I have the options.inSampleSize = 4 Bitmaps Ratio.
It's 0.59, 0.69 something depending on Landscape or portrait.

Im playing around with that to somehow change the new BitmapsetTextSize
to look the same as the ImageView smaller Bitmap.

What could i do here?
I have a feeling that since one never know what size an image have.
I have to somehow scale/constrain the Loaded Bitmap Ratio to a fixed Ratio.

Then i need to using percentage or something to transfer the text location to the bigger image.
I can kind of do that when it comes to initial (red small ball on picture) location. Hence, the starting point of the text.

But i dont know how long the text is so im stuck so so speak and asking for advice

One way i tried was to divide paint.getTextSize() with the Ratio something like 0.59. That looked like a solution at first. But the image ratio is not fixed and the Font size is not fixed something else is needed.

Here are two pictures showing the problem.

On phone Bitmap:

enter image description here

The saved new Bitmap:

enter image description here

Upvotes: 2

Views: 1515

Answers (1)

kabuko
kabuko

Reputation: 36302

I'm not 100% clear that I understand what you mean, but here's a go. It sounds like you were close to the right approach. Instead of using a fixed ratio, you need to calculate the ratio that the image is scaled by to fit in the view on the phone, then you can scale the text by the inverse ratio. So in steps:

  • Measure the width of the original image (height would do just as well, but we just need one dimension)
  • Measure the width of the scaled image
  • Calculate ratio (ratio = original / scaled)
  • Let the user type their text
  • You can then get the text size using something like: float paintSize = paint.getTextSize();
  • For rendering on the final image, use paint.setTextSize(paintSize / ratio);.

Upvotes: 4

Related Questions