原博文在此,原来的文章中对Matrix和ColorMatrix都有解释,不再多说。
https://blog.csdn.net/qqxiaoqiang1573/article/details/50781466
https://blog.csdn.net/QQxiaoqiang1573/article/details/50847587
看一下效果,
原文的源码我整合到一个项目中,有些显示地方作了改动,下载即可直接运行。
https://download.csdn.net/download/tanmx219/10574162
下面是来自Android 官网,因为不想老是FQ,所以拷贝过来Ref.
https://developer.android.com/reference/android/graphics/Matrix
https://developer.android.com/reference/android/graphics/ColorMatrix
ColorMatrix4x5 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 ]
Summary Public constructorsColorMatrix() Create a new colormatrix initialized to identity (as if reset() had been called).
ColorMatrix(float[] src) Create a new colormatrix initialized with the specified array of values.
ColorMatrix(ColorMatrix src) Create a new colormatrix initialized with the specified colormatrix.
Public methods
booleanequals(Object obj) Indicates whether some other object is "equal to" this one.
final float[]getArray() Return the array of floats representing this colormatrix.
voidpostConcat(ColorMatrix postmatrix) Concat this colormatrix with the specified postmatrix.
voidpreConcat(ColorMatrix prematrix) Concat this colormatrix with the specified prematrix.
voidreset() 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
voidset(float[] src) Assign the array of floats into this matrix, copying all of its values.
voidset(ColorMatrix src) Assign the src colormatrix into this matrix, copying all of its values.
voidsetConcat(ColorMatrix matA, ColorMatrix 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.
voidsetRGB2YUV() Set the matrix to convert RGB to YUV
voidsetRotate(int axis, float degrees) Set the rotation on a color axis by the specified values.
voidsetSaturation(float sat) Set the matrix to affect the saturation of colors.
voidsetScale(float rScale, float gScale, float bScale, float aScale) Set this colormatrix to scale by the specified values.
voidsetYUV2RGB() Set the matrix to convert from YUV to RGB
Public methods equalspublic boolean equals (Object obj)
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:
- It is reflexive: for any non-null reference value
x,x.equals(x)should returntrue. - It is symmetric: for any non-null reference values
xandy,x.equals(y)should returntrueif and only ify.equals(x)returnstrue. - It is transitive: for any non-null reference values
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)should returntrue. - It is consistent: for any non-null reference values
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided no information used inequalscomparisons on the objects is modified. - For any non-null reference value
x,x.equals(null)should returnfalse.
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.
objObject: the reference object with which to compare. Returns
booleantrue if this object is the same as the obj argument; false otherwise. getArray
public final float[] getArray ()
Return the array of floats representing this colormatrix.
Returns
float[] postConcat
public void postConcat (ColorMatrix postmatrix)
Concat this colormatrix with the specified postmatrix.
This is logically the same as calling setConcat(postmatrix, this);
Parameters
postmatrixColorMatrix preConcat
public void preConcat (ColorMatrix prematrix)
Concat this colormatrix with the specified prematrix.
This is logically the same as calling setConcat(this, prematrix);
Parameters
prematrixColorMatrix reset
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
set
public void set (float[] src)
Assign the array of floats into this matrix, copying all of its values.
Parameters
srcfloat set
public void set (ColorMatrix src)
Assign the src colormatrix into this matrix, copying all of its values.
Parameters
srcColorMatrix setConcat
public void setConcat (ColorMatrix matA, ColorMatrix 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
matAColorMatrix
matBColorMatrix setRGB2YUV
public void setRGB2YUV ()
Set the matrix to convert RGB to YUV
setRotate
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
axisint
degreesfloat setSaturation
public void setSaturation (float sat)
Set the matrix to affect the saturation of colors.
Parameters
satfloat: A value of 0 maps the color to gray-scale. 1 is identity. setScale
public void setScale (float rScale, float gScale, float bScale, float aScale)
Set this colormatrix to scale by the specified values.
Parameters
rScalefloat
gScalefloat
bScalefloat
aScalefloat setYUV2RGB
public void setYUV2RGB ()
Set the matrix to convert from YUV to RGB
Matrix java.lang.Object ↳android.graphics.Matrix
The Matrix class holds a 3x3 matrix for transforming coordinates.
Summary Nested classesenumMatrix.ScaleToFit Controlls how the src rect should align into the dst rect for setRectToRect().
ConstantsintMPERSP_0
intMPERSP_1
intMPERSP_2
intMSCALE_X
intMSCALE_Y
intMSKEW_X
intMSKEW_Y
intMTRANS_X
intMTRANS_Y Public constructors
Matrix() Create an identity matrix
Matrix(Matrix src) Create a matrix that is a (deep) copy of src
Public methodsbooleanequals(Object obj) Returns true iff obj is a Matrix and its values equal our values.
voidgetValues(float[] values) Copy 9 values from the matrix into the array.
inthashCode() Returns a hash code value for the object.
booleaninvert(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.
booleanisAffine() Gets whether this matrix is affine.
booleanisIdentity() Returns true if the matrix is identity.
voidmapPoints(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.
voidmapPoints(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.
voidmapPoints(float[] pts) Apply this matrix to the array of 2D points, and write the transformed points back into the array
floatmapRadius(float radius) Return the mean radius of a circle after it has been mapped by this matrix.
booleanmapRect(RectF rect) Apply this matrix to the rectangle, and write the transformed rectangle back into it.
booleanmapRect(RectF dst, RectF src) Apply this matrix to the src rectangle, and write the transformed rectangle into dst.
voidmapVectors(float[] vecs) Apply this matrix to the array of 2D vectors, and write the transformed vectors back into the array.
voidmapVectors(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.
voidmapVectors(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.
booleanpostConcat(Matrix other) Postconcats the matrix with the specified matrix.
booleanpostRotate(float degrees, float px, float py) Postconcats the matrix with the specified rotation.
booleanpostRotate(float degrees) Postconcats the matrix with the specified rotation.
booleanpostScale(float sx, float sy, float px, float py) Postconcats the matrix with the specified scale.
booleanpostScale(float sx, float sy) Postconcats the matrix with the specified scale.
booleanpostSkew(float kx, float ky) Postconcats the matrix with the specified skew.
booleanpostSkew(float kx, float ky, float px, float py) Postconcats the matrix with the specified skew.
booleanpostTranslate(float dx, float dy) Postconcats the matrix with the specified translation.
booleanpreConcat(Matrix other) Preconcats the matrix with the specified matrix.
booleanpreRotate(float degrees) Preconcats the matrix with the specified rotation.
booleanpreRotate(float degrees, float px, float py) Preconcats the matrix with the specified rotation.
booleanpreScale(float sx, float sy) Preconcats the matrix with the specified scale.
booleanpreScale(float sx, float sy, float px, float py) Preconcats the matrix with the specified scale.
booleanpreSkew(float kx, float ky) Preconcats the matrix with the specified skew.
booleanpreSkew(float kx, float ky, float px, float py) Preconcats the matrix with the specified skew.
booleanpreTranslate(float dx, float dy) Preconcats the matrix with the specified translation.
booleanrectStaysRect() Returns true if will map a rectangle to another rectangle.
voidreset() Set the matrix to identity
voidset(Matrix src) (deep) copy the src matrix into this matrix.
booleansetConcat(Matrix a, Matrix b) Set the matrix to the concatenation of the two specified matrices and return true.
booleansetPolyToPoly(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.
booleansetRectToRect(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 the result can be represented.
voidsetRotate(float degrees, float px, float py) Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).
voidsetRotate(float degrees) Set the matrix to rotate about (0,0) by the specified number of degrees.
voidsetScale(float sx, float sy) Set the matrix to scale by sx and sy.
voidsetScale(float sx, float sy, float px, float py) Set the matrix to scale by sx and sy, with a pivot point at (px, py).
voidsetSinCos(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).
voidsetSinCos(float sinValue, float cosValue) Set the matrix to rotate by the specified sine and cosine values.
voidsetSkew(float kx, float ky) Set the matrix to skew by sx and sy.
voidsetSkew(float kx, float ky, float px, float py) Set the matrix to skew by sx and sy, with a pivot point at (px, py).
voidsetTranslate(float dx, float dy) Set the matrix to translate by (dx, dy).
voidsetValues(float[] values) Copy 9 values from the array into the matrix.
StringtoShortString()StringtoString() Returns a string representation of the object.
Public methods equals
public boolean equals (Object obj)
Returns true iff obj is a Matrix and its values equal our values.
Parameters
objObject: the reference object with which to compare. Returns
booleantrue 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
valuesfloat 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
hashCodemethod must consistently return the same integer, provided no information used inequalscomparisons 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(Object)method, then calling thehashCodemethod 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(java.lang.Object)method, then calling thehashCodemethod 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
inta 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
inverseMatrix Returns
boolean isAffine
public boolean isAffine ()
Gets whether this matrix is affine. An affine matrix preserves straight lines and has no perspective.
Returns
booleanWhether 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
dstfloat: The array of dst points (x,y pairs)
dstIndexint: The index of the first [x,y] pair of dst floats
srcfloat: The array of src points (x,y pairs)
srcIndexint: The index of the first [x,y] pair of src floats
pointCountint: 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
dstfloat: The array of dst points (x,y pairs)
srcfloat: 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
ptsfloat: 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 origin.
Parameters
radiusfloat 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
rectRectF: The rectangle to transform. Returns
booleanthe 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
dstRectF: Where the transformed rectangle is written.
srcRectF: The original rectangle to be transformed. Returns
booleanthe 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 mapPoints(float[]) if you want the translation to be applied.
Parameters
vecsfloat: 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 mapPoints(float[], int, float[], int, int) if you want the translation to be applied.
Parameters
dstfloat: The array of dst vectors (x,y pairs)
dstIndexint: The index of the first [x,y] pair of dst floats
srcfloat: The array of src vectors (x,y pairs)
srcIndexint: The index of the first [x,y] pair of src floats
vectorCountint: 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 mapPoints(float[], float[]) if you want the translation to be applied.
Parameters
dstfloat: The array of dst vectors (x,y pairs)
srcfloat: 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
otherMatrix 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
degreesfloat
pxfloat
pyfloat Returns
boolean postRotate
public boolean postRotate (float degrees)
Postconcats the matrix with the specified rotation. M' = R(degrees) * M
Parameters
degreesfloat 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
sxfloat
syfloat
pxfloat
pyfloat Returns
boolean postScale
public boolean postScale (float sx, float sy)
Postconcats the matrix with the specified scale. M' = S(sx, sy) * M
Parameters
sxfloat
syfloat Returns
boolean postSkew
public boolean postSkew (float kx, float ky)
Postconcats the matrix with the specified skew. M' = K(kx, ky) * M
Parameters
kxfloat
kyfloat 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
kxfloat
kyfloat
pxfloat
pyfloat Returns
boolean postTranslate
public boolean postTranslate (float dx, float dy)
Postconcats the matrix with the specified translation. M' = T(dx, dy) * M
Parameters
dxfloat
dyfloat Returns
boolean preConcat
public boolean preConcat (Matrix other)
Preconcats the matrix with the specified matrix. M' = M * other
Parameters
otherMatrix Returns
boolean preRotate
public boolean preRotate (float degrees)
Preconcats the matrix with the specified rotation. M' = M * R(degrees)
Parameters
degreesfloat 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
degreesfloat
pxfloat
pyfloat Returns
boolean preScale
public boolean preScale (float sx, float sy)
Preconcats the matrix with the specified scale. M' = M * S(sx, sy)
Parameters
sxfloat
syfloat 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
sxfloat
syfloat
pxfloat
pyfloat Returns
boolean preSkew
public boolean preSkew (float kx, float ky)
Preconcats the matrix with the specified skew. M' = M * K(kx, ky)
Parameters
kxfloat
kyfloat 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
kxfloat
kyfloat
pxfloat
pyfloat Returns
boolean preTranslate
public boolean preTranslate (float dx, float dy)
Preconcats the matrix with the specified translation. M' = M * T(dx, dy)
Parameters
dxfloat
dyfloat 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 reset
public void reset ()
Set the matrix to identity
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
srcMatrix 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. InBuild.VERSION_CODES.HONEYCOMB and above, it always returns true.
aMatrix
bMatrix 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.
Parameterssrcfloat: The array of src [x,y] pairs (points)
srcIndexint: Index of the first pair of src values
dstfloat: The array of dst [x,y] pairs (points)
dstIndexint: Index of the first pair of dst values
pointCountint: The number of pairs/points to be used. Must be [0..4] Returns
booleantrue 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 the result can be represented.
ParameterssrcRectF: the source rectangle to map from.
dstRectF: the destination rectangle to map to.
stfMatrix.ScaleToFit: the ScaleToFit option Returns
booleantrue 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.
Parametersdegreesfloat
pxfloat
pyfloat setRotate
public void setRotate (float degrees)
Set the matrix to rotate about (0,0) by the specified number of degrees.
Parametersdegreesfloat setScale
public void setScale (float sx, float sy)
Set the matrix to scale by sx and sy.
Parameterssxfloat
syfloat 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.
Parameterssxfloat
syfloat
pxfloat
pyfloat 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.
ParameterssinValuefloat
cosValuefloat
pxfloat
pyfloat setSinCos
public void setSinCos (float sinValue, float cosValue)
Set the matrix to rotate by the specified sine and cosine values.
Parameters
sinValuefloat
cosValuefloat setSkew
public void setSkew (float kx, float ky)
Set the matrix to skew by sx and sy.
Parameters
kxfloat
kyfloat 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
kxfloat
kyfloat
pxfloat
pyfloat setTranslate
public void setTranslate (float dx, float dy)
Set the matrix to translate by (dx, dy).
Parameters
dxfloat
dyfloat 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.
Parametersvaluesfloat toShortString
public String toShortString ()
ReturnsString toString
public String 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())
