Android. graphics. the Matrix class provides two-dimensional coordinate transformation (translation, rotation, scaling, etc.). The common method of using Matrix is to directly specify the value of the required Matrix, if you know the coordinates of the vertices before and after the coordinate transformation, you can calculate the corresponding transformation matrix based on the ing relationship of these vertices. However, Matrix provides a more concise method:
Public boolean setPolyToPoly (float [] src, int srcIndex, float [] dst, int dstIndex, int pointCount)
Provides coordinate pairs before (src) and after (dst) the specified transformation. Matrix automatically helps you calculate the Matrix for implementing these coordinate transformations. The format of each coordinate is [x0, y0, x1, y1...]. Two float values represent one point.
This example uses one point (offset transformation), two points (rotation/scaling), three points (rotation/shear), and four points (perspective transformation) use polyToPoly to calculate the corresponding Matrix, and then use the calculated Matrix to draw the image:
[Java]
Private void doDraw (Canvas canvas, float src [], float dst []) {
Canvas. save ();
MMatrix. setPolyToPoly (src, 0, dst, 0, src. length> 1 );
Canvas. concat (mMatrix );
MPaint. setColor (Color. GRAY );
MPaint. setStyle (Paint. Style. STROKE );
Canvas. drawRect (0, 0, 64, 64, mPaint );
Canvas. drawLine (0, 0, 64, 64, mPaint );
Canvas. drawLine (0, 64, 64, 0, mPaint );
MPaint. setColor (Color. RED );
MPaint. setStyle (Paint. Style. FILL );
// How to draw the text center on our square
// Centering in X is easy... use alignment (and X at midpoint)
Float x = 64/2;
// Centering in Y, we need to measure ascent/descent first
Float y = 64/2-(mFontMetrics. ascent + mFontMetrics. descent)/2;
Canvas. drawText (src. length/2 + "", x, y, mPaint );
Canvas. restore ();
}
....
Canvas. save ();
Canvas. translate (10, 10 );
// Translate (1 point)
DoDraw (canvas, new float [] {0, 0 },
New float [] {5, 5 });
Canvas. restore ();
Canvas. save ();
Canvas. translate (160, 10 );
// Rotate/uniform-scale (2 points)
DoDraw (canvas, new float [] {32, 32, 64, 32 },
New float [] {32, 32, 64, 48 });
Canvas. restore ();
Canvas. save ();
Canvas. translate (10,110 );
// Rotate/skew (3 points)
DoDraw (canvas, new float [] {0, 0, 64, 0, 0, 64 },
New float [] {0, 0, 96, 0, 24, 64 });
Canvas. restore ();
Canvas. save ();
Canvas. translate (160,110 );
// Perspective (4 points)
DoDraw (canvas, new float [] {0, 0, 64, 0, 64, 64, 0, 64 },
New float [] {0, 0, 96, 0, 64, 96, 0, 64 });
Canvas. restore ();
Private void doDraw (Canvas canvas, float src [], float dst []) {
Canvas. save ();
MMatrix. setPolyToPoly (src, 0, dst, 0, src. length> 1 );
Canvas. concat (mMatrix );
MPaint. setColor (Color. GRAY );
MPaint. setStyle (Paint. Style. STROKE );
Canvas. drawRect (0, 0, 64, 64, mPaint );
Canvas. drawLine (0, 0, 64, 64, mPaint );
Canvas. drawLine (0, 64, 64, 0, mPaint );
MPaint. setColor (Color. RED );
MPaint. setStyle (Paint. Style. FILL );
// How to draw the text center on our square
// Centering in X is easy... use alignment (and X at midpoint)
Float x = 64/2;
// Centering in Y, we need to measure ascent/descent first
Float y = 64/2-(mFontMetrics. ascent + mFontMetrics. descent)/2;
Canvas. drawText (src. length/2 + "", x, y, mPaint );
Canvas. restore ();
}
....
Canvas. save ();
Canvas. translate (10, 10 );
// Translate (1 point)
DoDraw (canvas, new float [] {0, 0 },
New float [] {5, 5 });
Canvas. restore ();
Canvas. save ();
Canvas. translate (160, 10 );
// Rotate/uniform-scale (2 points)
DoDraw (canvas, new float [] {32, 32, 64, 32 },
New float [] {32, 32, 64, 48 });
Canvas. restore (); www.2cto.com
Canvas. save ();
Canvas. translate (10,110 );
// Rotate/skew (3 points)
DoDraw (canvas, new float [] {0, 0, 64, 0, 0, 64 },
New float [] {0, 0, 96, 0, 24, 64 });
Canvas. restore ();
Canvas. save ();
Canvas. translate (160,110 );
// Perspective (4 points)
DoDraw (canvas, new float [] {0, 0, 64, 0, 64, 64, 0, 64 },
New float [] {0, 0, 96, 0, 64, 96, 0, 64 });
Canvas. restore ();
Author: mapdigit