Convert a Bitmap to Gray Scale Android example

Black and white bitmap image pictures, gray scale colormatrix Android example.

public class ApokusActivity extends Activity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}
 
	private static class SampleView extends View {
 
		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);
 
		}
 
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
 
			canvas.drawColor(Color.YELLOW);
 
 
			// you need to insert a image flower_blue into res/drawable folder
 
			paint.setFilterBitmap(true);
			Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
					R.drawable.flower_blue);
			canvas.drawBitmap(bitmapOrg, 10, 10, paint);
			int width, height;
 
		    ColorMatrix cm = new ColorMatrix(
		    		new float[]{
		    		0.5f,0.5f,0.5f,0,0,
		            0.5f,0.5f,0.5f,0,0,
		            0.5f,0.5f,0.5f,0,0,
		            0,0,0,1,0,0,
		            0,0,0,0,1,0
		            });
		    cm.setSaturation(0);
		    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
		    paint.setColorFilter(f);
		    int h = bitmapOrg.getHeight();
			//canvas.drawBitmap(bitmapOrg, 10, 10, paint);
			canvas.drawBitmap(bitmapOrg, 10, 10 + h + 10, paint);
 
		}
 
	}
}

gry scale bitmap android

This entry was posted in Android Examples Code, Bitmap, drawing and tagged , , . Bookmark the permalink.


Comments are closed.



17,480 views