DisplayUtil.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. * 设置添加屏幕的背景透明度
  26. *
  27. * @param bgAlpha
  28. */
  29. public static void backgroundAlpha(Activity activity, float bgAlpha) {
  30. WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
  31. lp.alpha = bgAlpha; //0.0-1.0
  32. activity.getWindow().setAttributes(lp);
  33. }
  34. }