Android对于图片的额处理,最常使用后的数据结构是位图-Bitmap,它包含了一张图片的所有数据。整个图片都是由点阵和颜色值组成的。所谓点阵几句是一个包含像素的矩阵,每一个元素对应着图片的一个像素。颜色值ARGB,分别代表着透明度、红、绿、蓝这四个通道的分量,他们共同决定每个像素点显示的颜色。
色彩矩阵分析
在色彩处理中,通常使用以下三个角度来描述一个图像。
- 色调–物体传播的颜色
- 饱和度–颜色的纯度,从0(灰)到100%(灰)
亮度–颜色的相对明暗程度
在Android中,系统使用一个颜色矩阵–ColorMatrix来处理图像的这些色彩效果。Android 中的颜色矩阵是一个4X5的数字矩阵,它用来对图片进行处理。
改变色光的属性
在Android中,系统封装了一个类–ColorMarix颜色矩阵来改变矩阵值处理颜色效果。
色调:setRotate(int axis, float degrees) 参数一:取值0、1、2,参数二:改变的值
亮度:setScale(float rScale, float gScale, float bScale,float aScale)
- 饱和度:setSaturation(float sat),参数代表饱和度的值。
除了单独使用之外,他们还可以使用postConcat()叠加处理。使用如下
代码:
/**
* 关于颜色和图形的绘制
* 通过改变SeekBar的值,修改图片的色调,饱和度和亮度改变一张图片
* @author fanshenxia
*
*/
public class ColorActivity extends Activity implements OnSeekBarChangeListener {
private static int MAX_VALUE = 255;
private static int MID_VALUE = 127;
private Bitmap mBitmap;
private float mNum1, mNum2, mNum3;
/**
* mSeekBar1:改变色调 mSeekBar1:改变饱和度 mSeekBar1:亮度
*/
private SeekBar mSeekBar1, mSeekBar2, mSeekBar3;
private ImageView mImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color);
init();
}
private void init() {
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beautifuimg);
mSeekBar1 = findViewById(R.id.seekbar1);
mSeekBar2 = findViewById(R.id.seekbar2);
mSeekBar3 = findViewById(R.id.seekbar3);
mImg = findViewById(R.id.img);
mSeekBar1.setMax(MAX_VALUE);
mSeekBar2.setMax(MAX_VALUE);
mSeekBar3.setMax(MAX_VALUE);
mSeekBar1.setProgress(MID_VALUE);
mSeekBar2.setProgress(MID_VALUE);
mSeekBar3.setProgress(MID_VALUE);
mSeekBar1.setOnSeekBarChangeListener(this);
mSeekBar2.setOnSeekBarChangeListener(this);
mSeekBar3.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (seekBar == mSeekBar1) {
mNum1 = (progress - MID_VALUE) * 1F / MID_VALUE * 180;
} else if (seekBar == mSeekBar2) {
mNum2 = progress * 1F / MID_VALUE;
} else {
mNum3 = progress * 1F / MID_VALUE;
}
// 设置图片
mImg.setImageBitmap(getMatrixBitmap(mBitmap));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
private Bitmap getMatrixBitmap(Bitmap bitmap) {
Bitmap curBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(curBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix1 = new ColorMatrix();
colorMatrix1.setRotate(0, mNum1);
colorMatrix1.setRotate(1, mNum2);
colorMatrix1.setRotate(2, mNum3);
ColorMatrix colorMatrix2 = new ColorMatrix();
colorMatrix2.setSaturation(mNum2);
ColorMatrix colorMatrix3 = new ColorMatrix();
colorMatrix3.setScale(mNum3, mNum3, mNum3, 1);
ColorMatrix imageMatrix = new ColorMatrix();
imageMatrix.postConcat(colorMatrix1);
imageMatrix.postConcat(colorMatrix2);
imageMatrix.postConcat(colorMatrix3);
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
canvas.drawBitmap(bitmap, 0, 0, paint);
return curBitmap;
}
}
显示效果: