Saturday, September 22, 2012

Adjust lightness and darkness of ImageView, using LightingColorFilter.

Using LightingColorFilter, we can adjust lightness and darkness of ImageView by adjusting the passed parameter mul and add.

Adjust lightness and darkness of ImageView, using LightingColorFilter.


package com.example.androidcolorfilter;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 ImageView imageView;

 SeekBar alphaMulBar, redMulBar, greenMulBar, blueMulBar;
 SeekBar alphaAddBar, redAddBar, greenAddBar, blueAddBar;
 TextView colorInfo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageView = (ImageView)findViewById(R.id.iv);
        
        alphaMulBar = (SeekBar)findViewById(R.id.mula);
        redMulBar = (SeekBar)findViewById(R.id.mulr);
        greenMulBar = (SeekBar)findViewById(R.id.mulg);
        blueMulBar = (SeekBar)findViewById(R.id.mulb);
        
        alphaAddBar = (SeekBar)findViewById(R.id.adda);
        redAddBar = (SeekBar)findViewById(R.id.addr);
        greenAddBar = (SeekBar)findViewById(R.id.addg);
        blueAddBar = (SeekBar)findViewById(R.id.addb);
        
        colorInfo = (TextView)findViewById(R.id.colorinfo);

        alphaMulBar.setOnSeekBarChangeListener(colorBarChangeListener);
        redMulBar.setOnSeekBarChangeListener(colorBarChangeListener);
        greenMulBar.setOnSeekBarChangeListener(colorBarChangeListener);
        blueMulBar.setOnSeekBarChangeListener(colorBarChangeListener);
        
        alphaAddBar.setOnSeekBarChangeListener(colorBarChangeListener);
        redAddBar.setOnSeekBarChangeListener(colorBarChangeListener);
        greenAddBar.setOnSeekBarChangeListener(colorBarChangeListener);
        blueAddBar.setOnSeekBarChangeListener(colorBarChangeListener);

        setLightingColorFilter(imageView);
    }
    
    OnSeekBarChangeListener colorBarChangeListener
    = new OnSeekBarChangeListener(){

  @Override
  public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
   setLightingColorFilter(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 setLightingColorFilter(ImageView iv){
     
     int mul = Color.argb(
       alphaMulBar.getProgress(), 
       redMulBar.getProgress(), 
       greenMulBar.getProgress(), 
       blueMulBar.getProgress());
     
     int add = Color.argb(
       alphaAddBar.getProgress(), 
       redAddBar.getProgress(), 
       greenAddBar.getProgress(), 
       blueAddBar.getProgress());
     
     LightingColorFilter lightingColorFilter = new LightingColorFilter(mul, add);
     iv.setColorFilter(lightingColorFilter);
     
     colorInfo.setText(
       "mul = #" + Integer.toHexString(mul) +"\n" +
       "add = #" + Integer.toHexString(add));
     
    }

}


<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"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="mul"
        android:layout_alignParentLeft="true"/>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_mul_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="a"
            android:layout_alignParentLeft="true"/>
        <SeekBar 
            android:id="@+id/mula"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="255"
            android:layout_toRightOf="@id/text_mul_a"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_mul_r"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="r"
            android:layout_alignParentLeft="true"/>
        <SeekBar 
            android:id="@+id/mulr"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="255"
            android:layout_toRightOf="@id/text_mul_r"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_mul_g"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="g"
            android:layout_alignParentLeft="true"/>
        <SeekBar 
            android:id="@+id/mulg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="255"
            android:layout_toRightOf="@id/text_mul_g"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_mul_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="b"
            android:layout_alignParentLeft="true"/>
        <SeekBar 
            android:id="@+id/mulb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="255"
            android:layout_toRightOf="@id/text_mul_b"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add"
        android:layout_alignParentLeft="true"/>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_add_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="a"
            android:layout_alignParentLeft="true"/>
        <SeekBar 
            android:id="@+id/adda"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="255"
            android:layout_toRightOf="@id/text_add_a"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_add_r"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="r"
            android:layout_alignParentLeft="true"/>
        <SeekBar 
            android:id="@+id/addr"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="255"
            android:layout_toRightOf="@id/text_add_r"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_add_g"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="g"
            android:layout_alignParentLeft="true"/>
        <SeekBar 
            android:id="@+id/addg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="255"
            android:layout_toRightOf="@id/text_add_g"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_add_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="b"
            android:layout_alignParentLeft="true"/>
        <SeekBar 
            android:id="@+id/addb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="255"
            android:layout_toRightOf="@id/text_add_b"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>

    <TextView
        android:id="@+id/colorinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>


download filesDownload the files.

1 comment:

ramya@chweet said...

Hi sir,
Your tutorials are very nice and very useful to us.But in this tutorial I'm unable to see the image.Can i know why its happening...
Ramya