Use matrix for image transformation to display images in full screen (screen adaptive)

Source: Internet
Author: User

Matrix is a rectangle tool class provided by Android. It is mainly used with other APIs (for example, Canvas has a drawBitmap () method that uses Matrix parameters) to control the translation, rotation, scaling, and tilt conversion of graphics or View components. Here, I will only give a brief introduction to this article. The reason why this article is used only to record that Matrix has been used in the development process has solved a headache that I have encountered, the solution is to display images of different sizes in full screen and complete display on screens of different sizes. The specific solution is to use Matrix to control the setScale (float sx, float sy. There may be other solutions to this problem. If you can tell me, thank you first.

First, we will briefly introduce the methods provided by Matrix for controlling translation, rotation, and scaling:

SetTranslate (float dx, float dy): used to control the image translation.

SetSkew (float kx, float ky, float px, float py): controls the tilt of the Matrix with px and py as the axis, where, kx and ky are the skewed distances in the X and Y directions.

SetSkew (float kx, float ky): Controls Matrix skew. Kx and ky are the skewed distances in the X and Y directions.

SetRoatate (float degrees): controls the Matrix to rotate, and degrees indicates the rotation angle.

SetRotate (float degrees, flaot px, float py): Set to rotate with px and py as the axis, and degrees as the rotation angle.

SetScale (float sx, float sy): sets the Matrix for scaling. sx and sy control the scaling ratio in the XY direction.

SetScale (flaot sx, float sy, float px, float py): sets the axis to scale with px and py, and sx/sy controls the scaling ratio in the XY direction.

The steps for graph transformation using Matrix are also simple: first, get the Matrix object, and then call the Matrix method to set the transformation mode, finally, this Matrix object is used together with other Android APIs to perform graphic Transformation (for example, by calling the drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint) of Canvas) method to draw the transformed image on the Interface). In this way, the Matrix is used with other APIs to complete the graphic transformation.

Here, I will briefly introduce the solution to the problem at that time. In fact, it is very simple. to display the picture in full screen mode on the screen, you must call setScale (float sx, float sy) method. The key is to set two parameters in the setScale () method. These two parameters are mainly used to control the scaling ratio. 1.0 indicates no scaling. When it is another number, then, the corresponding values are reduced or zoomed in. Based on this, the screen width and height can be separately compared to the width and height of the slice, and the two ratios can be used as two parameters of setScale, you can display an image in full and full screen mode.

Based on this principle, the program interface can achieve screen self-adaptation. Of course, you can also set the program to full screen display instead of a specified proportion of full screen display.

Of course, the full screen display of images in this way is incorrect. You can use the draw9patch tool to make screen adaptation without distortion.

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.