What's the relationship between pixels and scaled pixels

android, user-interface

Solution

Here we go! I need to get the DisplayMetrics Density.

// Convert the dips to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);

Thank you all for pointing me in the right direction!

Problem

I used the layout editor in eclipse to mock up my ui layout and then I created the code to populate it dynamically and things are showing up as different sizes. The XML I use to add the star images looks like this: ``` <ImageView android:src="@drawable/star_gold" android:layout_height="22sp" android:layout_width="22sp" android:adjustViewBounds="true" android:layout_marginLeft="2sp" android:layout_marginRight="2sp" /> ``` and the Java code I use to do the same thing looks like this: ``` ViewGroup.MarginLayoutParams layout = new ViewGroup.MarginLayoutParams(22, 22); layout.setMargins(2, 0, 2, 0); ImageView star = new ImageView(mContext); star.setImageResource(R.drawable.star_gold); star.setLayoutParams(layout); star.setAdjustViewBounds(true); holder.Stars.addView(star); ``` When I run my app on the G1 everything lines up and looks fine, but when I run it on the Droid the stars that I add via Java are about half the size of the ones I add in my XML. What do I need to do to make sure everything scales appropriately? Edit: These are great responses, but I need to know how to do it from code. When I set the size it's in raw pixels, is there any way to set the size in dip's?

Original source

Related problems