在我的应用程序中,我希望能够在ImageView中更改图像的亮度.我为此目的使用了搜索栏.当我向右移动滚动条时,它会改变图像的亮度,但是当我想降低亮度并向左移动时,亮度会不断增加,图像会变得近乎白色.此外,使用搜索栏作为用户非常困难.它移动得不是很顺利.有人可以帮助我改进我的代码,因为我是编程的初学者.
以下是代码的一部分:
点击“过滤器”按钮:
btn_filter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("EditPhoto", "counter value = " + counter);
counter++;
sbarBrightness = (SeekBar) findViewById(R.id.seekBarForBrightness);
if (counter % 2 == 0) {
sbarBrightness.setVisibility(View.INVISIBLE);
} else {
sbarBrightness.setVisibility(View.VISIBLE);
}
sbarBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int brightness;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
brightness = progress;
imageBitmap = doBrightness(imageBitmap, brightness);
putGestureImageOnScreen(imageBitmap);
}
});
}
});
public static Bitmap doBrightness(Bitmap src, int value) {
Log.e("Brightness", "Changing brightnhjh");
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmout = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int pixel;
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
pixel = src.getPixel(i, j);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R += value;
if (R > 255) {
R = 255;
} else if (R < 0) {
R = 0;
}
G += value;
if (G > 255) {
G = 255;
} else if (G < 0) {
G = 0;
}
B += value;
if (B > 255) {
B = 255;
} else if (B < 0) {
B = 0;
}
bmout.setPixel(i, j, Color.argb(A, R, G, B));
}
}
return bmout;
}
Seekbar在布局中的声明:
<SeekBar
android:id="@+id/seekBarForBrightness"
android:layout_width="500dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
style="@style/tallerBarStyle"
android:layout_marginTop="64dp"
android:visibility="invisible" />
它的风格如下:
<style name="tallerBarStyle" parent="@android:style/Widget.SeekBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
<item
name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
<item name="android:minHeight">8dip</item>
<item name="android:maxHeight">10dip</item>
</style>
另外,我想保存图像的亮度级别,这样当我将图像传递给另一个活动时,亮度不会丢失.有人可以指导我,我怎样才能实现它.
谢谢.
解决方法:
该问题是由对相同的Bitmap引用进行操作引起的(imageBitmap = doBrightness(imageBitmap,brightness);).因此,当您将滑块向左拖动时,您仍然会不断增加图像的亮度.
尝试
brightness = progress;
//imageBitmap = doBrightness(imageBitmap, brightness);
putGestureImageOnScreen(doBrightness(imageBitmap, brightness));
对于第二个问题,您可以使用Intent将亮度值传输到下一个Activity并执行亮度操作一次以获得相同的结果.