【Android】使用SeekBar调整图像的色度、饱和及度亮度

来源:互联网 发布:淘宝海外旗舰店真假 编辑:程序博客网 时间:2024/05/29 09:06

SeekBar通过滑块位置来标识数值,且允许用户拖动滑块来改变值。下面利用三条SeeKBar来调整图片的色度(Hue)、饱和度(Saturation)、亮度(Lum)。

布局文件为一个ImageView,三条SeekBar,布局简单,在此省略。

首先创建一个图像处理类ImageHelper:

public class ImageHelper {    public static Bitmap handleImage(Bitmap bmp, float hue, float saturation, float lum){        Bitmap bm = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(bm);        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);        ColorMatrix hueMatrix = new ColorMatrix();        hueMatrix.setRotate(0,hue);        hueMatrix.setRotate(1,hue);        hueMatrix.setRotate(2,hue);        ColorMatrix satMatrix = new ColorMatrix();        satMatrix.setSaturation(saturation);        ColorMatrix lumMatrix = new ColorMatrix();        lumMatrix.setScale(lum,lum,lum,1); //涓変釜閫氶亾鍜岄�忔槑搴�        ColorMatrix imgMatrix = new ColorMatrix(); //涓変釜ColorMatrix杩涜铻嶅悎        imgMatrix.postConcat(hueMatrix);        imgMatrix.postConcat(satMatrix);        imgMatrix.postConcat(lumMatrix);        paint.setColorFilter(new ColorMatrixColorFilter(imgMatrix));        canvas.drawBitmap(bmp,0,0,paint);        return bm;    }}
下面是MainActivity中的处理:

/** * Created by dell on 2016/1/10. */public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener {    private TextView textView;    private SeekBar hue_bar, sat_bar, lum_bar;    private Button cancel_btn, ok_btn;    private ImageView hsl_img;    private static int MAX_VALUE = 255;    private static int MID_VALUE = 127;    private float mHue, mSaturation, mLum;    private Bitmap bitmap;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initViews();    }    private void initViews() {    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lin);        textView = (TextView) findViewById(R.id.textView);        hue_bar = (SeekBar) findViewById(R.id.seekBar_h);        sat_bar = (SeekBar) findViewById(R.id.seekBar_s);        lum_bar = (SeekBar) findViewById(R.id.seekBar_l);        hsl_img = (ImageView) findViewById(R.id.hsl_img);        hue_bar.setOnSeekBarChangeListener(this);        sat_bar.setOnSeekBarChangeListener(this);        lum_bar.setOnSeekBarChangeListener(this);        hue_bar.setMax(MAX_VALUE); //设置最大值        sat_bar.setMax(MAX_VALUE);        lum_bar.setMax(MAX_VALUE);        hue_bar.setProgress(MID_VALUE); //设置初始值        sat_bar.setProgress(MID_VALUE);        lum_bar.setProgress(MID_VALUE);                        hsl_img.setImageBitmap(bitmap);           }    @Override    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {        switch (seekBar.getId()){            case R.id.seekBar_h:                mHue = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180;                break;            case R.id.seekBar_s:                mSaturation = progress * 1.0F / MID_VALUE;                break;            case R.id.seekBar_l:                mLum = progress * 1.0F / MID_VALUE;                break;        }        hsl_img.setImageBitmap(ImageHelper.handleImage(bitmap,mHue,mSaturation,mLum));    }    @Override    public void onStartTrackingTouch(SeekBar seekBar) {    }    @Override    public void onStopTrackingTouch(SeekBar seekBar) {    }}
SeekBar需要绑定的是setOnSeekBarChangeListener来实现对拖动位置的改变,里面有三个方法,主要需要重写的是
void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
progress代表监测到的当前滑动位置。


最后,附上实验效果,打开界面,并进行调节:
 




0 0
原创粉丝点击