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.
Im playing around with that to somehow change the new BitmapsetTextSize
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:
The saved new Bitmap:
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);
.
精彩评论