标签:
转载请注明出处:http://blog.csdn.net/devilnov/article/details/44835243
import android.content.pm.ActivityInfo; import android.view.Display; import android.view.Gravity; import android.view.Window; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; /** * 动态适配 * 屏幕长宽按比例缩放 * @author Administrator * */ public class UiScaleUtil { /** * pad版的缩放比例 * w 长边 * h 短边 */ public final static double PAD_SCALE_LONGGER = 0.9; public final static double PAD_SCALE_LONG = 0.8; public final static double PAD_SCALE_SHORT = 0.6; /** * 对dialog的 w 进行动态缩放 * @param window * @param orientation */ public static void scaleDialog(Window window, int orientation){ WindowManager m = window.getWindowManager(); Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用 WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值 double scaleW = 1; if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { scaleW = UiScaleUtil.PAD_SCALE_SHORT; } else { scaleW = UiScaleUtil.PAD_SCALE_LONG; } int frameWidth = (int) (d.getWidth() * scaleW); if (frameWidth < 600) { frameWidth = (int) (d.getWidth() * UiScaleUtil.PAD_SCALE_LONGGER); } p.width = (int) (frameWidth / 2); // 宽度设置为屏幕缩放比例的一半,即收银台宽度的一半 window.setAttributes(p); window.setGravity(Gravity.CENTER); } /** * 对activity的 w 和 h 进行动态缩放 * @param window * @param orientation */ public static void scaleActivity(Window window, int orientation){ WindowManager m = window.getWindowManager(); Display d = m.getDefaultDisplay(); //为获取屏幕宽、高 LayoutParams p = window.getAttributes(); //获取对话框当前的参数值 double scaleH = 1; double scaleW = 1; if(orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ scaleH = UiScaleUtil.PAD_SCALE_LONG; scaleW = UiScaleUtil.PAD_SCALE_SHORT; }else{ scaleH = UiScaleUtil.PAD_SCALE_SHORT; scaleW = UiScaleUtil.PAD_SCALE_LONG; } p.height = (int) (d.getHeight() * scaleH); //高度设置为屏幕实际宽度的比例 p.width = (int) (d.getWidth() * scaleW); //宽度设置为屏幕实际宽度的比例 if(d.getHeight() > d.getWidth()){ if(p.width < 600){ p.width = (int) (d.getWidth() * UiScaleUtil.PAD_SCALE_LONGGER); } }else if(d.getHeight() < d.getWidth()){ if(p.height < 600){ p.height = (int) (d.getHeight() * UiScaleUtil.PAD_SCALE_LONGGER); } } p.alpha = 1.0f; //设置本身透明度 p.dimAmount = 0.0f; //设置黑暗度 window.setAttributes(p); //设置生效 window.setGravity(Gravity.CENTER); //设置靠右对齐 } }
标签:
原文地址:http://blog.csdn.net/devilnov/article/details/44835243