Matrix矩阵连接变换应用的一个错误

RectF rf = new RectF(100, 100, 300, 300);
Matrix m = new Matrix();
float centerX = 500;
float centerY = 500;
float scale = 1.5f;
m.preScale(scale, scale);
m.mapRect(rf);
float cx = rf.centerX();
float cy = rf.centerY();
m.postTranslate(centerX - cx, centerY -cy);
//m.setTranslate(centerX - cx, centerY - cy);
m.mapRect(rf);


上面这段代码目的是对原矩形以指定点(500,500)坐缩放,使用postTranslate怎么也得不到正确的值,但是使用setTranslate之后结果正确。

原因:第一次mapRect为了得到原点缩放的中心已经对rf作了变换。而postTranslate是一个连接操作,所以整个过程的矩阵变换为:preScale -> preScale -> postTranslate,相当于对原矩形作了两次preScale操作。

原文地址:https://www.cnblogs.com/fordreamxin/p/4732237.html