Adjust imageview brightness using seekbar in android

There are lot image processing application available in android market which will allow you to change the brightness ,contrast ,and color combinations of an image. In this tutorial we will learn how to change brightness factor of an imageview and how to adjust the brightness according to the value received from a seekbar in android. Our example will have a layout containing a seek bar and an imageview,and an activity class with code for executing the same.

Steps

  1. Create new project in android studio
  2. Add the required widgets into the layout file
  3. Implement the code for adjusting the brightness based on the change in seekbar value

Create New project in android studio

Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio.

Add the required widget into your layout file

In this tutorial we are implementing an example for imageview brightness change on seekbar values. So we need to widgets in layout , a seekbar and an imageview widget. Following is the code for implementing the same.

<ImageView
    android:id="@+id/im_brightness"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/landscape"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>

<SeekBar
    android:id="@+id/sb_value"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:max="255"/>

Implement the code for adjusting the brightness based on the change in seekbar value

Now we need to get the reference of imageview and seekbar in  the activity’s java file and implement the code for changing the brightness on onChange of the seekbar value. Following is the full code for your activity file.

public class Brightness extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brightness);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        SeekBar sb_value = (SeekBar) findViewById(R.id.sb_value);
        final ImageView im_brightness = (ImageView) findViewById(R.id.im_brightness);
        sb_value.setProgress(125);
        sb_value.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                im_brightness.setColorFilter(setBrightness(progress));

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });



    }

    public static PorterDuffColorFilter setBrightness(int progress) {
        if (progress >=    100)
        {
            int value = (int) (progress-100) * 255 / 100;

            return new PorterDuffColorFilter(Color.argb(value, 255, 255, 255), PorterDuff.Mode.SRC_OVER);

        }
        else
        {
            int value = (int) (100-progress) * 255 / 100;
            return new PorterDuffColorFilter(Color.argb(value, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);


        }
    }

}

output

brightnesss

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *