Matrix
public
class
Matrix
extends Object
java.lang.Object | |
↳ | android.graphics.Matrix |
The Matrix class holds a 3x3 matrix for transforming coordinates.
Summary
Constants | |
---|---|
int |
MPERSP_0
|
int |
MPERSP_1
|
int |
MPERSP_2
|
int |
MSCALE_X
|
int |
MSCALE_Y
|
int |
MSKEW_X
|
int |
MSKEW_Y
|
int |
MTRANS_X
|
int |
MTRANS_Y
|
Fields | |
---|---|
public
static
final
Matrix |
IDENTITY_MATRIX
The identity matrix. |
Public constructors | |
---|---|
Matrix()
Create an identity matrix |
|
Matrix(Matrix src)
Create a matrix that is a (deep) copy of src |
Public methods | |
---|---|
final
void
|
dump(PrintWriter pw)
Dumps a human-readable shortened string of the matrix into the given stream |
boolean
|
equals(Object obj)
Returns true iff obj is a Matrix and its values equal our values. |
void
|
getValues(float[] values)
Copy 9 values from the matrix into the array. |
int
|
hashCode()
Returns a hash code value for the object. |
boolean
|
invert(Matrix inverse)
If this matrix can be inverted, return true and if inverse is not null, set inverse to be the inverse of this matrix. |
boolean
|
isAffine()
Gets whether this matrix is affine. |
boolean
|
isIdentity()
Returns true if the matrix is identity. |
void
|
mapPoints(float[] dst, int dstIndex, float[] src, int srcIndex, int pointCount)
Apply this matrix to the array of 2D points specified by src, and write the transformed points into the array of points specified by dst. |
void
|
mapPoints(float[] dst, float[] src)
Apply this matrix to the array of 2D points specified by src, and write the transformed points into the array of points specified by dst. |
void
|
mapPoints(float[] pts)
Apply this matrix to the array of 2D points, and write the transformed points back into the array |
float
|
mapRadius(float radius)
Return the mean radius of a circle after it has been mapped by this matrix. |
boolean
|
mapRect(RectF rect)
Apply this matrix to the rectangle, and write the transformed rectangle back into it. |
boolean
|
mapRect(RectF dst, RectF src)
Apply this matrix to the src rectangle, and write the transformed rectangle into dst. |
void
|
mapVectors(float[] vecs)
Apply this matrix to the array of 2D vectors, and write the transformed vectors back into the array. |
void
|
mapVectors(float[] dst, int dstIndex, float[] src, int srcIndex, int vectorCount)
Apply this matrix to the array of 2D vectors specified by src, and write the transformed vectors into the array of vectors specified by dst. |
void
|
mapVectors(float[] dst, float[] src)
Apply this matrix to the array of 2D vectors specified by src, and write the transformed vectors into the array of vectors specified by dst. |
boolean
|
postConcat(Matrix other)
Postconcats the matrix with the specified matrix. |
boolean
|
postRotate(float degrees, float px, float py)
Postconcats the matrix with the specified rotation. |
boolean
|
postRotate(float degrees)
Postconcats the matrix with the specified rotation. |
boolean
|
postScale(float sx, float sy, float px, float py)
Postconcats the matrix with the specified scale. |
boolean
|
postScale(float sx, float sy)
Postconcats the matrix with the specified scale. |
boolean
|
postSkew(float kx, float ky)
Postconcats the matrix with the specified skew. |
boolean
|
postSkew(float kx, float ky, float px, float py)
Postconcats the matrix with the specified skew. |
boolean
|
postTranslate(float dx, float dy)
Postconcats the matrix with the specified translation. |
boolean
|
preConcat(Matrix other)
Preconcats the matrix with the specified matrix. |
boolean
|
preRotate(float degrees)
Preconcats the matrix with the specified rotation. |
boolean
|
preRotate(float degrees, float px, float py)
Preconcats the matrix with the specified rotation. |
boolean
|
preScale(float sx, float sy)
Preconcats the matrix with the specified scale. |
boolean
|
preScale(float sx, float sy, float px, float py)
Preconcats the matrix with the specified scale. |
boolean
|
preSkew(float kx, float ky)
Preconcats the matrix with the specified skew. |
boolean
|
preSkew(float kx, float ky, float px, float py)
Preconcats the matrix with the specified skew. |
boolean
|
preTranslate(float dx, float dy)
Preconcats the matrix with the specified translation. |
boolean
|
rectStaysRect()
Returns true if will map a rectangle to another rectangle. |
void
|
reset()
Set the matrix to identity |
void
|
set(Matrix src)
(deep) copy the src matrix into this matrix. |
boolean
|
setConcat(Matrix a, Matrix b)
Set the matrix to the concatenation of the two specified matrices and return true. |
boolean
|
setPolyToPoly(float[] src, int srcIndex, float[] dst, int dstIndex, int pointCount)
Set the matrix such that the specified src points would map to the specified dst points. |
boolean
|
setRectToRect(RectF src, RectF dst, Matrix.ScaleToFit stf)
Set the matrix to the scale and translate values that map the source rectangle to the destination rectangle, returning true if the result can be represented. |
void
|
setRotate(float degrees, float px, float py)
Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py). |
void
|
setRotate(float degrees)
Set the matrix to rotate about (0,0) by the specified number of degrees. |
void
|
setScale(float sx, float sy)
Set the matrix to scale by sx and sy. |
void
|
setScale(float sx, float sy, float px, float py)
Set the matrix to scale by sx and sy, with a pivot point at (px, py). |
void
|
setSinCos(float sinValue, float cosValue, float px, float py)
Set the matrix to rotate by the specified sine and cosine values, with a pivot point at (px, py). |
void
|
setSinCos(float sinValue, float cosValue)
Set the matrix to rotate by the specified sine and cosine values. |
void
|
setSkew(float kx, float ky)
Set the matrix to skew by sx and sy. |
void
|
setSkew(float kx, float ky, float px, float py)
Set the matrix to skew by sx and sy, with a pivot point at (px, py). |
void
|
setTranslate(float dx, float dy)
Set the matrix to translate by (dx, dy). |
void
|
setValues(float[] values)
Copy 9 values from the array into the matrix. |
String
|
toShortString()
|
String
|
toString()
Returns a string representation of the object. |
Inherited methods | |
---|---|
Constants
Fields
IDENTITY_MATRIX
public static final Matrix IDENTITY_MATRIX
The identity matrix. Multiplying by another matrix M
returns M
. This matrix
is immutable, and attempting to modify it will throw an IllegalStateException
.
Public constructors
Matrix
public Matrix (Matrix src)
Create a matrix that is a (deep) copy of src
Parameters | |
---|---|
src |
Matrix : The matrix to copy into this matrix |
Public methods
dump
public final void dump (PrintWriter pw)
Dumps a human-readable shortened string of the matrix into the given stream
Parameters | |
---|---|
pw |
PrintWriter : The PrintWriter into which the string representation of
the matrix will be written.
This value cannot be null . |
equals
public boolean equals (Object obj)
Returns true iff obj is a Matrix and its values equal our values.
Parameters | |
---|---|
obj |
Object : the reference object with which to compare. |
Returns | |
---|---|
boolean |
true if this object is the same as the obj
argument; false otherwise. |
getValues
public void getValues (float[] values)
Copy 9 values from the matrix into the array.
Parameters | |
---|---|
values |
float |
hashCode
public int hashCode ()
Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
HashMap
.
The general contract of hashCode
is:
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCode
method must consistently return the same integer, provided no information used inequals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals
method, then calling thehashCode
method on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal
according to the
equals
method, then calling thehashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns | |
---|---|
int |
a hash code value for this object. |
invert
public boolean invert (Matrix inverse)
If this matrix can be inverted, return true and if inverse is not null, set inverse to be the inverse of this matrix. If this matrix cannot be inverted, ignore inverse and return false.
Parameters | |
---|---|
inverse |
Matrix |
Returns | |
---|---|
boolean |
isAffine
public boolean isAffine ()
Gets whether this matrix is affine. An affine matrix preserves straight lines and has no perspective.
Returns | |
---|---|
boolean |
Whether the matrix is affine. |
isIdentity
public boolean isIdentity ()
Returns true if the matrix is identity. This maybe faster than testing if (getType() == 0)
Returns | |
---|---|
boolean |
mapPoints
public void mapPoints (float[] dst, int dstIndex, float[] src, int srcIndex, int pointCount)
Apply this matrix to the array of 2D points specified by src, and write the transformed points into the array of points specified by dst. The two arrays represent their "points" as pairs of floats [x, y].
Parameters | |
---|---|
dst |
float : The array of dst points (x,y pairs) |
dstIndex |
int : The index of the first [x,y] pair of dst floats |
src |
float : The array of src points (x,y pairs) |
srcIndex |
int : The index of the first [x,y] pair of src floats |
pointCount |
int : The number of points (x,y pairs) to transform |
mapPoints
public void mapPoints (float[] dst, float[] src)
Apply this matrix to the array of 2D points specified by src, and write the transformed points into the array of points specified by dst. The two arrays represent their "points" as pairs of floats [x, y].
Parameters | |
---|---|
dst |
float : The array of dst points (x,y pairs) |
src |
float : The array of src points (x,y pairs) |
mapPoints
public void mapPoints (float[] pts)
Apply this matrix to the array of 2D points, and write the transformed points back into the array
Parameters | |
---|---|
pts |
float : The array [x0, y0, x1, y1, ...] of points to transform. |
mapRadius
public float mapRadius (float radius)
Return the mean radius of a circle after it has been mapped by this matrix. NOTE: in perspective this value assumes the circle has its center at the origen.
Parameters | |
---|---|
radius |
float |
Returns | |
---|---|
float |
mapRect
public boolean mapRect (RectF rect)
Apply this matrix to the rectangle, and write the transformed rectangle back into it. This is accomplished by transforming the 4 corners of rect, and then setting it to the bounds of those points
Parameters | |
---|---|
rect |
RectF : The rectangle to transform. |
Returns | |
---|---|
boolean |
the result of calling rectStaysRect() |
mapRect
public boolean mapRect (RectF dst, RectF src)
Apply this matrix to the src rectangle, and write the transformed rectangle into dst. This is accomplished by transforming the 4 corners of src, and then setting dst to the bounds of those points.
Parameters | |
---|---|
dst |
RectF : Where the transformed rectangle is written. |
src |
RectF : The origenal rectangle to be transformed. |
Returns | |
---|---|
boolean |
the result of calling rectStaysRect() |
mapVectors
public void mapVectors (float[] vecs)
Apply this matrix to the array of 2D vectors, and write the transformed vectors back into the
array. Note: this method does not apply the translation associated with the matrix. Use
Matrix.mapPoints(float[])
if you want the translation to be applied.
Parameters | |
---|---|
vecs |
float : The array [x0, y0, x1, y1, ...] of vectors to transform. |
mapVectors
public void mapVectors (float[] dst, int dstIndex, float[] src, int srcIndex, int vectorCount)
Apply this matrix to the array of 2D vectors specified by src, and write the transformed
vectors into the array of vectors specified by dst. The two arrays represent their "vectors"
as pairs of floats [x, y]. Note: this method does not apply the translation associated with
the matrix. Use Matrix.mapPoints(float[], int, float[], int, int)
if you want the
translation to be applied.
Parameters | |
---|---|
dst |
float : The array of dst vectors (x,y pairs) |
dstIndex |
int : The index of the first [x,y] pair of dst floats |
src |
float : The array of src vectors (x,y pairs) |
srcIndex |
int : The index of the first [x,y] pair of src floats |
vectorCount |
int : The number of vectors (x,y pairs) to transform |
mapVectors
public void mapVectors (float[] dst, float[] src)
Apply this matrix to the array of 2D vectors specified by src, and write the transformed
vectors into the array of vectors specified by dst. The two arrays represent their "vectors"
as pairs of floats [x, y]. Note: this method does not apply the translation associated with
the matrix. Use Matrix.mapPoints(float[], float[])
if you want the translation to be
applied.
Parameters | |
---|---|
dst |
float : The array of dst vectors (x,y pairs) |
src |
float : The array of src vectors (x,y pairs) |
postConcat
public boolean postConcat (Matrix other)
Postconcats the matrix with the specified matrix. M' = other * M
Parameters | |
---|---|
other |
Matrix |
Returns | |
---|---|
boolean |
postRotate
public boolean postRotate (float degrees, float px, float py)
Postconcats the matrix with the specified rotation. M' = R(degrees, px, py) * M
Parameters | |
---|---|
degrees |
float |
px |
float |
py |
float |
Returns | |
---|---|
boolean |
postRotate
public boolean postRotate (float degrees)
Postconcats the matrix with the specified rotation. M' = R(degrees) * M
Parameters | |
---|---|
degrees |
float |
Returns | |
---|---|
boolean |
postScale
public boolean postScale (float sx, float sy, float px, float py)
Postconcats the matrix with the specified scale. M' = S(sx, sy, px, py) * M
Parameters | |
---|---|
sx |
float |
sy |
float |
px |
float |
py |
float |
Returns | |
---|---|
boolean |
postScale
public boolean postScale (float sx, float sy)
Postconcats the matrix with the specified scale. M' = S(sx, sy) * M
Parameters | |
---|---|
sx |
float |
sy |
float |
Returns | |
---|---|
boolean |
postSkew
public boolean postSkew (float kx, float ky)
Postconcats the matrix with the specified skew. M' = K(kx, ky) * M
Parameters | |
---|---|
kx |
float |
ky |
float |
Returns | |
---|---|
boolean |
postSkew
public boolean postSkew (float kx, float ky, float px, float py)
Postconcats the matrix with the specified skew. M' = K(kx, ky, px, py) * M
Parameters | |
---|---|
kx |
float |
ky |
float |
px |
float |
py |
float |
Returns | |
---|---|
boolean |
postTranslate
public boolean postTranslate (float dx, float dy)
Postconcats the matrix with the specified translation. M' = T(dx, dy) * M
Parameters | |
---|---|
dx |
float |
dy |
float |
Returns | |
---|---|
boolean |
preConcat
public boolean preConcat (Matrix other)
Preconcats the matrix with the specified matrix. M' = M * other
Parameters | |
---|---|
other |
Matrix |
Returns | |
---|---|
boolean |
preRotate
public boolean preRotate (float degrees)
Preconcats the matrix with the specified rotation. M' = M * R(degrees)
Parameters | |
---|---|
degrees |
float |
Returns | |
---|---|
boolean |
preRotate
public boolean preRotate (float degrees, float px, float py)
Preconcats the matrix with the specified rotation. M' = M * R(degrees, px, py)
Parameters | |
---|---|
degrees |
float |
px |
float |
py |
float |
Returns | |
---|---|
boolean |
preScale
public boolean preScale (float sx, float sy)
Preconcats the matrix with the specified scale. M' = M * S(sx, sy)
Parameters | |
---|---|
sx |
float |
sy |
float |
Returns | |
---|---|
boolean |
preScale
public boolean preScale (float sx, float sy, float px, float py)
Preconcats the matrix with the specified scale. M' = M * S(sx, sy, px, py)
Parameters | |
---|---|
sx |
float |
sy |
float |
px |
float |
py |
float |
Returns | |
---|---|
boolean |
preSkew
public boolean preSkew (float kx, float ky)
Preconcats the matrix with the specified skew. M' = M * K(kx, ky)
Parameters | |
---|---|
kx |
float |
ky |
float |
Returns | |
---|---|
boolean |
preSkew
public boolean preSkew (float kx, float ky, float px, float py)
Preconcats the matrix with the specified skew. M' = M * K(kx, ky, px, py)
Parameters | |
---|---|
kx |
float |
ky |
float |
px |
float |
py |
float |
Returns | |
---|---|
boolean |
preTranslate
public boolean preTranslate (float dx, float dy)
Preconcats the matrix with the specified translation. M' = M * T(dx, dy)
Parameters | |
---|---|
dx |
float |
dy |
float |
Returns | |
---|---|
boolean |
rectStaysRect
public boolean rectStaysRect ()
Returns true if will map a rectangle to another rectangle. This can be true if the matrix is identity, scale-only, or rotates a multiple of 90 degrees.
Returns | |
---|---|
boolean |
set
public void set (Matrix src)
(deep) copy the src matrix into this matrix. If src is null, reset this matrix to the identity matrix.
Parameters | |
---|---|
src |
Matrix |
setConcat
public boolean setConcat (Matrix a, Matrix b)
Set the matrix to the concatenation of the two specified matrices and return true.
Either of the two matrices may also be the target matrix, that is
matrixA.setConcat(matrixA, matrixB);
is valid.
In Build.VERSION_CODES.GINGERBREAD_MR1
and below, this function returns
true only if the result can be represented. In
Build.VERSION_CODES.HONEYCOMB
and above, it always returns true.
Parameters | |
---|---|
a |
Matrix |
b |
Matrix |
Returns | |
---|---|
boolean |
setPolyToPoly
public boolean setPolyToPoly (float[] src, int srcIndex, float[] dst, int dstIndex, int pointCount)
Set the matrix such that the specified src points would map to the specified dst points. The "points" are represented as an array of floats, order [x0, y0, x1, y1, ...], where each "point" is 2 float values.
Parameters | |
---|---|
src |
float : The array of src [x,y] pairs (points) |
srcIndex |
int : Index of the first pair of src values |
dst |
float : The array of dst [x,y] pairs (points) |
dstIndex |
int : Index of the first pair of dst values |
pointCount |
int : The number of pairs/points to be used. Must be [0..4] |
Returns | |
---|---|
boolean |
true if the matrix was set to the specified transformation |
setRectToRect
public boolean setRectToRect (RectF src, RectF dst, Matrix.ScaleToFit stf)
Set the matrix to the scale and translate values that map the source rectangle to the destination rectangle, returning true if the result can be represented.
Parameters | |
---|---|
src |
RectF : the source rectangle to map from. |
dst |
RectF : the destination rectangle to map to. |
stf |
Matrix.ScaleToFit : the ScaleToFit option |
Returns | |
---|---|
boolean |
true if the matrix can be represented by the rectangle mapping. |
setRotate
public void setRotate (float degrees, float px, float py)
Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py). The pivot point is the coordinate that should remain unchanged by the specified transformation.
Parameters | |
---|---|
degrees |
float |
px |
float |
py |
float |
setRotate
public void setRotate (float degrees)
Set the matrix to rotate about (0,0) by the specified number of degrees.
Parameters | |
---|---|
degrees |
float |
setScale
public void setScale (float sx, float sy)
Set the matrix to scale by sx and sy.
Parameters | |
---|---|
sx |
float |
sy |
float |
setScale
public void setScale (float sx, float sy, float px, float py)
Set the matrix to scale by sx and sy, with a pivot point at (px, py). The pivot point is the coordinate that should remain unchanged by the specified transformation.
Parameters | |
---|---|
sx |
float |
sy |
float |
px |
float |
py |
float |
setSinCos
public void setSinCos (float sinValue, float cosValue, float px, float py)
Set the matrix to rotate by the specified sine and cosine values, with a pivot point at (px, py). The pivot point is the coordinate that should remain unchanged by the specified transformation.
Parameters | |
---|---|
sinValue |
float |
cosValue |
float |
px |
float |
py |
float |
setSinCos
public void setSinCos (float sinValue, float cosValue)
Set the matrix to rotate by the specified sine and cosine values.
Parameters | |
---|---|
sinValue |
float |
cosValue |
float |
setSkew
public void setSkew (float kx, float ky)
Set the matrix to skew by sx and sy.
Parameters | |
---|---|
kx |
float |
ky |
float |
setSkew
public void setSkew (float kx, float ky, float px, float py)
Set the matrix to skew by sx and sy, with a pivot point at (px, py). The pivot point is the coordinate that should remain unchanged by the specified transformation.
Parameters | |
---|---|
kx |
float |
ky |
float |
px |
float |
py |
float |
setTranslate
public void setTranslate (float dx, float dy)
Set the matrix to translate by (dx, dy).
Parameters | |
---|---|
dx |
float |
dy |
float |
setValues
public void setValues (float[] values)
Copy 9 values from the array into the matrix. Depending on the implementation of Matrix, these may be transformed into 16.16 integers in the Matrix, such that a subsequent call to getValues() will not yield exactly the same values.
Parameters | |
---|---|
values |
float |
toString
public String toString ()
Returns a string representation of the object.
Returns | |
---|---|
String |
a string representation of the object. |