Change text height and width separately when drawing with canvas - android

I want to know if there is any way to modify text height and width separately when drawing with canvas. To set text size you can simply do paint.setTextSize(x);but it change text size in X and Y and I need to change text size in X and Y separately as you do with paint.setTextScaleX(x);but there isn't anything like paint.setTextScaleY(y);.
Is any way to implement this or does it already exists in Android ?
Thank you

There is no setTextScaleY method, because there is no need for one. setTextSize multiplies both X and Y scale factors, and setTextScaleX multiplies the X scale factor only. So you could reach any desired scaling scaleX, scaleY this way:
setTextSize(scaleY);
setTextScaleX(scaleX/scaleY); //setTextScaleX scales according to the CURRENT size (setTextScaleX(1) does nothing).

Related

Android. Font size too large to fit in cache. how to get cache size?

This is not duplicate, please read it first.
I've been recently working with a custom view that displays text and scales it with 'scaleX' and 'scaleY'. problem with this approach is that if you rotate the text to certain angles the quality text drawn on screen is extremely bad, so to counter that I set text size of TextPaint to a large number and scale it down to desired scale. problem with that approach is that if I enter an emoji inside that text then the emoji wouldn't render because of that large number that I set as text size but normal characters are rendering fine; and then in logcat says OpenGlRenderer: Font size too large to fit in cache
How can I retrieve size of the font cache to determine the size of text according to that?
Setting layer type to hardware or software did not solve the problem.
And I don't want to scale the text by increasing the text size (I scale it with 'scaleX' and 'scaleY')
Code for setting text size:
context.resources.displayMetrics.run {
textPaint.textSize = min(widthPixels, heightPixels) / density
}
How I draw text:
canvas.drawText(text,0f /* x */, textBaseline /* y */, textPaint /* has large text size*/)
How I scale the text:
scaleX *= scaleFactor
scaleY *= scaleFactor
After looking at OpenGLRenderer documents, I came across few properties that defined the minimum size of font cache (small, medium, large) and I took the small font cache minimum height which was 256 pixels and set it as text size and it started to render emojis and text had good rendering quality too but I'm not sure if that's directly relatable to my problem, so I let the question to be open.
https://source.android.com/devices/graphics/renderer

How to use method setDocumentViewBox() in AndroidSVG

I use the androidsvg-1.2.1.jar for rendering svg image. The original size of the image is 260 pixels in width and 100 pixels in height. I tried to set the width of the image in proportion to the width of the display as follows:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
height = (int) (width / 2.6);
svg.setDocumentHeight(height);
svg.setDocumentWidth(width);
svg.setDocumentViewBox(0, 0, width, height);
The docs says that methods getDocumentHeight, setDocumentWidth and method setDocumentViewBox accept input values in pixels. But in this case viewbox had estimated size, but the picture itself was located in the left top corner of the viewbox and its size was much smaller than the size of the viewbox (less than about four times).
When I changed the last row of the code as
svg.setDocumentViewBox(0, 0, width/4, height/4);
the size of the picture has become almost equal to the size viewbox, but still remained a little smaller. Why is this happening? And what values should be applied to the input of the setDocumentViewBox method?
The viewBox is meant to describe the limits of the contents of the SVG. In other words, the bounding box around the graphical elements in the file. That's how the renderer knows how much it needs to scale the SVG to fill the area of the SVG viewport. The viewport is the rectangle you specify with width and height (setDocumentWidth() and setDocumentHeight()).
To get a perfect fit, you need to set the viewBox to the exact dimensions of your contents. You haven't provided or linked your SVG, so I can't tell you exactly what that is in your case.
But for example, say your SVG was a rectangle that was at 0,0 and was 100 wide and 20 high. You would need to do setDocumentViewBox(0,0,100,20). If your SVG was a circle of radius 50 at 80,60, you would do setDocumentViewBox(30,10,100,100).
In your case, it looks as if the light gray rectangle at the back defines the limits of your content, so you would probably be using the dimensions of that for your view box.
You say the original size of your SVG is 260x100. If that corresponds to the size of that grey rectangle, then you would set the viewBox with setDocumentViewBox(0,0,260,100).

How to convert coordinates on Bitmap to real coordiates on Image View displayed on screen

I have an Image View which displays an image (e.g 2000x1000 pixels) and I have a coordinate (X,Y) on that image (not the image view). The canvas of my Image View is 600x800 for example. How can I convert the point (X,Y) to screen coordinate so that I can draw a path with them on the OnDraw(...) method of Image View. Any help is appreciated! Thank you.
Update: If I use matrix to draw the path between coordinates, it works but the path and objects i draw become really small. Here is the code i used.
final Matrix matrix = canvas.getMatrix();
matrix.preConcat( _view.getImageMatrix() );
matrix.preScale( 1.0f /_inSampleSize, 1.0f / _inSampleSize);
canvas.setMatrix( matrix );
//I draw the path here
Update: I add a picture to show the effect when using matrix to draw the path. I would like to have the 4 line and the 4 corner balls to be in normal size. The red color is the boundary of the Image View which holds the picture.
I think that might depend on how exactly you are displaying your image. Your ImageView (600x800) is not the same aspect ratio as your bitmap (2000x1000).
You are keeping the bitmap's aspect ratio stable as you scale it down? If so, which part (height or width) takes up the full screen and which has black (or whatever else) as padding? This will help you determine your scale factor.
scale_factor = goal_height/height1; //if height is what you are scaling by
scale_factor = goal_width/width1; //if width is what you are scaling by.
I would try:
x_goal = x1 * scale_factor;
y_goal = y1 * scale_factor;
That is, if you have a point (1333, 900) in your image, and your image takes up the full width, you would multiply both x and y by 600/2000 to get (399.9, 270). (you might want to round that decimal).
If you are NOT keeping the bitmaps aspect ratio stable (that is, you're squeezing it to fit), then you'd have a height_scale_factor and a width_scale factor. So you'd take (1333,900) and multiply x by 600/2000 and y by 800/1000 to get (399.9,720).

Accessing View dimensions at startup

I have an android View X and Y where X has to be manipulated based on the dimensions of Y (specifically, X and all its contents must be scaled up to Y without its aspect ratio being changed). I do this by working out the new (larger) size of X and setting scaleX and scaleY accordingly. Please tell me if there is a better way! If not, my second problem is - I would like this to be done in Activity.onCreate or onStart or onResume. But in these the dimensions of X and Y are reported as 0.0. Is there any way I can do this on activity start after Y is laid out so its dimensions are not 0.0.
Regards,
Steve Kucera
Any view in android goes through several steps before it appears on screen. Firat of all it goes to measure() - here will be field measuredHeight and measuredWidth. Then it goes to layout() and only after this method finish view has params width and height. So, you can provide your own view, that will contains both X and Y, and there, while measure and layout, change width or height of child X.

draw a rectangle using rect method from android graphics but i want the rectangle to fit any screen

i want to draw a rectangle using rect method form android graphics but i want to specify the values in dp so that it can fix to any screen size. i presume by default its in pixel or just x,y coordinates.
how can i draw it to fit in any screen size using the rect method
You can use DisplayMetrics to obtain the screen's size and pixel density and then calculate your rectangle's width and height accordingly by adjusting the width and height in relation to the screen's characteristics.

Categories

Resources