Android学习笔记之-:对Android图像色调饱和度亮度处理
2015-08-12 20:48
441 查看
首先也简单介绍下图像的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>
处理工具类及方法:
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>
相关文章推荐
- 深入浅出 - Android系统移植与平台开发(六)- 为Android启动加速
- 浅析android中handler与Message(一)
- 安卓开发中的折线图
- 只需一步教你解决Win10下Android Studio terminal无法使用的问题
- Android入门第十六篇之Style与Theme
- 史上最简单的Ubuntu上搭建java和Android开发环境
- Android 不归路之百度地图API学习(导航篇)
- Android LayoutCast 初探
- Android.SQLite数据库:初始化数据库并插入默认数据记录注意点
- Android 安装应用后点击打开带来的问题
- android使用adb命令卸载程序,当有多个模拟器设备和真机设备时
- Debian编译Android源码
- android jni方法模拟高频按键点击
- Android学习之浅谈selector与Shape用法
- Android 进阶 教你打造 Android 中的 IOC 框架 【ViewInject】 (下) .
- Android 进阶 教你打造 Android 中的 IOC 框架 【ViewInject】 (上)
- 获取Toolbar中title的TextView
- 《第一行代码--Android》阅读笔记之数据持久化
- Intent相关知识(2)
- Android Fragment