ImageView of Android UI realizes image rotation and scaling

In this article, we will introduce the use of ImageView control. ImageView is mainly used to display pictures, which can be enlarged, reduced and rotated. The Android: sacletype property specifies how the ImageView control displays pictures. For example, center means that the image is displayed in the center of the ImageView control without scaling. If it is set to fitcenter, it means that the image is scaled to an appropriate position and in the center of the ImageView control. First, we develop a simple case to realize the zoom in, zoom out and rotation of the picture: first look at the implementation effect: zoom screenshot 1:

Zoom screenshot 2:

Rotating screenshot 1:

Rotating screenshot 2:

We all need to use android.graphics.matrix to zoom and rotate pictures. The matrix class holds a 3x3 matrix for transforming coordinates. Matrix does not have a constructor, so it must be explicitly initialized using either reset() - to construct an identity matrix, or one of the set..() functions (e.g. setTranslate,setRotate,etc.).

In this example, the setrotate method of android.graphics.matrix is used to set the rotation angle. The following is an introduction to this method in the API:

Source code: mainactivity.java

Layout file main.xml

Finally, to rotate the picture displayed in ImageView, please select a picture that conforms to the 3 * 3 matrix of the matrix, otherwise an error will be caused if the screen width exceeds during the rotation. In this example, a square picture is selected. If it is a rectangle, it is recommended to make code logic judgment. The above is about the implementation effect of matrix. I hope it will be helpful to everyone's learning.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>