首先也简单介绍下图像的RGBA模型,R指红色(Red),G指绿色(Green),B指蓝色(Blue)及A指透明度(Alpha),由这四种元素搭配组合成了各种各样的颜色。
处理工具类及方法:
public class ImageTools {
/**
* 对图片进行处理
* @description:
* @date 2015-8-12 下午8:45:05
*/
public static Bitmap getColorImage(Bitmap bitmap, float sx, float bhd, float ld) {// 参数分别是色相,饱和度和亮度
Bitmap bmp = bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
ColorMatrix sxMatrix = new ColorMatrix();// 设置色调
sxMatrix.setRotate(0, sx);
sxMatrix.setRotate(1, sx);
sxMatrix.setRotate(2, sx);
ColorMatrix bhdMatrix = new ColorMatrix();// 设置饱和度
bhdMatrix.setSaturation(bhd);
ColorMatrix ldMatrix = new ColorMatrix();// 设置亮度
ldMatrix.setScale(ld, ld, ld, 1);
ColorMatrix mixMatrix = new ColorMatrix();// 设置整体效果
mixMatrix.postConcat(sxMatrix);
mixMatrix.postConcat(bhdMatrix);
mixMatrix.postConcat(ldMatrix);
paint.setColorFilter(new ColorMatrixColorFilter(mixMatrix));// 用颜色过滤器过滤
canvas.drawBitmap(bmp, 0, 0, paint);// 重新画图
return bmp;
}
}
在Activity中选择要处理的ImageView及对应的Bitmap,调用工具类中方法即可:
private ImageView colorIv;
private SeekBar sxBar, bhdBar, ldBar;
private static int MIN_COLOR = 100;
private static int MAX_COLOR = 255;
private float sx, bhd, ld;
private Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_main);
initViews();
}
private void initViews() {
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
colorIv = (ImageView) findViewById(R.id.color_iv);
colorIv.setImageBitmap(bmp);
sxBar = (SeekBar) findViewById(R.id.sx_seekbar);
bhdBar = (SeekBar) findViewById(R.id.bhd_seekbar);
ldBar = (SeekBar) findViewById(R.id.ld_seekbar);
sxBar.setOnSeekBarChangeListener(this);
sxBar.setMax(MAX_COLOR);// 设置最大值
sxBar.setProgress(MIN_COLOR);// 设置初始值(当前值)
bhdBar.setOnSeekBarChangeListener(this);
bhdBar.setMax(MAX_COLOR);
bhdBar.setProgress(MIN_COLOR);
ldBar.setOnSeekBarChangeListener(this);
ldBar.setMax(MAX_COLOR);
ldBar.setProgress(MIN_COLOR);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.sx_seekbar:
sx = (progress - MIN_COLOR) * 1.0f / MIN_COLOR * 180;
break;
case R.id.bhd_seekbar:
bhd = progress * 1.0f / MIN_COLOR;
break;
case R.id.ld_seekbar:
ld = progress * 1.0f / MIN_COLOR;
break;
}
colorIv.setImageBitmap(ImageTools.getColorImage(bmp, sx, bhd, ld));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
xml布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/color_iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp" />
<SeekBar
android:id="@+id/sx_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/color_iv"
/>
<SeekBar
android:id="@+id/bhd_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sx_seekbar"
android:layout_marginTop="10dp" />
<SeekBar
android:id="@+id/ld_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bhd_seekbar"
android:layout_marginTop="10dp" />
</RelativeLayout>