在我的应用程序中,我希望能够更改 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>
我还想保存图像的亮度级别,这样当我将图像传递给另一个活动时,亮度不会丢失。有人可以指导我,我该如何实现。
谢谢。