话不多说,直接上代码,通过代码与效果进行学习,希望可以帮到大家,同时也让自己理解深刻。
先来看布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/hanker_paint_color_matrix_img" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" android:src="@drawable/baby"/> <LinearLayout android:paddingTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="饱和度(0-20):"/> <SeekBar android:id="@+id/hanker_paint_color_matrix_seekbar" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
布局布局很简单,这里就不详解了
然后看看Activity中的处理代码:
public class PaintColorMatrixSetSaturationActivity extends BaseActivity { private SeekBar mSeekBar; private ImageView mImageView; private Bitmap mOriginBmp, mTempBmp; @Override protected int getLayoutResID() { return R.layout.hanker_paint_color_matrix_set_saturation; } @Override protected void initView() { mImageView = (ImageView) findViewById(R.id.hanker_paint_color_matrix_img); mSeekBar = (SeekBar) findViewById(R.id.hanker_paint_color_matrix_seekbar); mOriginBmp = BitmapFactory.decodeResource(getResources(), R.drawable.baby); mTempBmp = Bitmap.createBitmap(mOriginBmp.getWidth(), mOriginBmp.getHeight(), Bitmap .Config.ARGB_8888); } @Override protected void initData() { //滑块默认在一的位置,向左到底是0,向右到底是20(即饱和度放大20倍) mSeekBar.setMax(20); mSeekBar.setProgress(1); } @Override protected void initListener() { mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Bitmap bitmap = handleColorMatrixBmp(progress); mImageView.setImageBitmap(bitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } private Bitmap handleColorMatrixBmp(int progress) { //创建一个相同尺寸的可变的位图区,用于绘制调色后的图片 Canvas canvas = new Canvas(mTempBmp); Paint paint = new Paint();//新建Paint paint.setAntiAlias(true);//边缘平滑处理 ColorMatrix mSaturationMatrix = new ColorMatrix(); mSaturationMatrix.setSaturation(progress); paint.setColorFilter(new ColorMatrixColorFilter(mSaturationMatrix));//设置颜色变换效果 canvas.drawBitmap(mOriginBmp,0,0,paint);//将颜色变化后的图片输出到新创建的位图区 //返回新位图,也即调色处理后的图片 return mTempBmp; } @Override public void onClick(View v) { } }
然后运行程序,默认是这样的:
当取值为0时,效果是这样的:
我们在将取值放在,效果是这样的:
呃。。。不怎么好看,但这不是重点,重点是我们达到想要的效果,大家可以自由去移动所设置的值,达到不同的效果