DisplayUtil.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.xzjmyk.pm.activity.util;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.view.WindowManager;
  5. public class DisplayUtil {
  6. public static final int voice_view_max_width = 165;// dp
  7. public static final int voice_view_min_width = 30;// dp
  8. public static final float voice_max_length = 30 ;// 声音最长可以表现为多少毫秒(实际本程序是60s,但是如果这里是60s的话,当时间很短,就没啥差别
  9. public static int dip2px(Context context, float dpValue) {
  10. final float scale = context.getResources().getDisplayMetrics().density;
  11. return (int) (dpValue * scale + 0.5f);
  12. }
  13. public static int px2dip(Context context, float pxValue) {
  14. final float scale = context.getResources().getDisplayMetrics().density;
  15. return (int) (pxValue / scale + 0.5f);
  16. }
  17. public static int getVoiceViewWidth(Context context, int seconds) {
  18. if (seconds >= voice_max_length) {
  19. return dip2px(context, voice_view_max_width);
  20. }
  21. final int dpLen = (int) ((seconds / voice_max_length) * (voice_view_max_width - voice_view_min_width)) + voice_view_min_width;
  22. return dip2px(context, dpLen);
  23. }
  24. /**
  25. * 将px值转换为sp值,保证文字大小不变
  26. *
  27. * @param pxValue
  28. * @param (DisplayMetrics类中属性scaledDensity)
  29. * @return
  30. */
  31. public static int px2sp(Context context, float pxValue) {
  32. final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
  33. return (int) (pxValue / fontScale + 0.5f);
  34. }
  35. /**
  36. * 设置添加屏幕的背景透明度
  37. *
  38. * @param bgAlpha
  39. */
  40. public static void backgroundAlpha(Activity activity, float bgAlpha) {
  41. WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
  42. lp.alpha = bgAlpha; //0.0-1.0
  43. activity.getWindow().setAttributes(lp);
  44. }
  45. }