开发者

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

开发者 https://www.devze.com 2023-03-11 22:32 出处:网络
I have a Canvas that i draw text on. (this is quite advanced i think hope you can follow me) See attached image below.

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 th开发者_如何学编程at 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:

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

The saved new Bitmap:

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


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);.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号