| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package com.xzjmyk.pm.activity.util;
- import android.app.Activity;
- import android.content.Context;
- import android.view.WindowManager;
- public class DisplayUtil {
- public static final int voice_view_max_width = 165;// dp
- public static final int voice_view_min_width = 30;// dp
- public static final float voice_max_length = 30 ;// 声音最长可以表现为多少毫秒(实际本程序是60s,但是如果这里是60s的话,当时间很短,就没啥差别
- public static int dip2px(Context context, float dpValue) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dpValue * scale + 0.5f);
- }
- public static int px2dip(Context context, float pxValue) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (pxValue / scale + 0.5f);
- }
- public static int getVoiceViewWidth(Context context, int seconds) {
- if (seconds >= voice_max_length) {
- return dip2px(context, voice_view_max_width);
- }
- final int dpLen = (int) ((seconds / voice_max_length) * (voice_view_max_width - voice_view_min_width)) + voice_view_min_width;
- return dip2px(context, dpLen);
- }
- /**
- * 将px值转换为sp值,保证文字大小不变
- *
- * @param pxValue
- * @param (DisplayMetrics类中属性scaledDensity)
- * @return
- */
- public static int px2sp(Context context, float pxValue) {
- final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
- return (int) (pxValue / fontScale + 0.5f);
- }
- /**
- * 设置添加屏幕的背景透明度
- *
- * @param bgAlpha
- */
- public static void backgroundAlpha(Activity activity, float bgAlpha) {
- WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
- lp.alpha = bgAlpha; //0.0-1.0
- activity.getWindow().setAttributes(lp);
- }
- }
|