Is it possible to change the unit for Paint.setTextSize()? As far as I know, it's pixel but I like to set the text size in DIP for multiple screen support.
I know this topic is old and already answered but I would like to also suggest this piece of code:
int MY_DIP_VALUE = 5; //5dp
int pixel= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
MY_DIP_VALUE, getResources().getDisplayMetrics());
Convert it like this
// The gesture threshold expressed in dip
private static final float GESTURE_THRESHOLD_DIP = 16.0f;
// Convert the dips to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels
from here http://developer.android.com/guide/practices/screens_support.html#dips-pels
The accepted answer is for gestures, not setting text size. The highest voted answer (at the time of this writing) is close, but the documentation recommends using sp rather than dp because in addition to being scaled for screen densities (as dp values are), sp is also scaled according to user preferred font sizes.
From an int in code
int spSize = 17;
float scaledSizeInPixels = spSize * getResources().getDisplayMetrics().scaledDensity;
mTextPaint.setTextSize(scaledSizeInPixels);
Or alternatively
int spSize = 17;
float scaledSizeInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spSize, getResources().getDisplayMetrics());
mTextPaint.setTextSize(scaledSizeInPixels);
From resources
Or if you have the sp or dp value in resources:
<resources>
<dimen name="fontSize">17sp</dimen>
</resources>
with
float scaledSizeInPixels = getResources().getDimensionPixelSize(R.dimen.fontSize);
mTextPaint.setTextSize(scaledSizeInPixels);
Other links
How to convert DP, PX, SP among each other, especially DP and SP?
Android: Canvas.drawText() text size on different screen resolutions
Paint.setTextSize
getDimensionPixelSize
And here is even shorter method to convert dp-s to px-els taking display metrics into account
https://developer.android.com/reference/android/content/res/Resources.html#getDimensionPixelSize(int)
If your Paint object is being used to draw text on a Canvas, you can let the Canvas handle scaling for you.
When calling Canvas.drawText() the text size is first determined by the passed in Paint object, which can be set via Paint.setTextSize(). The text size is automatically scaled by Canvas based on the canvas density, which can be found using Canvas.getDensity().
When setting the text size on a paint object that will be drawn on Canvas, work with a unit value of dp or sp and let Canvas handle the scaling for you.
Related
I had this issue:
I wanted to add several lines of TextViews to a Linear Layout and calculate text size
to fill it with the text.
It took me several hours researching and I got it.
Is very complicated in Android calculate text size because there are several screens with
different dpi.
I duckduckgoed several hours without success.
I found the solution from this way:
final float scaledDensity_sp = getResources().getDisplayMetrics().scaledDensity;
final float widthPixels = getResources().getDisplayMetrics().widthPixels;
float sp = ((widthPixels / scaleI found the solution from this way:
final float scaledDensity_sp = getResources().getDisplayMetrics().scaledDensity;
final float widthPixels = getResources().getDisplayMetrics().widthPixels;
float sp = ((widthPixels / scaledDensity_sp) / text_len) * 2;
et.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
I tested in several screen's sizes (about ten) and works fine in all (portrait and landscape in most of them).
I used the same text_len (the largest) for all lines because I want the same text size for all.dDensity_sp) / text_len) * 2;
et.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
I tested in several screen's sizes (about ten) and works fine in all (portrait and landscape in most of them).
I used the same text_len (the longest) for all lines because I wanted the same text size for all.
Update 19th august 2022:
Today I updated my HUAWEI to EMUI 12.0.0 and I need to add:
// For EMUI 12.0.0
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{
sp--;
}
before
et.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
Would it be possible to get the width of a string based on its font?
So for example, if the font size is 40, and the string was "hi", could you do 2*40.
An example of this would be
startX = 260; startWidth = "Start".length()*getFont().getSize();
startY = getHeight()-startWidth-20;
startHeight = getFont().getSize();
It really depends on the type of font, whether it's monospaced or proportional. Most font now are proportional so you would not simply be able to calculate num_of_chars * n.
The gui interface you are using should give you the width of the container once it has been populated with the given text.
I am using this code to randomly change the color of my textView. I have a black background so sometimes these colors are hard to read.
int r = (rand.nextInt(176) * 80 ) / 256;
int g = (rand.nextInt(176) * 80 ) / 256;
int b = (rand.nextInt(176) * 80 ) / 256;
TextView pos = (TextView) view.findViewById(R.id.position);
pos.setText(((Integer)(position + 1)).toString());
pos.setTextSize(30);
pos.setTextColor(Color.argb(255,r,g,b));
TextView data = (TextView) view.findViewById(R.id.textOfTip);
data.setText(tipsList[position].toString());
data.setTextSize(24);
data.setTextColor(Color.argb(255,r,g,b));
My question is, how can i increase the brightness or luminous effect of the text color so they can read easily.
Best Regards
First, get your color in the format 0xAARRGGBB (example, solid red is 0xFFFF0000). Then, push it to the method colorToHSV. Next, change the L/V/B value (which vary slightly, but I think will be close enough for what you're doing). Lastly, call HSVToColor to get your new color in 0xAARRGGBB format again. There are then several ways to convert this to R,G,B values, which usually involve byte shifting.
Something like this:
int color = 0xFFFF0000;
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] = 0.2f;
color = Color.HSVToColor(hsv);
int[] rgb = new int[3];
MyColor.colorToRGB(color, rgb); // Your custom method
// The rgb array now contains your RGB colors.
Note: There is also RGBToHSV, which may come in handy.
I think there are some formulae for acheiving the brightness and luminous effct.This article gives you good understanding about it.
Formula to determine brightness of RGB color
I have found a good way to draw text in openGL by creating a Bitmap, drawing text on it, and making it into a texture.
I have screenCoordinates(320,~~480) and gameCoordinates(20,28), in which quads' dimensions are defined. Example constructor: GameObject(x, y, width, height, text).
To create the bitmap I create a Canvas with this size:
// get width and height in px (p is a Paint() object)
p.setTextSize(textSize * Main.getMainResources().getDisplayMetrics().density);
final int width = (int) p.measureText(text);
final int height = (int) (p.descent() + -p.ascent());
canvas.drawText(text, 0, -p.ascent(), p);
Create a bitmap of this, scaled to the next power of 2, and you have your texture-bitmap.
However the problem is that because the quad's dimensions are not adapted to the bitmap(text) dimensions, the text isn't very smooth. I have the possibility to change the quad's dimensions with quad.setDimensions(width, height), but what would be the size I'd have to give it, and also shouldn't I also have to worry about the textSize(sp) of the text i'm drawing?
This all is in 2D, and some simple math's might get involved, but how should I get the right dimensions?
I was making a Gauroud algorithm and when i had calculated point intensity on the edge I didn't know what to do with it. I tried to decide this problem like:
private int getPointRGB(double intensity)
{
float[] hsb=null;
double newCrRed;
double newCrGr;
double newCrBlue;
int nRGB;
//crRed, crGr, crBlue - primary components of edge RGB
newCrRed = intensity*crRed;
newCrGr = intensity*crGr;
newCrBlue = intensity*crBlue;
hsb = Color.RGBtoHSB((int)newCrRed, (int)newCrGr, (int)newCrBlue, null);
nRGB = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
return(nRGB);
}
am I right?
If none of the default color choosers are satisfactory, you can create your own custom chooser panel, as discussed in How to Use Color Choosers: Creating a Custom Chooser Panel. For example, you could implement the CIE 1976 color space, shown here.