Algorithms for image nostalgia:
We use the color matrix to achieve our nostalgic effect. If you do not know how colormatrix works, refer to: Image Color Processing (colormatrix) for Android study notes)
This is the color matrix. We can use the above calculation method to change the value of our color matrix to achieve the desired effect.
The above calculation method can be converted:
M =
In Android, the color matrix M is a one-dimensional array M = [A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T] storage.
M = [0.393, 0.768, 0.189, 0.349, 0.686, 0.168, 0.272, 0.534, 0, 0, 0.131, 0, 0, 0, 0,]
Specific operations:
Custom View
Colorview. Java
Package COM. color; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. colormatrix; import android. graphics. colormatrixcolorfilter; import android. graphics. paint; import android. util. attributeset; import android. widget. imageview; public class colorview extends imageview {private paint mypaint = NULL; private Bitmap bitmap = NULL; private colormatrix mycolormatrix = NULL; // set the color value private float [] colorarray = {(float) 0.393, (float) 0.768, (float) 0.189, 0.349, (float) 0.686, (float, (float) 0.168, 0.272, (float) 0.534, (float) 0.131, (float), 0}; Public colorview (context, attributeset attrs) {super (context, attrs); bitmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. WW); invalidate () ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); // create a paint brush object mypaint = new paint (); // draw (original image) canvas. drawbitmap (bitmap, 0, 0, mypaint); // create a color matrix object mycolormatrix = new colormatrix (); // set the color matrix value mycolormatrix. set (colorarray); // set the paint color filter mypaint. setcolorfilter (New colormatrixcolorfilter (mycolormatrix); // draw (processed image) canvas. drawbitmap (bitmap, 0, 0, mypaint); invalidate ();}}
Main. xml layout File
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/colorView_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.color.ColorView android:id="@+id/myColorView" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
Original Image of nostalgia Effect
Method 2:
Separate the RGB values of each pixel, and then recalculate the RGB values based on the preceding three formulas as the RGB values of the current vertex.
Colorview. Java
Package COM. color; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. util. attributeset; import android. widget. imageview; public class colorview extends imageview {private paint mypaint = NULL; private Bitmap bitmap = NULL; private int width, height; Pu BLIC colorview (context, attributeset attrs) {super (context, attrs); bitmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. WW); width = bitmap. getwidth (); Height = bitmap. getheight (); invalidate () ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); int pixcolor = 0; int pixr = 0; int pixg = 0; int pixb = 0; int newr = 0; int newg = 0; int newb = 0; int [] pi Xels = new int [width * Height]; // get the image's pixels (one-dimensional array) /** pixels receives the array of Bitmap color values * offset is written to the row spacing value in the first pixel index value * stride pixels [] of pixels (must be greater than or equal to the bitmap width ). The X coordinate value of the first pixel that can be read from the bitmap by a negative number X. * Y the Y coordinate value of the first pixel read from the bitmap * width the pixel width read from each row * number of rows read by height */bitmap. getpixels (pixels, 0, width, 0, 0, width, height); // get an image pixel with a high height and width for (INT I = 0; I