超级简单的图片冷暖色、灰度、高亮图片实现效果
第一种实现方式:
只需要传进rgb?对应的值,就可以修改图片颜色
public void imagec(int r, int g, int b) {
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.a11);
ColorMatrix colorMatrix=new ColorMatrix(new float[]{
1,0,0,0,r*10,
0,1,0,0,g*10,
0,0,1,0,b*10,
0,0,0,1,0});
ColorMatrixColorFilter colorMatrixColorFilter=new ColorMatrixColorFilter(colorMatrix);
Bitmap newBitmap=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(newBitmap);
Paint paint=new Paint();
paint.setColorFilter(colorMatrixColorFilter);
canvas.drawBitmap(bitmap,0,0,paint);
imageView.setImageBitmap(newBitmap);
}
第二种实现方式
只需要传进图片bitmap?就可以修改图片颜色
public Bitmap handleImageNegative(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
int color;
int r, g, b, a;
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPx = new int[width * height];
int[] newPx = new int[width * height];
bm.getPixels(oldPx, 0, width, 0, 0, width, height);
for (int i = 0; i < width * height; i++) {
color = oldPx[i];
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
a = Color.alpha(color);
//
r = 255 - r;
g = 255 - g;
b = 255 - b;
if (r > 255) {
r = 255;
} else if (r < 0) {
r = 0;
}
if (g > 255) {
g = 255;
} else if (g < 0) {
g = 0;
}
if (b > 255) {
b = 255;
} else if (b < 0) {
b = 0;
}
newPx[i] = Color.argb(a, r, g, b);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
return bmp;
}
|