Android:滚动搜索栏时更改图像对比度和亮度

时间:2014-12-28 18:28:07

标签: android imageview brightness android-seekbar contrast

我想设置一个imageview的对比度和亮度。从How to programmatically change contrast of a bitmap in android?引用,这应该是最快速有效的方法,我已经实现了如下代码:

代码:

final SeekBar SeekBar_contrast = (SeekBar) dialog_preview.findViewById(R.id.SeekBar_contrast);
        final SeekBar SeekBar_brightness = (SeekBar) dialog_preview.findViewById(R.id.SeekBar_brightness);
        SeekBar_contrast.setMax(10);
        SeekBar_brightness.setMax(510);
        SeekBar_contrast.setProgress((1));
        SeekBar_brightness.setProgress((255));              
        SeekBar_contrast.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
             @Override
             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
             {  
                    new_bm = changeBitmapContrastBrightness(bimap, (float)(progress-1),(float)(SeekBar_brightness.getProgress()-255));
                    cropImageView.setImageBitmap(new_bm);
             } 

             @Override
             public void onStartTrackingTouch(SeekBar seekBar) 
             {
             } 

             @Override
             public void onStopTrackingTouch(SeekBar seekBar) 
             {              
             }                  
        });
        SeekBar_brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
             @Override
             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
             {  
                    new_bm = changeBitmapContrastBrightness(bimap, (float)(SeekBar_contrast.getProgress()-1), (float)(progress-255));
                    cropImageView.setImageBitmap(new_bm);
             } 

             @Override
             public void onStartTrackingTouch(SeekBar seekBar) 
             {
             } 

             @Override
             public void onStopTrackingTouch(SeekBar seekBar) 
             {

             }                  
        });

方法:

public static Bitmap changeBitmapContrastBrightness(Bitmap bmp, float contrast, float brightness)
{
    ColorMatrix cm = new ColorMatrix(new float[]
            {
                contrast, 0, 0, 0, brightness,
                0, contrast, 0, 0, brightness,
                0, 0, contrast, 0, brightness,
                0, 0, 0, 1, 0
            });

    Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

    Canvas canvas = new Canvas(ret);

    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    canvas.drawBitmap(bmp, 0, 0, paint);

    return ret;
}

参数如下:

 @param bmp input bitmap
 * @param contrast 0..10 1 is default
 * @param brightness -255..255 0 is default
 * @return new bitmap

问题:

虽然亮度默认值为0,这是范围的中间,因此滚动亮度搜索栏,它工作正常,我想问一下如何设置搜索栏最大值和对比度的进度变化,如默认值为1,它不是范围的中间值。

在搜索seekbar_contrast时,上面的代码会使图像显示“负色”"在进度= 0时,当向右滚动(搜索条的较大值)时,它会产生白色图像。

1 个答案:

答案 0 :(得分:1)

首先,在onProgressChanged对比搜索栏的监听器中,你必须为搜索树的亮度值做 -255

为了使对比度的默认值放在中间,请使用以下代码:

    SeekBar_contrast.setMax(10);
    SeekBar_contrast.setProgress((5));
         @Override
         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {  
                if (progress <= 5) {
                    progress = 1 - progress / 5;                
                } else {
                    progress = 1 + (progress / 10) * 9 
                }
                new_bm = changeBitmapContrastBrightness(bimap, (float)(progress-1),(float)SeekBar_brightness.getProgress() - 255);
                cropImageView.setImageBitmap(new_bm);
         }

如果你想增加对比度更平滑 - 尝试使用任何非线性函数。