本文主要是记录学习情况, 不是非常合适当做教程, 水平有限, 讲得不好.
首先上图
图中三个SeekBar分别代表hue色相, saturation饱和度跟lum亮度
布局代码
<LinearLayout 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" android:paddingTop="30dp" android:paddingLeft="20dp" android:paddingRight="20dp" tools:context=".MainActivity" android:orientation="vertical" > <ImageView android:layout_width="300dp" android:layout_height="300dp" android:id="@+id/id_image" android:background="@drawable/image_1" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/id_seekbar1" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/id_seekbar2" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/id_seekbar3" /> </LinearLayout>
主界面,activity代码
package com.example.imageprocess; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView; import android.widget.SeekBar; public class PrimaryActivity extends Activity implements SeekBar.OnSeekBarChangeListener { private Context context; private static final String TAG = ""; private Bitmap bitmap; private ImageView imageView; private SeekBar seekBar1; private SeekBar seekBar2; private SeekBar seekBar3; private static int MAX_VALUE = 255; private static int MID_VALUE = 127; private float mHue, mSaturation, mLua; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.process_image_activity); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_1); imageView = (ImageView) findViewById(R.id.id_image); imageView.setImageBitmap(bitmap); seekBar1 = (SeekBar) findViewById(R.id.id_seekbar1); seekBar2 = (SeekBar) findViewById(R.id.id_seekbar2); seekBar3 = (SeekBar) findViewById(R.id.id_seekbar3); seekBar1.setOnSeekBarChangeListener(this); seekBar2.setOnSeekBarChangeListener(this); seekBar3.setOnSeekBarChangeListener(this); seekBar1.setMax(MAX_VALUE); seekBar2.setMax(MAX_VALUE); seekBar3.setMax(MAX_VALUE); seekBar1.setProgress(MID_VALUE); seekBar2.setProgress(MID_VALUE); seekBar3.setProgress(MID_VALUE); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub switch (seekBar.getId()) { case R.id.id_seekbar1: mHue = (progress - MID_VALUE) * 1.0f / MID_VALUE * 180; break; case R.id.id_seekbar2: mSaturation = progress * 1.0f / MID_VALUE; break; case R.id.id_seekbar3: mLua = progress * 1.0f / MID_VALUE; break; } imageView.setImageBitmap(ImageHelper.handleImageEffect(bitmap, mHue, mSaturation, mLua)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } }
其中,
MAX_VALUE = 255;//代表seekBar的最大值
MID_VALUE = 127;//seekbar的中间值
在onProgressChanged函数中得三个公式, 是一个经验公式, 是从视频上得知的, 在此就用上了
重点就是ImageHelper里面的处理代码
public class ImageHelper { /** * 用系统的matrix来处理图片 * * @param bm * @param hue * @param saturation * @param lua * @return */ public static Bitmap handleImageEffect(Bitmap bm, Float hue, Float saturation, Float lua) { // 参数bitmap是final 不可修改 所以新建一个bitmap Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); ColorMatrix hueMatrix = new ColorMatrix(); hueMatrix.setRotate(0, hue);// R hueMatrix.setRotate(1, hue);// G hueMatrix.setRotate(2, hue);// B ColorMatrix saturationMatrix = new ColorMatrix(); saturationMatrix.setSaturation(saturation); ColorMatrix luaMatrix = new ColorMatrix(); luaMatrix.setScale(lua, lua, lua, 1); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.postConcat(hueMatrix); colorMatrix.postConcat(saturationMatrix); colorMatrix.postConcat(luaMatrix); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bm, 0, 0, paint); return bmp; } }