Give a PNG resource import, from RGB to grayscale and display the example, the code is as follows:
Public classGrayviewextendsView {PrivateBitmap bmp; PublicGrayview (Context context) {Super(context); Resources Res=getresources (); BMP=Bitmapfactory.decoderesource (res, r.drawable.ic_launcher); //Ic_launcher A resource to generate a bitmap object } @Overrideprotected voidOnDraw (canvas canvas) {//TODO auto-generated Method StubBitmap output =Bitmap.createbitmap (Bmp.getwidth (), Bmp.getheight (), config.argb_8888); //Define a Bitmap object of the same size as the original bitmap for(inti = 0; I < bmp.getwidth (); i++){ for(intj = 0; J < Bmp.getheight (); J + +){ intcolor =Bmp.getpixel (i, j); //Gets the pixel value of a coordinate intRed =color.red (Color); //Parse out values for R, G, b intGreen =Color.green (Color); intBlue =Color.Blue (Color); intTMP = (red + green + Blue)/3; //converted to grayscale value output.setpixel (i, J, Color.rgb (tmp,tmp,tmp)); //Assign values R, G, B} to the pixel points of the corresponding coordinates } canvas.drawbitmap (Output,100, 100,NULL); //Draw the bitmap on the canvas }}
Drawable (2, bitmap)