Android Matrix (i): ColorMatrix

Source: Internet
Author: User

The recent learning process to see the content of the Android color matrix, previously seen this part of the content is basically skipped, not carefully read.

The excuses you find for yourself are:

1, freshman study of the matrix content has long forgotten clean, when learning is very annoying, so now also simply skip don't look at it, anyway, I do not do image processing and other tasks.

2,android in color, there are ready-made setsaturation-set saturation ,setscale-color scaling, setrotate-color rotation method, there is no need to learn colormatrix it.

                               

                

Of course, later still on this excuse to reflect on, can not always avoid the matrix related content bar, so out of a freshman "linear algebra", turned over the matrix of related knowledge.

The following summary of the matrix of some simple knowledge, not deep, but belong to a developer should grasp the degree.

   One: The source of the Matrix : We all know the form of matrices

       

But what is the source of the Matrix, in fact, the matrix is originally from the equation Group of coefficients and constants of the square.

Like what:

      

The corresponding matrix array:

      

    

What is the use of this matrix array? Do not underestimate the matrix, where the matrix determines whether the equations have a solution, and if there is a solution, what the solution is.
In other words, the matrix can be used to solve the linear equations conveniently and quickly.

    Second: The operation of the Matrix

Addition of matrices:

    

Multiplication of matrices:

    

The addition operation condition must be the same type matrix, the multiplication condition is a is mxn Matrix,B is nxp Matrix.

    

Learn about these basic matrix concepts, and then look at ColorMatrix in Android.

We all know that in the Android image using ARGB to represent the color, each point in the picture has its own Rgba value, the RGBA value as only a column of the matrix, so that the matrix

Multiply the other matrix to change the color of the picture, and another matrix here is ColorMatrix.

First of all, based on matrix multiplication, we think that a 4*4 ColorMatrix is possible,

Like what:

But ask what the last ColorMatrix used the 4*5 matrix. The reason looks at the picture:

    

   

E,j,o,t is not a factor, and when we want to add a property without affecting others, we only need to modify the value of e,j,o,t.

    

    Basic Usage :

        ColorMatrix ColorMatrix = new ColorMatrix (new float[]{                  1, 0, 0, 0, 0,                  0, 1, 0, 0, 0,                  0, 0, 1, 0, 0,                  0, 0, 0, 1, 0,          });          Mpaint.setcolorfilter (New Colormatrixcolorfilter (ColorMatrix));    

    Practical Walkthrough:

     1, we want to achieve the effect of removing the blue

Just set ColorMatrix to

        ColorMatrix ColorMatrix = new ColorMatrix (new float[]{                  1, 0, 0, 0, 0,                  0, 1, 0, 0, 0,                  0, 0, 0, 0, 0, 0,                  0, 0, 1, 0,          

So the original image of the Rgba matrix and ColorMatrix multiplied by the result is rg0a, no B.

Effects we use a gadget in the Android Heroes book about ColorMatrix Learning:

Https://github.com/xurui1995/ColorMatrixStudy

Key code:

    Set the matrix value to the image    private void Setimagematrix () {        Bitmap bmp = Bitmap.createbitmap (                bitmap.getwidth (),                Bitmap.getheight (),                Bitmap.Config.ARGB_8888);        Android.graphics.ColorMatrix ColorMatrix =                new Android.graphics.ColorMatrix ();        Colormatrix.set (Mcolormatrix);        Canvas canvas = new canvas (BMP);        Paint paint = new paint ();        Paint.setcolorfilter (New Colormatrixcolorfilter (ColorMatrix));        Canvas.drawbitmap (bitmap, 0, 0, paint);        Mimageview.setimagebitmap (BMP);    }

Effect:

        

At this time you must think: "Lying trough, how so yellow?" It's OK, show you a picture and you'll get it right away.

    

    

2, answer the change effect according to ColorMatrix

        

The answer is obvious: the first effect is one-fold green. The second effect is to enhance the 100 components of red and green.

3, some other "effects", you can try more.

      

     

  

      

      

    

    

 

  

Android Matrix (i): ColorMatrix

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.