Android under a small summary of screen adaptation

Source: Internet
Author: User

Why do I need a screen fit? For this I do not say, the online processing method either lets you use a few sets of different resolutions of the picture, either write a few sets of layout files, or in the XML write dip (this still can), the front two kinds of feeling process workload is too big, by loading large picture of the optimization idea

Similarly to a small algorithm to implement this feature:

To test the code first:

Package Cn.marsxtu.screenadapter;import Android.os.bundle;import Android.app.activity;import Android.content.context;import Android.util.displaymetrics;import Android.util.log;import Android.view.Menu; public class Mainactivity extends Activity {private static final String TAG = "mainactivity"; @Overrideprotected void Oncre Ate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Getdisplaymetrics (mainactivity.this);} public static String Getdisplaymetrics (Context cx) {String str = "";D isplaymetrics dm = new Displaymetrics ();//Get Displaym Etrics Object method One//DM = Cx.getapplicationcontext (). Getresources (). Getdisplaymetrics ();//Get Displaymetrics object method Two (( Activity) (CX). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); int screenwidth = Dm.widthpixels;int ScreenHeight = dm.heightpixels;float Density = dm.density;float xdpi = dm.xdpi;float ydpi = dm.ydpi;str + = "the absolute w Idth: "+ string.valueof (screenwidth) +" pixels\n "; LOG.I (TAG,STR); str + = "theAbsolute Heightin: "+ string.valueof (screenheight) +" pixels\n "; LOG.I (TAG,STR); str + = "The logical density of the display.:" + string.valueof (density) + "\ n"; LOG.I (TAG,STR); str + = "X dimension:" + string.valueof (xdpi) + "Pixels per inch\n"; LOG.I (TAG,STR); str + = "Y dimension:" + string.valueof (ydpi) + "Pixels per inch\n"; LOG.I (TAG,STR); return str;}}



Print the following information:

09-14 20:01:43.327:i/mainactivity (1384): the absolute width:480pixels
09-14 20:01:43.327:i/mainactivity (1384): the absolute width:480pixels
09-14 20:01:43.327:i/mainactivity (1384): the absolute heightin:800pixels
09-14 20:01:43.339:i/mainactivity (1384): the absolute width:480pixels
09-14 20:01:43.339:i/mainactivity (1384): the absolute heightin:800pixels
09-14 20:01:43.339:i/mainactivity (1384): The logical density of the display.:1.5
09-14 20:01:43.339:i/mainactivity (1384): the absolute width:480pixels
09-14 20:01:43.339:i/mainactivity (1384): the absolute heightin:800pixels
09-14 20:01:43.339:i/mainactivity (1384): The logical density of the display.:1.5
09-14 20:01:43.339:i/mainactivity (1384): X dimension:240.0pixels per inch
09-14 20:01:43.343:i/mainactivity (1384): the absolute width:480pixels
09-14 20:01:43.343:i/mainactivity (1384): the absolute heightin:800pixels
09-14 20:01:43.343:i/mainactivity (1384): The logical density of the display.:1.5
09-14 20:01:43.343:i/mainactivity (1384): X dimension:240.0pixels per inch
09-14 20:01:43.343:i/mainactivity (1384): Y dimension:240.0pixels per inch
09-14 20:01:43.539:d/libegl (1384): loaded/system/lib/egl/libegl_emulation.so
09-14 20:01:43.551:d/(1384): Hostconnection::get () New Host Connection established 0xb7d17680, Tid 1384
09-14 20:01:43.571:d/libegl (1384): loaded/system/lib/egl/libglesv1_cm_emulation.so
09-14 20:01:43.571:d/libegl (1384): loaded/system/lib/egl/libglesv2_emulation.so
09-14 20:01:43.707:w/egl_emulation (1384): Eglsurfaceattrib not implemented
09-14 20:01:43.751:d/openglrenderer (1384): Enabling Debug mode 0
09-14 20:01:43.859:d/openglrenderer (1384): texturecache::get:create texture (0XB7CD38E0): Name, size, msize = 1, 1048576 , 1048576
09-14 20:01:43.991:d/openglrenderer (1384): texturecache::get:create texture (0XB7CEAD10): Name, size, msize = 2, 5184, 10 53760
09-14 20:01:44.119:d/openglrenderer (1384): texturecache::get:create texture (0xb7d0fa70): Name, size, msize = 4, 20736, 1 074496
09-14 20:01:44.159:d/openglrenderer (1384): texturecache::get:create texture (0xb7cce840): Name, size, msize = 6, 2304, 10 76800
09-14 20:06:09.639:d/openglrenderer (1384): Texturecache::flush:target size:646080
09-14 20:06:09.639:d/openglrenderer (1384): Texturecache::callback:name, removed size, msize = 1, 1048576, 28224

Scene

Match the 1280*720 apk to the 800*480 device


First, when the program starts, get the real resolution of the screen, and then calculate the corresponding zoom ratio
Displaymetrics dm = new Displaymetrics ();
DM = Getapplicationcontext (). Getresources (). Getdisplaymetrics ();
Constant.screen_width = Dm.widthpixels;
Constant.screen_height = Dm.heightpixels;
if (Constant.screen_width > Constant.screen_height) {
Constant.screen_height = Dm.widthpixels;
Constant.screen_width = Dm.heightpixels;
}
Constant.xscale = (float) constant.screen_width/constant.camera_width;
Constant.yscale = (float) constant.screen_height/constant.camera_height;




The image is scaled as follows:


/**
Get the bitmap after scaling
*/
Protected Bitmap Scalebitmap (Bitmap Bitmap) {
Bitmap newbitmap = null;
if (Constant.xscale! = 1 | | Constant.yscale! = 1) {
Bitmapwidth = (int) (Bitmap.getwidth () * Constant.xscale);
Bitmapheight = (int) (Bitmap.getheight () * Constant.yscale);
Newbitmap = Bitmap.createscaledbitmap (Bitmap, (int) (bitmapwidth), (int) (bitmapheight), true);
Bitmap.recycle ();
return newbitmap;
}
return bitmap;
}


Android under a small summary of screen adaptation

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: 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.