Android 可设置为随着窗口大小调整缩放比例及设定fixed的窗口大小。对于surface的控制在SurfaceHolder类中进行
而Android 屏幕分辩率中已经有一个类DisplayMetics提供Andorid.util 包下的DisplayMetrics 类提供了一种关于显示的通用信息,如显示大小,分辨率和字体。
为了获取DisplayMetrics 成员,首先初始化一个对象如下:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
String str = metrics.toString();
String str = "屏幕分辨率为:" + metrics.widthPixels
+" * "+metrics.heightPixels;
textview.setText(str); //显示
在CTS中有如下一段代码可以体现出不同的单位显示情况:
public void testAccessTextSize() { DisplayMetrics metrics = mActivity.getResources().getDisplayMetrics(); mTextView = new TextView(mActivity); mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f); assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20f, metrics), mTextView.getTextSize(), 0.01f); mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20f); assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20f, metrics), mTextView.getTextSize(), 0.01f); mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f); assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20f, metrics), mTextView.getTextSize(), 0.01f); // setTextSize by default unit "sp" mTextView.setTextSize(20f); assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20f, metrics), mTextView.getTextSize(), 0.01f); mTextView.setTextSize(200f); assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 200f, metrics), mTextView.getTextSize(), 0.01f); }
/** * Standard quantized DPI for low-density screens. */ public static final int DENSITY_LOW = 120; /** * Standard quantized DPI for medium-density screens. */ public static final int DENSITY_MEDIUM = 160; /** * Standard quantized DPI for high-density screens. */ public static final int DENSITY_HIGH = 240; /** * Standard quantized DPI for extra-high-density screens. */ public static final int DENSITY_XHIGH = 320; /** * The reference density used throughout the system. */ public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
Options opts = new Options(); opts.inScaled = false; opts.inSampleSize = 1; opts.inScreenDensity = DisplayMetrics.DENSITY_HIGH; opts.inPreferredConfig = Bitmap.Config.ARGB_8888; mBitmapbg = BitmapFactory.decodeResource(this.getResources(), R.drawable.bj, opts); BitmapDrawable bd = new BitmapDrawable(mBitmapbg);
/** * Enum for specifying the WebView's desired density. * FAR makes 100% looking like in 240dpi * MEDIUM makes 100% looking like in 160dpi * CLOSE makes 100% looking like in 120dpi */ public enum ZoomDensity { FAR(150), // 240dpi MEDIUM(100), // 160dpi CLOSE(75); // 120dpi ZoomDensity(int size) { value = size; } int value; }
int dDensity = getResources().getDisplayMetrics().densityDpi ; WebSettings.ZoomDensity zDensity = WebSettings.ZoomDensity.MEDIUM ; switch(dDensity) { case DisplayMetrics.DENSITY_LOW : zDensity = WebSettings.ZoomDensity.CLOSE; break; case DisplayMetrics.DENSITY_MEDIUM: zDensity = WebSettings.ZoomDensity.MEDIUM; break; case DisplayMetrics.DENSITY_HIGH: zDensity = WebSettings.ZoomDensity.FAR; break ; } webSettings.setDefaultZoom(zDensity);