Convert ImageView to black and white and set brightness using ColorFilter



MainActivity.java
package com.blogspot.android_er.androidcolorfilter;

import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

ImageView imageView;
SeekBar brightnessBar;
TextView brightnessInfo;

PorterDuff.Mode[] optMode = PorterDuff.Mode.values();

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

imageView = (ImageView)findViewById(R.id.iv);
brightnessBar = (SeekBar)findViewById(R.id.bar_brightness);
brightnessInfo = (TextView)findViewById(R.id.brightness_info);

brightnessBar.setOnSeekBarChangeListener(brightnessBarChangeListener);

setBlackAndWhite(imageView);
}

SeekBar.OnSeekBarChangeListener brightnessBarChangeListener
= new SeekBar.OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setBlackAndWhite(imageView);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
};

private void setBlackAndWhite(ImageView iv){

float brightness = (float)(brightnessBar.getProgress() - 255);

float[] colorMatrix = {
0.33f, 0.33f, 0.33f, 0, brightness, //red
0.33f, 0.33f, 0.33f, 0, brightness, //green
0.33f, 0.33f, 0.33f, 0, brightness, //blue
0, 0, 0, 1, 0 //alpha
};

ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
iv.setColorFilter(colorFilter);

brightnessInfo.setText(String.valueOf(brightness));
}

}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context=".MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<ImageView
android_id="@+id/iv"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_src="@mipmap/ic_launcher"/>

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Brightness"/>

<SeekBar
android_id="@+id/bar_brightness"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_max="512"
android_progress="255"/>

<TextView
android_id="@+id/brightness_info"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Brightness"/>
</LinearLayout>


Related:
- Android example code using ColorFilter

Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.