MainActivity.java
package com.blogspot.android_er.androidcolorfilter;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
SeekBar redBar, greenBar, blueBar;
@Override
protected 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);
}
SeekBar.OnSeekBarChangeListener colorBarChangeListener
= new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setColorFilter(imageView);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
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);
}
}
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"/>
<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>
Related:
- Convert ImageView to black and white, and set brightness, using ColorFilter
0 comments:
Post a Comment