The following function changes contrast and brightness in the picture successfully.
Bitmap bmp;
ImageView alteredImageView;
...
public void drawAlteredImage(float contr,float bright) {
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
contr, 0, 0, 0, bright,
0, contr, 0, 0, bright,
0, 0, contr, 0, bright,
0, 0, 0, 1, 0 });
paint.setColorFilter(new ColorMatrixColorFilter(cm));
Matrix matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
alteredImageView.setImageBitmap(alteredBitmap);
}
But when I added setSaturation method to ColorMatrix the contrast and brightness altering ceased to work. The code:
public void drawAlteredImage(float contr,float bright,float satur) {
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
contr, 0, 0, 0, bright,
0, contr, 0, 0, bright,
0, 0, contr, 0, bright,
0, 0, 0, 1, 0 });
cm.setSaturation(satur);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
Matrix matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
alteredImageView.setImageBitmap(alteredBitmap);
}
Only the saturation effect is applying in this case. Why does this problem happening? How can I fix it?
The
setSaturation(...)method replaces all the values in the matrix. You can either create a matrix that combines both operations, or do the operations with two separatedrawBitmap(...)calls.Try: