Summary on screen adaptation in android

Source: Internet
Author: User

Summary on screen adaptation in android

Why screen adaptation? For this reason, I will not talk about it. The online processing method either allows you to use several sets of images with different resolutions, or write several sets of layout files, either write dip in xml (this is acceptable). The first two feelings are that the process is too heavy, and the optimization idea of loading large images is

This function is also implemented for a small algorithm ..

First test the Code:

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 onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); getDisplayMetrics (MainActivity. this);} public static String getDisplayMetrics (Context cx) {String str = ""; DisplayMetrics dm = new DisplayMetrics (); // method 1 for obtaining the DisplayMetrics object // dm = cx. getApplicationContext (). getResources (). getDisplayMetrics (); // get DisplayMetrics object method 2 (Activity) cx ). getWindowManager (). getdefadisplay display (). 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 width:" + String. valueOf (screenWidth) + "pixels \ n"; Log. I (TAG, str); str + = "The absolute 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 ;}}



The printed information is as follows:

09-14 20:01:43. 327: I/MainActivity (1384): The absolute width: 480 pixels
09-14 20:01:43. 327: I/MainActivity (1384): The absolute width: 480 pixels
09-14 20:01:43. 327: I/MainActivity (1384): The absolute heightin: 800 pixels
09-14 20:01:43. 339: I/MainActivity (1384): The absolute width: 480 pixels
09-14 20:01:43. 339: I/MainActivity (1384): The absolute heightin: 800 pixels
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: 480 pixels
09-14 20:01:43. 339: I/MainActivity (1384): The absolute heightin: 800 pixels
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.0 pixels per inch
09-14 20:01:43. 343: I/MainActivity (1384): The absolute width: 480 pixels
09-14 20:01:43. 343: I/MainActivity (1384): The absolute heightin: 800 pixels
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.0 pixels per inch
09-14 20:01:43. 343: I/MainActivity (1384): Y dimension: 240.0 pixels 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,104 8576
09-14 20:01:43. 991: D/OpenGLRenderer (1384): TextureCache: get: create texture (0xb7cead10): name, size, mSize = 2, 5184,105 3760
09-14 20:01:44. 119: D/OpenGLRenderer (1384): TextureCache: get: create texture (0xb7d0fa70): name, size, mSize = 4, 20736,107 4496
09-14 20:01:44. 159: D/OpenGLRenderer (1384): TextureCache: get: create texture (0xb7cce840): name, size, mSize = 6, 2304,107 6800
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,282 24

Scenario

Adapt 1280*720 apk to 800*480 Devices


// Obtain the actual screen resolution when the program starts, and then calculate the corresponding scaling 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;




Scale the image below:


/**
The scaled bitmap is obtained.
*/
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;
}


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.