Convert Imageview To Dark As Well As White, As Well As Laid Upward Brightness, Using Colorfilter

Posted on


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;  world shape 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             = novel SeekBar.OnSeekBarChangeListener(){          @Override         world void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {             setBlackAndWhite(imageView);         }          @Override         world void onStartTrackingTouch(SeekBar seekBar) {          }          @Override         world void onStopTrackingTouch(SeekBar seekBar) {          }     };      soul 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 = novel ColorMatrixColorFilter(colorMatrix);         iv.setColorFilter(colorFilter);          brightnessInfo.setText(String.valueOf(brightness));     }  }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     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="https://rootandroid.org//"         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 illustration code using ColorFilter