2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 图片饱和度 色相 亮度调整

图片饱和度 色相 亮度调整

时间:2019-02-21 07:32:59

相关推荐

图片饱和度 色相 亮度调整

改变图片颜色,会让图片更具灵活性和趣味性,那我们通常在修改图片的像素色差时,如何实现呢?

效果图:

饱和度:

色相:

亮度:

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

xml布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/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/2private final int MIDDLE_VALUE = 50;//原始图片Bitmap srcBitmap = null;@Overrideprotected 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() {@Overridepublic 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 + "");}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic 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(); // 新建paintpaint.setAntiAlias(true); // 设置抗锯齿,也即是边缘做平滑处理paint.setColorFilter(new ColorMatrixColorFilter(mAllMatrix));// 设置颜色变换效果canvas.drawBitmap(srcBitmap, 0, 0, paint); // 将颜色变化后的图片输出到新创建的位图区// 返回新的位图,也即调色处理后的图片return bmp;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。