[Android] Util: Add dp/px conversion functions

Edward Wang git at videolan.org
Sun Aug 12 04:28:02 CEST 2012


android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Fri Aug 10 21:25:34 2012 -0400| [1416c0175e24a5e9840bf709b3d378cc6b0e501f] | committer: Edward Wang

Util: Add dp/px conversion functions

These are a bit of a pain to find :(

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=1416c0175e24a5e9840bf709b3d378cc6b0e501f
---

 vlc-android/src/org/videolan/vlc/Util.java |   22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/Util.java b/vlc-android/src/org/videolan/vlc/Util.java
index 77d68ce..d02d45f 100644
--- a/vlc-android/src/org/videolan/vlc/Util.java
+++ b/vlc-android/src/org/videolan/vlc/Util.java
@@ -36,7 +36,11 @@ import java.util.Properties;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.net.Uri;
+import android.util.DisplayMetrics;
+import android.util.TypedValue;
+import android.view.Display;
 import android.view.View;
+import android.view.WindowManager;
 import android.widget.Toast;
 
 public class Util {
@@ -194,6 +198,24 @@ public class Util {
                 : R.drawable.background_item2);
     }
 
+    public static int convertPxToDp(int px) {
+        WindowManager wm = (WindowManager)VLCApplication.getAppContext().
+                getSystemService(Context.WINDOW_SERVICE);
+        Display display = wm.getDefaultDisplay();
+        DisplayMetrics metrics = new DisplayMetrics();
+        display.getMetrics(metrics);
+        float logicalDensity = metrics.density;
+        int dp = Math.round(px / logicalDensity);
+        return dp;
+    }
+
+    public static int convertDpToPx(int dp) {
+        return Math.round(
+                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
+                VLCApplication.getAppResources().getDisplayMetrics())
+                         );
+    }
+
     public static boolean isGingerbreadOrLater()
     {
         return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD;



More information about the Android mailing list