Some Notes on Transformation Matrices

As you have seen in this chapter, matrices are key to transforming views and animations. We will now briefly explore some key methods of the Matrix class. These are the primary operations on a matrix:

matrix.reset();

matrix.setScale();

matrix.setTranslate()

matrix.setRotate();

matrix.setSkew();

The first operation resets a matrix to an identity matrix, which causes no change to the view when applied. setScale is responsible for changing size, setTranslate is responsible for changing position to simulate movement, and setRotate is responsible for changing orientation. setSkew is responsible for distorting a view.

You can concatenate matrices or multiply them together to compound the effect of individual transformations. Consider the following example, where ml, m2, and m3 are identity matrices:

m1.setScale();

m2.setTranlate()

m3.concat(m1,m2)

Transforming a view by ml and then transforming the resulting view with m2 is equivalent to transforming the same view by m3. Note that set methods replace the previous transformations, and that m3.concat(m1,m2) is different from m3.concat(m2,ml).

You have already seen the pattern used by preTranslate and postTranslate methods to affect matrix transformation. In fact, pre and post methods are not unique to translate, and you have versions of pre and post for every one of the set transformation methods. Ultimately, a preTranslate such as m1.preTranslate(m2) is equivalent to m1.concat(m2,ml)

In a similar manner, the method m1.postTranslate(m2) is equivalent to m1.concat(m1,m2)

By extension, the code matrix.setScale(interpolatedTime, interpolatedTime); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY);

is equivalent to

Matrix matrixPreTranslate = new Matrix(); matrixPreTranslate.setTranslate(-centerX, -centerY);

Matrix matrixPostTranslate = new Matrix(); matrixPostTranslate.setTranslate(cetnerX, centerY);

matrix.concat(matrixPreTranslate,matrix); matrix.postTranslate(matrix,matrixpostTranslate);

0 0