Android Paint Colorfilter Detailed

Source: Internet
Author: User
Tags mul

respect for the original, reproduced please indicate the source http://blog.csdn.net/abcdef314159

Before talking about the use of Android paint, where the Setcolorfilter (Colorfilter filter) method does not speak, today to simple analysis, in Android Colorfilter a total of 3 sub-categories, Colormatrixcolorfilter,lightingcolorfilter,porterduffcolorfilter, let's take a look at the first colormatrixcolorfilter today. The Colormatrixcolorfilter method is simple, one is an incoming array, and the other is an object passed into the ColorMatrix type.

    Public Colormatrixcolorfilter (ColorMatrix matrix) {        mmatrix.set (matrix);        Update ();    }
    Public Colormatrixcolorfilter (float[] array) {        if (Array.Length <) {            throw new ArrayIndexOutOfBoundsException ();        }        Mmatrix.set (array);        Update ();    }
Here's a look at the ColorMatrix class, which has an array marray inside, in fact he's saving a 4x5 color matrix,
*  [A, B, C, D, E, *    F, G, H, I, J, *    K, L, M, N, O, *    p, Q, R, S, T]
Can be used to modify the value of the ARGB, where the first row determines the red R, the second row determines the green G, the third row determines the blue B, the fourth row determines the transparency a, and the fifth column is the offset of the color

The ARGB value of the image is stored in a 5*1 color component matrix [R, G, B, a,1]. The result of the final operation is the multiplication of two matrices

   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;
We see that the size of Marray is 20, which is equivalent to an array of 4*5,
  Private final float[] Marray = new FLOAT[20];
The value of the matrix is initialized at initialization time.
    /**     * Set this ColorMatrix to identity:     * <pre>     * [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     * </pre>     */
   public void Reset () {        final float[] a = Marray;        Arrays.fill (A, 0);        A[0] = a[6] = a[12] = a[18] = 1;    }
The fifth column is the offset, such as
[1 0 0 0 8    0 1 0 0 8     0 0 1 0 8     

Represents a color component after the calculation is completed, add 8, where the last line represents transparency and is generally not modified. Let's take a demonstration.

public class Colorfilterview extends View {private Paint mpaint;private Bitmap mbitmap;private int padding = 12;float[] Co Lormatrix = {     1, 0, 0, 0, 0,//red    0, 0, 0, 0, 0,//green    0, 0, 0, 0, 0,//blue    0, 0, 0, 1, 0//alpha    };p rivate colormatrixcolorfilter mlightingcolorfilter= new Colormatrixcolorfilter (ColorMatrix);p ublic Colorfilterview (context context, AttributeSet Attrs) {Super (context, attrs); init ();} private void Init () {mpaint = new Paint (paint.anti_alias_flag); mbitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon);} @Overrideprotected void OnDraw (canvas canvas) {Super.ondraw (canvas), for (int i = 0; i < 8; i++) {Mpaint.setcolorfilter (  Mlightingcolorfilter); Canvas.drawbitmap (Mbitmap, (i% 4) * (Mbitmap.getwidth () + padding), (I/4) * (Mbitmap.getheight () + padding), mpaint);}}}
Look at the results.


Change it again.

Float[] ColorMatrix = {     1, 0, 0, 0, 0,//red    0, 1, 0, 0, 0,//green    0, 0, 0, 0, 0,//blue    0, 0, 0, 1, 0// Alpha    };
Look at the results of the operation


Finally, modify it, let him restore the normal picture

Float[] ColorMatrix = {     1, 0, 0, 0, 0,//red    0, 1, 0, 0, 0,//green    0, 0, 1, 0, 0,//blue    0, 0, 0, 1, 0// Alpha    };
Look at the results.


OK, the above demo is over, let's look at a matrix below.

[1 0 0 0 8    0 2 0 0 8     0 0 3 0 8     
He says the red component is offset by 8, the green component is offset by 8, and the blue component is offset by 8. Here's a look at the main method, Setscale (float rscale, float gscale, float bscale, float ascale)
    public void Setscale (float rscale, float gscale, float bscale,                         float ascale) {        final float[] a = Marray;        for (int i = n; i > 0; i) {            a[i] = 0;        }        A[0] = Rscale;        A[6] = Gscale;        A[12] = Bscale;        A[18] = Ascale;    }
This is almost the same as when the matrix was initialized, but the value here is not 1, which is the value we passed in, which represents the brightness, let's see
public class Colorfilterview extends View {private Paint mpaint;private Bitmap mbitmap;private ColorMatrix ColorMatrix = n EW ColorMatrix ();p rivate colormatrixcolorfilter matrixcolorfilter[] = new COLORMATRIXCOLORFILTER[24];p rivate int padding = 12;public Colorfilterview (context context, AttributeSet Attrs) {Super (context, attrs); init ();} private void Init () {mpaint = new Paint (paint.anti_alias_flag); mbitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon); for (int i = 0; i < i++) {if (I < 8) Colormatrix.setscale (i *. 1f, I *. 1f, I *. 1f, I *. 1f); El Se if (i <) Colormatrix.setscale (i *. 1f, I *. 1f, I *. 1f, I *. 1f); Elsecolormatrix.setscale (i *. 1f, I *. 1f, I *. 1f, I *. 1f); Matrixcolorfilter[i] = new Colormatrixcolorfilter (ColorMatrix);}} @Overrideprotected void OnDraw (canvas canvas) {Super.ondraw (canvas), for (int i = 0; i <; i++) {Mpaint.setcolorfilter (Matrixcolorfilter[i]); Canvas.drawbitmap (Mbitmap, (i% 4) * (Mbitmap.getwidth () + padding), (I/4) * (Mbitmap.getheight () + padding), mpaint);}}} 

Let's look at the results of the operation


Change it again.

for (int i = 0; i < i++) {if (I < 8) Colormatrix.setscale (i *. 1f, I *. 3f, I *. 9f, I *. 1f); else if (I < 16) Colormatrix.setscale (i *. 1f, I *. 3f, I *. 9f, I *. 1f); Elsecolormatrix.setscale (i *. 1f, I *. 3f, I *. 9f, I *. 1f); Matr Ixcolorfilter[i] = new Colormatrixcolorfilter (ColorMatrix);}
Look at the results of the operation

Look at another method setrotate (int axis, float degrees), which represents the hue

    /** * Set the rotation on a color axis by the specified values. * <p> * <code>axis=0</code> correspond to a rotation around the RED color * &LT;CODE&GT;AXIS=1&L  t;/code> correspond to a rotation around the GREEN color * <code>axis=2</code> correspond to a rotation        Around the BLUE color * </p> * * public void setRotate (int axis, float degrees) {reset ();        Double radians = degrees * MATH.PI/180D;        float cosine = (float) math.cos (radians);        float sine = (float) math.sin (radians);            Switch (axis) {//Rotation around the red color case 0:marray[6] = marray[12] = cosine;            Marray[7] = sine;            MARRAY[11] =-sine;        Break            Rotation around the green color case 1:marray[0] = marray[12] = cosine;            MARRAY[2] =-sine;            MARRAY[10] = sine;        Break Rotation around the blue color        Case 2:marray[0] = marray[6] = cosine;            Marray[1] = sine;            MARRAY[5] =-sine;        Break        Default:throw new RuntimeException (); }    }
Where axis is 0 represents the angle of rotation of the red component, which is the angle of rotation of the green component at 1 o'clock, and the angle of rotation of the blue component at 2 o'clock.
for (int i = 0; i < i++) {if (I < 8) colormatrix.setrotate (0, i*50); else if (I <) colormatrix.setrotate (1, I *50); Elsecolormatrix.setrotate (2, i*50); Matrixcolorfilter[i] = new Colormatrixcolorfilter (ColorMatrix);}
Look at the results of the operation

Change it again.

for (int i = 0; i < i++) {if (I < 8) colormatrix.setrotate (0, i*50), else if (I <) colormatrix.setrotate (1, ( i%8) *50) elsecolormatrix.setrotate (2, (i%8) *50); Matrixcolorfilter[i] = new Colormatrixcolorfilter (ColorMatrix);}

Look at the results of the operation


Another way to look at Setsaturation (float sat), which represents saturation, where 0 is gray and 1 is normal

for (int i = 0; i < i++) {if (I < 8) colormatrix.setsaturation (i*.2f); else if (I <) colormatrix.setsaturatio N (i*.5f); elsecolormatrix.setsaturation (i*2f); Matrixcolorfilter[i] = new Colormatrixcolorfilter (ColorMatrix);}
Run the result as


Setconcat (ColorMatrix MatA, ColorMatrix MATB), multiply two matrices

 public void        Setconcat (ColorMatrix MatA, ColorMatrix MATB) {float[] tmp;        if (MatA = = This | | matb = = this) {tmp = new float[20];        } else {tmp = Marray;        } final float[] a = Mata.marray;        Final float[] B = matb.marray;        int index = 0; for (int j = 0; J <, J + = 5) {for (int i = 0; i < 4; i++) {tmp[index++] = a[j + 0] *            B[i + 0] + a[j + 1] * b[i + 5] + a[j + 2] * b[i + ten] + a[j + 3] * b[i + 15]; } tmp[index++] = a[j + 0] * B[4] + a[j + 1] * B[9] + a[j + 2] * b[14] + a[j + 3] * b        [+] + a[j + 4];        } if (tmp! = Marray) {system.arraycopy (tmp, 0, Marray, 0, 20); }    }
Preconcat (ColorMatrix Prematrix), followed by Postconcat (ColorMatrix Postmatrix), called Setconcat (ColorMatrix MatA, ColorMatrix MATB) method, because the multiplication of matrices does not have a commutative law, changing the position of two matrices produces different results.

Another subclass of Colorfilter. Lightingcolorfilter Ray color Filter, just a construction method

    Public lightingcolorfilter (int mul, int add) {        Mmul = mul;        MADD = add;        Update ();    }
Mul represents a multiple of the color increase, add adds color,
public class Colorfilterview extends View {private Paint mpaint;private Bitmap mbitmap;private lightingcolorfilter mlight ingcolorfilter[] = new Lightingcolorfilter[8];p rivate int padding = 12;public Colorfilterview (context context, AttributeSet attrs) {Super (context, attrs); init ();} private void Init () {mpaint = new Paint (paint.anti_alias_flag); mbitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon);//constant mlightingcolorfilter[0] = new Lightingcolorfilter (0xffffffff,0x00000000);// Remove red mlightingcolorfilter[1] = new Lightingcolorfilter (0xff00ffff,0x00000000);//Remove green mlightingcolorfilter[3] = new Lightingcolorfilter (0xffff00ff,0x00000000);//Remove the blue mlightingcolorfilter[4] = new Lightingcolorfilter (0xFFFFFF00, 0x00000000);//Add red mlightingcolorfilter[5] = new Lightingcolorfilter (0xffffffff,0x00560000);// Add Green mlightingcolorfilter[6] = new Lightingcolorfilter (0xffffffff,0x00006400);//Add blue mlightingcolorfilter[7] = new Lightingcolorfilter (0xffffffff,0x00000056);} @Overrideprotected void OnDraw(Canvas canvas) {Super.ondraw (canvas); for (int i = 0; i < 8; i++) {mpaint.setcolorfilter (mlightingcolorfilter[i]); Canvas.drawbitmap ( Mbitmap, (i% 4) * (Mbitmap.getwidth () + padding), (I/4) * (mbitmap.getheight () + padding), mpaint);}}}
Run the result as


And then look at the last subclass of Colorfilter Porterduffcolorfilter, and he's just one way to construct it.

    /**     * Create A color filter that uses the specified color and porter-duff mode.     *     * @param color the ARGB source color used with the specified Porter-duff mode     * @param mode the Porter-duff mode That is applied     *     * @see Color     * @see #setColor (int)     * @see #setMode (Android.graphics.PorterDuff.Mode)     *    /public porterduffcolorfilter (@ColorInt int color, @NonNull porterduff.mode Mode) {        Mcolor = color;< C12/>mmode = mode;        Update ();    }
Let's take a look.
public Class Colorfilterview extends View {private Paint mpaint;private Bitmap mbitmap;private int length = PorterDuff.Mode.value S (). Length;private porterduffcolorfilter mlightingcolorfilter[] = new Porterduffcolorfilter[length];p rivate int padding = 12;public Colorfilterview (context context, AttributeSet Attrs) {Super (context, attrs); init ();} private void Init () {mpaint = new Paint (paint.anti_alias_flag); mbitmap = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.ICON); int i = 0;for (Porterduff.mode e:porterduff.mode.values ()) mlightingcolorfilter[i++] = new Porterduffcolorfilter (color.yellow,e);} @Overrideprotected void OnDraw (canvas canvas) {Super.ondraw (canvas), for (int i = 0; i < length; i++) {Mpaint.setcolorfi Lter (Mlightingcolorfilter[i]); Canvas.drawbitmap (Mbitmap, (i% 4) * (Mbitmap.getwidth () + padding), (I/4) * ( Mbitmap.getheight () + padding), mpaint);}} 
Run the result as

The mode is the 18 kinds of blending modes mentioned before in the detailed use of Android paint, OK, so far, has been analyzed.

Android Paint Colorfilter Detailed

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.