Different android versions have different resolutions, and the length and width of the same image are different! Obtain the screen width and height.

Source: Internet
Author: User

Use canvas today. when the drawBitmap method draws an image, it finds a problem. When it is made in version 2.1, It is center-centered. When it is used in version 2.2, it finds the same image, but it cannot be centered. After reading the code, I used the same image and thought about it. It is estimated that it has something to do with the version. So I tested it and found the same test code, in version 2.1, the screen length and width are 533*320, and the width is 480*320. In version 2.2, the screen length and width are 800*480. I suddenly realized this result, because in my code, the width and height of the screen are unified to 480*320, the coordinates of an image are determined by the width and height of the screen and the image. The code is roughly as follows: Paint paint = new Paint (); // brush int screenWidth = 480; // screen width int screenHeight = 320; // screen height Bitmap bm = BitmapFactory. decodeResource (activity. getResources (), R. drawable. bnkjs); // picture currentX = screenWidth/2-bm.getWidth ()/2; // picture X coordinate currentY = screenHeight/2-bm.getHeight ()/2; // picture Y coordinate canvas. drawBitmap (bm, currentX, currentY, paint); then, the screen width and height are changed to get the width and height of the current screen, and then the screen can be normally centered. The Code is as follows: paint paint = new Paint (); // Paint int screenWidth = wm. getdefadisplay display (). getWidth (); // screen width int screenHeight = wm. getdefadisplay display (). getHeight (); // screen height Bitmap bm = BitmapFactory. decodeResource (activity. getResources (), R. drawable. bnkjs); // picture currentX = screenWidth/2-bm.getWidth ()/2; // picture X coordinate currentY = screenHeight/2-bm.getHeight ()/2; // picture Y coordinate canvas. drawBitmap (bm, currentX, currentY, paint );

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.