Utils.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.epson.mobilephone.common.guide;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Point;
  5. import android.util.TypedValue;
  6. import android.view.Display;
  7. /**
  8. * 像素转换工具类
  9. */
  10. public class Utils {
  11. public static int LAYOUT_MEARGIN = 32;
  12. public static int WEBVIEW_300DP_SIZE = 300;
  13. public static int WEBVIEW_500DP_SIZE = 500;
  14. public static int getWebViewDP(Activity activity, Context context) {
  15. float pxtodp = pxtodp(getDisplaySize(activity).x, context);
  16. float pxtodp2 = pxtodp(getDisplaySize(activity).y, context);
  17. if (pxtodp2 == 0.0f || pxtodp == 0.0f) {
  18. return WEBVIEW_300DP_SIZE;
  19. }
  20. if (pxtodp2 < pxtodp) {
  21. int i = (int) ((pxtodp2) * 0.7d);
  22. int i2 = WEBVIEW_500DP_SIZE;
  23. return i < LAYOUT_MEARGIN + i2 ? i : i2;
  24. }
  25. int i3 = (int) ((pxtodp) * 0.7d);
  26. int i4 = WEBVIEW_500DP_SIZE;
  27. return i3 < LAYOUT_MEARGIN + i4 ? i3 : i4;
  28. }
  29. public static Point getDisplaySize(Activity activity) {
  30. Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
  31. Point point = new Point();
  32. defaultDisplay.getSize(point);
  33. return point;
  34. }
  35. public static int pxtodp(int i, Context context) {
  36. return (int) (i/ context.getResources().getDisplayMetrics().density);
  37. }
  38. public static int dptopx(int i, Context context) {
  39. return (int) TypedValue.applyDimension(1, i, context.getResources().getDisplayMetrics());
  40. }
  41. }