Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set unit for Paint.setTextSize()

Tags:

java

android

view

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.

like image 386
Yverman Avatar asked Jun 17 '10 13:06

Yverman


2 Answers

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

like image 28
Alex Volovoy Avatar answered Oct 14 '22 06:10

Alex Volovoy


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()); 
like image 89
Felipe Caldas Avatar answered Oct 14 '22 08:10

Felipe Caldas