Android ApiDemos example (74): Graphics-& gt; PolyToPoly

Source: Internet
Author: User

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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.