In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what is the use of preTranslate() and postTranslate() in Matrix. The introduction in the article is very detailed and has certain reference value. Interested friends must read it!
achieve
Let's take a look at the renderings:
You can see one original image and three processed images. The top and bottom of the left and right images are not symmetrical. Only the middle image is symmetrical. The result image I hope to get is the middle image. Why are there three different rotating pictures? The reason is that the rotation center is different.
Let's look at the source code first:
public class MainActivity extends AppCompatActivity { private ImageView view2,view3,view4; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view2 = (ImageView) findViewById(R.id.iv2); view3 = (ImageView) findViewById(R.id.iv3); view4 = (ImageView) findViewById(R.id.iv4); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.i004); /** * Rotation center is (0,0) */ Matrix matrix = getMatrix(); Bitmap bit = getBitmap(bitmap, matrix); view2.setImageBitmap(bit); /** * Rotation center is (0,height/2) */ matrix = getMatrix(); matrix.preTranslate(0, -bitmap.getHeight() / 2); matrix.postTranslate(0, bitmap.getHeight() / 2); bit = getBitmap(bitmap, matrix); view3.setImageBitmap(bit); /** * Rotation center is (0,height) */ matrix = getMatrix(); matrix.preTranslate(0, -bitmap.getHeight()); matrix.postTranslate(0,bitmap.getHeight()); bit = getBitmap(bitmap, matrix); view4.setImageBitmap(bit); } private Bitmap getBitmap(Bitmap bitmap, Matrix matrix) { return Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true); } @NonNull private Matrix getMatrix(){ Matrix matrix = new Matrix(); Camera camera = new Camera(); camera.save(); camera.rotateY(45); camera.getMatrix(matrix); camera.restore(); return matrix; }}
Picture on the left:
From the source code we can see that the image on the left does not do any processing on Matrix, so the rotateY() method of Camera rotates with the y axis, and the rotation center is (0,0), as shown in the following figure:
Because the rotation center is not on the symmetry point of the image, the result is that the image is asymmetrical up and down.
Middle picture:
The middle picture is symmetrical, so how do you get it? According to the above, because the center of rotation is (0,0) is not on the symmetry point of the image, then we only need to make the center of rotation on the symmetry point, which means preTranslate() and postTranslate() methods, these two methods can achieve what we call the change of the center of rotation, in fact, it has not changed, just the image in the form of a matrix to achieve the same effect as the change of the center of rotation.
matrix.preTranslate(0, -bitmap.getHeight());matrix.postTranslate(0,bitmap.getHeight());
These two lines of code do exactly that. The preTranslate method is used to move the image up half the height of the image between rotations, so that the image is symmetrical about the x axis, and then rotate the transformation. The postTranslate method is to move the image down half the height of the image after the transformation, that is, back to the original position, so that the image is displayed symmetrically. The principle is also very simple, the rotation center is still (0,0), but we move the image, so that when the rotation transformation will get symmetrical results.
The above is "What is the use of preTranslate() and postTranslate() in Matrix" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.