Wednesday, September 19, 2012

Example to apply ColorFilter on ImageView

Example to apply ColorFilter on ImageView


package com.example.androidcolorfilter;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {
 
 ImageView imageView;
 SeekBar redBar, greenBar, blueBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageView = (ImageView)findViewById(R.id.iv);
        redBar = (SeekBar)findViewById(R.id.redbar);
        greenBar = (SeekBar)findViewById(R.id.greenbar);
        blueBar = (SeekBar)findViewById(R.id.bluebar);
        
        redBar.setOnSeekBarChangeListener(colorBarChangeListener);
        greenBar.setOnSeekBarChangeListener(colorBarChangeListener);
        blueBar.setOnSeekBarChangeListener(colorBarChangeListener);
        
        setColorFilter(imageView);
    }
    
    OnSeekBarChangeListener colorBarChangeListener
    = new OnSeekBarChangeListener(){

  @Override
  public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
   setColorFilter(imageView);
  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   
  }};
    
    private void setColorFilter(ImageView iv){
     
       /*
        * 5x4 matrix for transforming the color+alpha components of a Bitmap. 
        * The matrix is stored in a single array, and its treated as follows: 
        * [  a, b, c, d, e, 
        *   f, g, h, i, j, 
        *   k, l, m, n, o, 
        *   p, q, r, s, t ] 
        * 
        * When applied to a color [r, g, b, a], the resulting color is computed 
        * as (after clamping) 
        * R' = a*R + b*G + c*B + d*A + e; 
        * G' = f*R + g*G + h*B + i*A + j; 
        * B' = k*R + l*G + m*B + n*A + o; 
        * A' = p*R + q*G + r*B + s*A + t;
        */
     
     
     float redValue = ((float)redBar.getProgress())/255;
     float greenValue = ((float)greenBar.getProgress())/255;
     float blueValue = ((float)blueBar.getProgress())/255;
     
     float[] colorMatrix = { 
       redValue, 0, 0, 0, 0,  //red
       0, greenValue, 0, 0, 0, //green
       0, 0, blueValue, 0, 0,  //blue
       0, 0, 0, 1, 0    //alpha    
     };
     
     ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
     
     iv.setColorFilter(colorFilter);

    }


}


<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:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_launcher"/>
    
    <SeekBar 
        android:id="@+id/redbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"/>
    <SeekBar 
        android:id="@+id/greenbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"/>
    <SeekBar 
        android:id="@+id/bluebar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"/>

</LinearLayout>


download filesDownload the files.

Next:
- Swap color using ColorFilter
- Convert color image to black and white using ColorFilter


No comments: