Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Typed value class

Tags:

android

Most of the time class's name is intuitive enough so as to make what it is used for. Like BufferedReader,InputStreamReader etc but Since recently I've started learning android, This class, android.util.TypedValue is taught to be used for converting Dip to pixels (Hopefully it has many other uses, of which I am not aware though). I am really having trouble with getting along with its name and work. Its use is like

int px = (int)TypedValue.applyDimension(TypedValue.Complex_Unit_Dip,200,r.getDisplayMetrices());

In Documentation it says, Its used for storing Dynamically typed data value, I can't see anything being typed :P. So Please I would be really thankful, If any one would get me through this.

PS: I am a Student :)

like image 969
Salim Shamim Avatar asked Dec 14 '22 11:12

Salim Shamim


1 Answers

TypedValue holds raw representations of typed data (ex. floats, dimensions, etc.) loaded from compiled Android XML resources. The class is primarily used as an intermediary between the resources framework and the places where application developers need to use values stored in resources.

For example, you may want to resolve the value of the dimension android.R.dimen.app_icon_size. This value is stored in a packed representation of the resource table, which includes data from the framework's values.xml and dimens.xml files among others. To retrieve the data in a usable form, you would do something like this:

Resources res = getResources;
DisplayMetrics metrics = res.getDisplayMetrics();
TypedValue outValue = new TypedValue();

// Loads the raw data into the typed value
res.getValue(android.R.dimen.app_icon_size, outValue, true);

// Converts the raw data into a usable value
int appIconSizePx = outValue.getDimension(metrics);

Like many Android classes, the best way to familiarize yourself with how the class works is to look through the source code.

like image 186
alanv Avatar answered Jan 13 '23 22:01

alanv