Android Adaptation Program summary (ii)

Source: Internet
Author: User

The main record for this section is to get the various parameters related to screen adaptation from your code:

Java code such as the following

public class Screenutil {/** * NOTE: * Only activity is able to use Getwindowmanager. Otherwise, you should use * context.getresources (). Getdisplaymetrics () to get *//** * Get displaymetric related parameters * @param Context * @return */public STA Tic String Getmetricparams (Activity context) {displaymetrics dm = new Displaymetrics (); Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); return "Density:" +dm.density+ ";d ensitydpi:" +dm.densitydpi+ "; Height:" + Dm.heightpixels+ "; width:" +dm.widthpixels+ "; scaleddensity:" +dm.scaleddensity+ "; xdpi:" +dm.xdpi+ "; ydpi:" +DM.YDPI;} /** * Get screen size in pixels * @param context * @return */public static String getscreensizeininpixels (Activity context) {DISPLAYMETR ICS DM = new Displaymetrics (); Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);d ouble heightininpixels = (double) dm.heightpixels;double widthininpixels = (double) Dm.widthpixels;return "High:" +heightininpixels+ "width:" + widthininpixels+ "unit (pixel)";} /** * Get screen size in inches * Calculate screen size should use exact density: xdpi ydpi to calculate * Use normalized density: densitydpi is wrong. It is a fixed value, * 120 160 240 320 480, according to DP compute pixels to use it * @param context * @return */public static String Getscreensizeininch (Activity context) {displaymetrics DM = new Displaymetrics (); Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);d ouble heightininch = (double) Dm.heightpixels/(double) dm.ydpi;double widthininch = (double) dm.widthpixels/(double) dm.xdpi;double Scrrensizeininch = math.sqrt (heightininch*heightininch+ widthininch*widthininch); return "High:" +heightininch+ "width:" + widthininch+ "Size:" +scrrensizeininch+ "unit (Inch)";} /** * Get screen size in DP * @param context * @return */public static String GETSCREENSIZEININDP (Activity context) {Displaymetrics DM = new Displaymetrics (); Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); float HEIGHTININDP = Px2dip ( (context) context, (float) dm.heightpixels); float WIDTHININDP = Px2dip ((context) context, (float) dm.widthpixels); Return "High:" +heightinindp+ "width:" +widthinindp+ "unit (DP)";} /** * DP to PX * @param context * @param dpvalue * @return */public static int dip2px (context context, FloaT dpvalue) {final float scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (dpvalue*scale+0.5f);} /** * px to DP * @param context * @param pxvalue * @return */public static int Px2dip (context context, float Pxvalue) {final F Loat scale = Context.getresources (). Getdisplaymetrics (). Density;return (int) (pxvalue/scale+0.5f);}}

Settings FOR XML:

<support-screens
Android:anydensity = "true"
Android:largescreens = "true"
Android:normalscreens = "true"
Android:resizeable = "true"
Android:smallscreens = "true
Android:xlargescreens = "true" >
Android:anydensity = "true" when. Applications are installed at different densities of terminals,
The program will load the resources in xxhdpi, xhdpi, hdpi, mdpi,ldpi directory, respectively.




Android Adaptation Program summary (ii)

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: info-contact@alibabacloud.com 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.