您的位置:首页 > 其它

图片饱和度,色相,亮度调整

2016-09-23 14:56 417 查看
改变图片颜色,会让图片更具灵活性和趣味性,那我们通常在修改图片的像素色差时,如何实现呢?

效果图:

饱和度:



色相:



亮度:



OK,看完效果了,那么我们就用最简单的方式实现这几种有逼格的效果吧:

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/showview_iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitXY"
android:src="@drawable/img_3" />

<TextView
android:id="@+id/progress_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/showview_iv"
android:layout_marginLeft="200dp"
android:layout_marginTop="30dp"
android:text="50" />

<TextView
android:id="@+id/baoheduText_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progress_tv"
android:text="饱和度:" />

<SeekBar
android:id="@+id/baohedu_seekbar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@+id/progress_tv"
android:layout_toRightOf="@+id/baoheduText_tv"
android:max="100"
android:progress="50" />

<TextView
android:id="@+id/sexiangText_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/baoheduText_tv"
android:layout_marginTop="10dp"
android:text="    色相:" />

<SeekBar
android:id="@+id/sexiang_seekbar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@+id/baoheduText_tv"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/sexiangText_tv"
android:max="100"
android:progress="50" />

<TextView
android:id="@+id/liangduText_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/sexiangText_tv"
android:layout_marginTop="10dp"
android:text="    亮度:" />

<SeekBar
android:id="@+id/liangdu_seekbar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@+id/sexiangText_tv"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/liangduText_tv"
android:max="100"
android:progress="50" />

</RelativeLayout>


在activity中:

public class RecPictrueActivity extends Activity {

private ImageView showview_iv;
private TextView progress_tv;
private SeekBar baohedu_seekbar, sexiang_seekbar, liangdu_seekbar;

// 用于颜色变换的矩阵,android位图颜色变化处理主要是靠该对象完成
private ColorMatrix mSaturationMatrix = new ColorMatrix();//饱和度
private ColorMatrix mHueMatrix = new ColorMatrix();//色相
private ColorMatrix mLightnessMatrix = new ColorMatrix();//亮度

private ColorMatrix mAllMatrix = new ColorMatrix();

/**
* 饱和度
*/
private float mSaturationValue = 0F;
/**
* 色相
*/
private float mHueValue = 0F;
/**
* 亮度
*/
private float mLightnessValue = 1F;

//运算像素和颜色值的比例,是最大值的1/2
private final int MIDDLE_VALUE = 50;

//原始图片
Bitmap srcBitmap = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baohedu_layout);

showview_iv = (ImageView) findViewById(R.id.showview_iv);
srcBitmap = ((BitmapDrawable) showview_iv.getDrawable()).getBitmap();

progress_tv = (TextView) findViewById(R.id.progress_tv);
baohedu_seekbar = (SeekBar) findViewById(R.id.baohedu_seekbar);
sexiang_seekbar = (SeekBar) findViewById(R.id.sexiang_seekbar);
liangdu_seekbar = (SeekBar) findViewById(R.id.liangdu_seekbar);

addListener(baohedu_seekbar, sexiang_seekbar, liangdu_seekbar);
}

private void addListener(SeekBar... seekbars) {
for (SeekBar seekbar : seekbars)
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
switch (seekBar.getId()) {
case R.id.baohedu_seekbar:
//饱和度表现为距离色轮中心的距离
//颜色相对于所选像素的起始颜色值,从色轮中心向外移动,或从外向色轮中心移动。数值的范围可以从 -100 到 +100。
mSaturationValue = (float) (i * 1.0D / MIDDLE_VALUE);
//饱和度
mSaturationMatrix.reset();
mSaturationMatrix.setSaturation(mSaturationValue);
break;
case R.id.sexiang_seekbar:
//色相表现为色轮的旋转角度,正值表示顺时针旋转,负值表示逆时针旋转
//正值表示顺时针旋转,负值表示逆时针旋转。数值的范围可以从 -180 到 +180。
mHueValue = (float) ((i - MIDDLE_VALUE) * 1.0D / MIDDLE_VALUE * 180);
mHueMatrix.reset();
mHueMatrix.setRotate(0, mHueValue); // 控制让红色区在色轮上旋转hueColor葛角度
mHueMatrix.setRotate(1, mHueValue); // 控制让绿红色区在色轮上旋转hueColor葛角度
mHueMatrix.setRotate(2, mHueValue); // 控制让蓝色区在色轮上旋转hueColor葛角度
break;
case R.id.liangdu_seekbar:
//明亮度则表现为RGB各分量的大小,0表示最暗,255表示最亮
mLightnessValue = (float) (i * 1.0D / MIDDLE_VALUE);
mLightnessMatrix.reset();
// 红、绿、蓝三分量按相同的比例,最后一个参数1表示透明度不做变化,此函数详细说明参考
mLightnessMatrix.setScale(mLightnessValue, mLightnessValue, mLightnessValue, 1);
break;
}

Bitmap bitmap = resetPictrue();
showview_iv.setImageBitmap(bitmap);
progress_tv.setText(i + "");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
}

public Bitmap resetPictrue() {
mAllMatrix.reset();
// 效果叠加
mAllMatrix.postConcat(mSaturationMatrix);
mAllMatrix.postConcat(mHueMatrix);
mAllMatrix.postConcat(mLightnessMatrix);

Bitmap bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.ARGB_8888);
// 创建一个相同尺寸的可变的位图区,用于绘制调色后的图片
Canvas canvas = new Canvas(bmp); // 得到画笔对象
Paint paint = new Paint(); // 新建paint
paint.setAntiAlias(true); // 设置抗锯齿,也即是边缘做平滑处理
paint.setColorFilter(new ColorMatrixColorFilter(mAllMatrix));// 设置颜色变换效果
canvas.drawBitmap(srcBitmap, 0, 0, paint); // 将颜色变化后的图片输出到新创建的位图区
// 返回新的位图,也即调色处理后的图片
return bmp;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片