本文共 25038 字,大约阅读时间需要 83 分钟。
原博文在此,原来的文章中对Matrix和ColorMatrix都有解释,不再多说。
看一下效果,
原文的源码我整合到一个项目中,有些显示地方作了改动,下载即可直接运行。
下面是来自Android 官网,因为不想老是FQ,所以拷贝过来Ref.
4x5 matrix for transforming the color and alpha components of a Bitmap. The matrix can be passed as single array, and is treated as follows:
[ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
When applied to a color [R, G, B, A]
, the resulting color is computed as:
R’ = a*R + b*G + c*B + d*A + e;
G’ = f*R + g*G + h*B + i*A + j;
B’ = k*R + l*G + m*B + n*A + o;
A’ = p*R + q*G + r*B + s*A + t;
That resulting color [R’, G’, B’, A’]
then has each channel clamped to the 0
to 255
range.
The sample ColorMatrix below inverts incoming colors by scaling each channel by -1
, and then shifting the result up by255
to remain in the standard color space.
[ -1, 0, 0, 0, 255, 0, -1, 0, 0, 255, 0, 0, -1, 0, 255, 0, 0, 0, 1, 0 ]
Public constructors | |
---|---|
() Create a new colormatrix initialized to identity (as if reset() had been called). | |
(float[] src) Create a new colormatrix initialized with the specified array of values. | |
( src) Create a new colormatrix initialized with the specified colormatrix. |
Public methods | |
---|---|
boolean | ( obj) Indicates whether some other object is "equal to" this one. |
final float[] | () Return the array of floats representing this colormatrix. |
void | ( postmatrix) Concat this colormatrix with the specified postmatrix. |
void | ( prematrix) Concat this colormatrix with the specified prematrix. |
void | () Set this colormatrix to identity: [ 1 0 0 0 0 - red vector 0 1 0 0 0 - green vector 0 0 1 0 0 - blue vector 0 0 0 1 0 ] - alpha vector
|
void | (float[] src) Assign the array of floats into this matrix, copying all of its values. |
void | ( src) Assign the src colormatrix into this matrix, copying all of its values. |
void | ( matA, matB) Set this colormatrix to the concatenation of the two specified colormatrices, such that the resulting colormatrix has the same effect as applying matB and then applying matA. |
void | () Set the matrix to convert RGB to YUV |
void | (int axis, float degrees) Set the rotation on a color axis by the specified values. |
void | (float sat) Set the matrix to affect the saturation of colors. |
void | (float rScale, float gScale, float bScale, float aScale) Set this colormatrix to scale by the specified values. |
void | () Set the matrix to convert from YUV to RGB |
public boolean equals ( obj)
Indicates whether some other object is "equal to" this one.
The equals
method implements an equivalence relation on non-null object references:
x
, x.equals(x)
should return true
.x
and y
, x.equals(y)
should return true
if and only ify.equals(x)
returns true
.x
, y
, and z
, if x.equals(y)
returns true
and y.equals(z)
returns true
, then x.equals(z)
should return true
.x
and y
, multiple invocations of x.equals(y)
consistently return true
or consistently return false
, provided no information used in equals
comparisons on the objects is modified.x
, x.equals(null)
should return false
.The equals
method for class Object
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x
and y
, this method returns true
if and only if x
and y
refer to the same object (x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the general contract for the hashCode
method, which states that equal objects must have equal hash codes.
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.
|
public final float[] getArray ()
Return the array of floats representing this colormatrix.
Returns | |
---|---|
float[] |
|
public void postConcat ( postmatrix)
Concat this colormatrix with the specified postmatrix.
This is logically the same as calling setConcat(postmatrix, this);
Parameters | |
---|---|
postmatrix | ColorMatrix
|
public void preConcat ( prematrix)
Concat this colormatrix with the specified prematrix.
This is logically the same as calling setConcat(this, prematrix);
Parameters | |
---|---|
prematrix | ColorMatrix
|
public void reset ()
Set this colormatrix to identity:
[ 1 0 0 0 0 - red vector 0 1 0 0 0 - green vector 0 0 1 0 0 - blue vector 0 0 0 1 0 ] - alpha vector
public void set (float[] src)
Assign the array of floats into this matrix, copying all of its values.
Parameters | |
---|---|
src | float
|
public void set ( src)
Assign the src colormatrix into this matrix, copying all of its values.
Parameters | |
---|---|
src | ColorMatrix
|
public void setConcat ( matA, matB)
Set this colormatrix to the concatenation of the two specified colormatrices, such that the resulting colormatrix has the same effect as applying matB and then applying matA.
It is legal for either matA or matB to be the same colormatrix as this.
Parameters | |
---|---|
matA | ColorMatrix
|
matB | ColorMatrix
|
public void setRGB2YUV ()
Set the matrix to convert RGB to YUV
public void setRotate (int axis, float degrees)
Set the rotation on a color axis by the specified values.
axis=0
correspond to a rotation around the RED color axis=1
correspond to a rotation around the GREEN coloraxis=2
correspond to a rotation around the BLUE color
Parameters | |
---|---|
axis | int
|
degrees | float
|
public void setSaturation (float sat)
Set the matrix to affect the saturation of colors.
Parameters | |
---|---|
sat | float : A value of 0 maps the color to gray-scale. 1 is identity.
|
public void setScale (float rScale, float gScale, float bScale, float aScale)
Set this colormatrix to scale by the specified values.
Parameters | |
---|---|
rScale | float
|
gScale | float
|
bScale | float
|
aScale | float
|
public void setYUV2RGB ()
Set the matrix to convert from YUV to RGB
↳ | android.graphics.Matrix |
The Matrix class holds a 3x3 matrix for transforming coordinates.
Nested classes | |
---|---|
enum | Controlls how the src rect should align into the dst rect for setRectToRect(). |
Constants | |
---|---|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
Public constructors | |
---|---|
() Create an identity matrix | |
( src) Create a matrix that is a (deep) copy of src |
Public methods | |
---|---|
boolean | ( obj) Returns true iff obj is a Matrix and its values equal our values. |
void | (float[] values) Copy 9 values from the matrix into the array. |
int | () Returns a hash code value for the object. |
boolean | ( 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 | () Gets whether this matrix is affine. |
boolean | () Returns true if the matrix is identity. |
void | (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 | (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 | (float[] pts) Apply this matrix to the array of 2D points, and write the transformed points back into the array |
float | (float radius) Return the mean radius of a circle after it has been mapped by this matrix. |
boolean | ( rect) Apply this matrix to the rectangle, and write the transformed rectangle back into it. |
boolean | ( dst, src) Apply this matrix to the src rectangle, and write the transformed rectangle into dst. |
void | (float[] vecs) Apply this matrix to the array of 2D vectors, and write the transformed vectors back into the array. |
void | (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 | (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 | ( other) Postconcats the matrix with the specified matrix. |
boolean | (float degrees, float px, float py) Postconcats the matrix with the specified rotation. |
boolean | (float degrees) Postconcats the matrix with the specified rotation. |
boolean | (float sx, float sy, float px, float py) Postconcats the matrix with the specified scale. |
boolean | (float sx, float sy) Postconcats the matrix with the specified scale. |
boolean | (float kx, float ky) Postconcats the matrix with the specified skew. |
boolean | (float kx, float ky, float px, float py) Postconcats the matrix with the specified skew. |
boolean | (float dx, float dy) Postconcats the matrix with the specified translation. |
boolean | ( other) Preconcats the matrix with the specified matrix. |
boolean | (float degrees) Preconcats the matrix with the specified rotation. |
boolean | (float degrees, float px, float py) Preconcats the matrix with the specified rotation. |
boolean | (float sx, float sy) Preconcats the matrix with the specified scale. |
boolean | (float sx, float sy, float px, float py) Preconcats the matrix with the specified scale. |
boolean | (float kx, float ky) Preconcats the matrix with the specified skew. |
boolean | (float kx, float ky, float px, float py) Preconcats the matrix with the specified skew. |
boolean | (float dx, float dy) Preconcats the matrix with the specified translation. |
boolean | () Returns true if will map a rectangle to another rectangle. |
void | () Set the matrix to identity |
void | ( src) (deep) copy the src matrix into this matrix. |
boolean | ( a, b) Set the matrix to the concatenation of the two specified matrices and return true. |
boolean | (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 | ( src, dst, stf) Set the matrix to the scale and translate values that map the source rectangle to the destination rectangle, returning true if the the result can be represented. |
void | (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 | (float degrees) Set the matrix to rotate about (0,0) by the specified number of degrees. |
void | (float sx, float sy) Set the matrix to scale by sx and sy. |
void | (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 | (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 | (float sinValue, float cosValue) Set the matrix to rotate by the specified sine and cosine values. |
void | (float kx, float ky) Set the matrix to skew by sx and sy. |
void | (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 | (float dx, float dy) Set the matrix to translate by (dx, dy). |
void | (float[] values) Copy 9 values from the array into the matrix. |
| () |
| () Returns a string representation of the object. |
public boolean equals ( 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.
|
public void getValues (float[] values)
Copy 9 values from the matrix into the array.
Parameters | |
---|---|
values | float
|
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 .
The general contract of hashCode
is:
hashCode
method must consistently return the same integer, provided no information used in equals
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.equals(Object)
method, then calling the hashCode
method on each of the two objects must produce the same integer result.
method, then calling the hashCode
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.As much as is reasonably practical, the hashCode method defined by class Object
does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
Returns | |
---|---|
int | a hash code value for this object.
|
public boolean invert ( 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 |
|
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.
|
public boolean isIdentity ()
Returns true if the matrix is identity. This maybe faster than testing if (getType() == 0)
Returns | |
---|---|
boolean |
|
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
|
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)
|
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.
|
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 origin.
Parameters | |
---|---|
radius | float
|
Returns | |
---|---|
float |
|
public boolean mapRect ( 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()
|
public boolean mapRect ( dst, 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 original rectangle to be transformed.
|
Returns | |
---|---|
boolean | the result of calling rectStaysRect()
|
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 if you want the translation to be applied.
Parameters | |
---|---|
vecs | float : The array [x0, y0, x1, y1, ...] of vectors to transform.
|
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 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
|
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 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)
|
public boolean postConcat ( other)
Postconcats the matrix with the specified matrix. M' = other * M
Parameters | |
---|---|
other | Matrix
|
Returns | |
---|---|
boolean |
|
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 |
|
public boolean postRotate (float degrees)
Postconcats the matrix with the specified rotation. M' = R(degrees) * M
Parameters | |
---|---|
degrees | float
|
Returns | |
---|---|
boolean |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
public boolean preConcat ( other)
Preconcats the matrix with the specified matrix. M' = M * other
Parameters | |
---|---|
other | Matrix
|
Returns | |
---|---|
boolean |
|
public boolean preRotate (float degrees)
Preconcats the matrix with the specified rotation. M' = M * R(degrees)
Parameters | |
---|---|
degrees | float
|
Returns | |
---|---|
boolean |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
public void reset ()
Set the matrix to identity
public void set ( src)
(deep) copy the src matrix into this matrix. If src is null, reset this matrix to the identity matrix.
Parameters | |
---|---|
src | Matrix
|
public boolean setConcat ( a, 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 and below, this function returns true only if the result can be represented. In
and above, it always returns true.
Parameters | |
---|---|
a | Matrix
|
b | Matrix
|
Returns | |
---|---|
boolean |
|
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
|
public boolean setRectToRect ( src, dst, stf)
Set the matrix to the scale and translate values that map the source rectangle to the destination rectangle, returning true if the 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.
|
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
|
public void setRotate (float degrees)
Set the matrix to rotate about (0,0) by the specified number of degrees.
Parameters | |
---|---|
degrees | float
|
public void setScale (float sx, float sy)
Set the matrix to scale by sx and sy.
Parameters | |
---|---|
sx | float
|
sy | float
|
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
|
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
|
public void setSinCos (float sinValue, float cosValue)
Set the matrix to rotate by the specified sine and cosine values.
Parameters | |
---|---|
sinValue | float
|
cosValue | float
|
public void setSkew (float kx, float ky)
Set the matrix to skew by sx and sy.
Parameters | |
---|---|
kx | float
|
ky | float
|
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
|
public void setTranslate (float dx, float dy)
Set the matrix to translate by (dx, dy).
Parameters | |
---|---|
dx | float
|
dy | float
|
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
|
public toShortString ()
Returns | |
---|---|
|
|
public toString ()
Returns a string representation of the object. In general, the toString
method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@
', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())