How to set text size relative to bitmap resolution? - android

Hey I'm trying to write text to a bitmap, And i have no idea how to set the text size relative.
I've found some who set text size as DIP using the density, which i assume is OK
but if i try and write text on a smaller image text becomes huge.
This is the code i have :
private static final float GESTURE_THRESHOLD_DIP = 95.0f;
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
paint.setTextSize((int) (GESTURE_THRESHOLD_DIP * scale + 0.5f));
How can i set text size to be relative to the paint size?

Well in the end i just checked what was the resolution of the images it looks good and calculated that height-width result divided to X gave me the number i wanted,
so i just divided the font by 6.
It will fail on several occasions but it's better then what i had.
Please someone answer a better solution.

Related

setTextSize accordingly to ImageView's image size

I got a horizontally oriented LinearLayout with an ImageView and a TextView. TextView has a text of 1- or 2-digits number.
What I need is to set text size of a TextView to be exactly same in a hight of an ImageView's image.
I know I can get ImageView's Image height , but how can I make textSize as high accordingly?
View.getHeight():
Return the height of your view. Returns
Returns
    The height of your view, in pixels.
TextView.setTextSize(float):
Set the default text size to the given value, interpreted as "scaled
pixel" units. This size is adjusted based on the current density and
user font size preference.
Parameters
   size    The scaled pixel size.
This basically means you need to convert the height (px) to text height (sp):
float density = getResources().getDisplayMetrics().scaledDensity;
float textHeight = imageHeight / density;
Give constant height-width to both(image view and textview)... or try scaleType="fitxy"...
You can try:
float height = imageView.getHeight();
TextView tv = new TextView(context);
tv.setTextSize(height)
I'm not sure this will do exactly what you want, but it is the most obvious starting point I see.
well, I've found myself the following :
first we need to find a real height of an ImageView's Image - it's not as high as the ImageView, so it's explained well here : How to get the width and height of an android.widget.ImageView?
next we set the height of the text :
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
imageView.getMeasuredHeight()*(imageView.getMeasuredHeight()/
imageView.getMeasuredWidth()));
which means we are setting textSize in raw pixels, then we take height and multiply it with the picture's ratio of height to width so TextView's text size will be exactly high as the image

How to apply 9 patch images in android

I have an Image with width to height ratio 7:2.
Currently I am using scaleType="fitXY" with android:width="match_parent" and android:height="wrap_content".
But due to irregular scaling the text in the image is skewed and I am not get a quality Image. Can using 9 patch images solve the issue what other workaround do I have?
You could indeed use a 9 patch image, but you would need to stretch the non-text parts of the image so the text would not stretch. This would most likely give undesirable results with possibly tiny text in a large scaled image.
A better way would to have a textless 9patch image and use it as a background to a TextView and scale the text with textView.setTextSize(x) to fit the image.
Eg.
TextView theText = new TextView(this);
theText.setBackgroundResource(R.drawable.yourninepatch);
float scale = c.getResources().getDisplayMetrics().density;
int screenWidth = c.getResources().getDisplayMetrics().widthPixels;
float fontSize = screenWidth / 26 / scale;
theText.setTextSize(fontSize);

calculate how much space will text need

I need to make "pages" (most likely for use ViewPager) which will contain images and text.
For example first will have image, at image download will get image dimensions. After image on layout will be X space left where i can display Y lenght text. Then for next page i will split rest of text into new string to be displayed. TextSize is in dp units.
I had idea to get how much pixels avarage letter takes and then calculate approx how many lines i can show in one page.
What would be a best way to make these calculations ?
And for starter i did letter calculation
final float densityMult = ctx.getResources().getDisplayMetrics().density;
final float scaledPx = 20 * densityMult; //i guess its same as 20dp
paint.setTextSize(scaledPx);
final float size = paint.measureText("a");
Where on 480x800 3.7" screen it returns value 16.0 and on 540x960 4.0" 17.0
Is these values pixels ?
I didn't really understand why you want to measure your text, but just like you have:
paint.measureText("a");
you can measure any string, not just characters. If you want to split your text manually (I shouldn't recommend that), you can check whenever the measure of your text is higher than the available width.
And yes, measureText returns the measure in px

Android TextView getTextSize() does it include descenders, ascenders

I can't find anywhere there is a mention of how the getTextSize() in Textview is measured. From visual tests it doesn't seem to be including descenders, but seems to include ascenders. It doesn't appear to start exactly from the baseline also.
http://en.wikipedia.org/wiki/Descender
This is the closest mention of it but Romain Guy from Google just ignores that part of the question.
http://www.mail-archive.com/android-developers#googlegroups.com/msg08514.html
As I need this as I am using Compound Drawables and I need to be able to align the drawable to the text on different devices.
Here is code I used to test on a compound drawable a circle that touches edges
tvData.setTextSize(TypedValue.COMPLEX_UNIT_PX, 100);
tvData.setText("agB5ãÂ");
int size = (int)tvData.getTextSize();
Drawable img = getResources().getDrawable(R.drawable.circle_white_full_72 ).mutate();
img.setBounds( 0 , 0 , size , size);
tvData.setCompoundDrawables( null, null, img, null );
Here is the result
as you can see it doesn't seem to use the descenders and ascenders.
Here is the drawable image, if others want to test
http://i.imgur.com/Yhf8b.png
When changing the image to 80% of the text size using
int size = (int)tvData.getTextSize() *80/100;
Here is the result, with the image transposed on top of the 100% image. Maybe setCompoundrawables is doing it's own scaling
I tried measuring midpoints of the font and drawable and it is off. Here is an image highlighting it
Finally I moved the drawable 50pixels to the left, and then measured the output and it was half the height of the font text baseline to baseline, as the setTextSize was set at 100px.
Android must be using some other layout to scale and position the compound drawable. Maybe I should create another question for this.
Here is an image highlighting baseline to baseline.
From some light testing, it appears to be from ascent to descent (text size = descent - ascent). Did some debugging with a TextPaint to verify, and just to be a little more specific, I did:
Paint.FontMetricsInt metrics;
for(int i = 1; i < 100; i++) {
mTextPaint.setTextSize(i);
metrics = mTextPaint.getFontMetricsInt();
if((metrics.descent - metrics.ascent) != i) Log.v("type", "Not equal");
}
And it remained true for each value.

Android textsizes different screens

I'm building a android application that need to look allright on all screen sizes. I've managed to scale drawables according to resolution with different drawable resource folders, however how can i achieve the same with pure text?
Do i really need to use different layout folders to achieve this?
Thanks for any help!
Use sp for text sizes and you should be fine.
sp
Scale-independent Pixels - This is like the dp unit, but it is also
scaled by the user's font size preference. It is recommend you use
this unit when specifying font sizes, so they will be adjusted for
both the screen density and the user's preference.
from Dimensions
dp/dip can also work, but doesn't include the users font preferences.
If it is for only textview. declare the size ad dp in textview in xml. dp is desipixels which is related to the screen resolution and the textview size gets adjusted accordingly.
I used an own-written method for that.. worked for two displays(1024x720, 800x480), hope it works for yours, too
The Class:
//this is a seperated class, for button textsize
//declaration of standardvalues
public class Fontstyles {
static int INT_standardwidth = 1024, INT_standardsize = 50; //do not change
public static double BTN_TXT_size(Button BTN_X) {
Point size = new Point();
YourMainActivity.DIS_main.getSize(size);
int INT_width = size.x;
if (BTN_X.getText().length() == 0)
return 0;
// width of display (INT_width)
// width standartvalue (INT_standardwidth)
// standardfontsize (INT_standardsize)
return ((float) ((INT_width * 100 / INT_standardwidth)
* (INT_standardsize * (INT_width * 100 / INT_standardwidth) / 100) / 100));
}}
this is in YourMainAvtivity:
public static Display DIS_main;
DIS_main = getWindowManager().getDefaultDisplay();
and this is for calling the sizecalculation (wherever you want from):
YourButton.setTextSize((float) Fontstyles.BTN_TXT_size_(YourButton));
works for TextViews and Button.
after use please reply and tell me wether this worked for your display or not.

Categories

Resources