Android adjusts screen resolution

Source: Internet
Author: User
Android can be set to adjust the zoom ratio with the window size and set the size of the fixed window.
Control the surface in the surfaceholder class.
In Android screen resolution, displaymetics provides a class

The displaymetrics class under the andorid. util package provides general information about the display, such as the display size, resolution, and font.

To obtain displaymetrics members, first Initialize an object as follows:
Displaymetrics metrics = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (metrics );
String STR = metrics. tostring ();
String STR = "Screen Resolution:" + metrics. widthpixels
+ "*" + Metrics. heightpixels;
Textview. settext (STR); // display

The following code in CTS shows the display of different units:
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 );

There are several units of DIP, DP, PX, and sp. First of all:
Dip: Device Independent pixels (device independent pixel). Different devices have different display effects,
This is related to the hardware of the device. We recommend that you use this feature to support WVGA, hvga, and qvga without pixels.
Px: pixels (pixels). Different devices have the same display effect. Generally, we use hvga to represent 320x480 pixels, which is usually used.
PT: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use;
SP: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts. According to Google's suggestion,
It is best to use SP as the unit for the font size of textview.

Programmers usually design computer user interfaces in pixels, but if the display resolution changes (higher ),
The previous application interface is reduced accordingly, so it is necessary to use a measurement unit unrelated to the resolution to solve this problem.

Android supports all the following units:
Px (pixel): the point on the screen.
In (INCHES): the unit of length.
Mm (mm): the unit of length.
Pt (lbs): 1/72 inch.
DP (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.
Dip: it is the same as DP and is mostly used in Android/ophone examples.

DP is dip. This is basically similar to sp. You can use DP or SP to specify attributes such as length and height. However, if
Set the font. SP is required. DP is not related to density. SP is not only related to density, but also to scale.
Therefore, we recommend that you always use SP
The unit of text size. Dip is used as the unit of other elements. Of course, you can also consider using a vector image instead of a bitmap.

Two solutions are included:
When decoding the image display, the Set Density will affect the display effect.

Default value defined in displaymetrics. Java:
* 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;

1. If the image is decoded in high pixels but the display effect is poor, modify density_default to view the effect.
Public static final int density_default = density_xhigh;

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 );

2. If the image is enlarged at different display frequencies, it may be related to webview, which is also affected by density.

* Enum for specifying the webview's desired density.
* Far makes 100% looking like in 240 DPI
* Medium makes 100% looking like in 160 DPI
* Close makes 100% looking like in 120 DPI
Public Enum zoomdensity {
FAR (150), // 240 DPI
Medium (100), // 160 DPI
Close (75); // 120 DPI
Zoomdensity (INT size ){
Value = size;
Int value;

Is it possible to dynamically set the screen resolution?
Int ddensity = getresources (). getdisplaymetrics (). densitydpi;
Websettings. zoomdensity zdensity = websettings. zoomdensity. medium;
Switch (ddensity ){
Case displaymetrics. density_low:
Zdensity = websettings. zoomdensity. close;

Case displaymetrics. density_medium:
Zdensity = websettings. zoomdensity. medium;

Case displaymetrics. density_high:
Zdensity = websettings. zoomdensity. far;

Websettings. setdefazoom zoom (zdensity );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.