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
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="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
No comments:
Post a Comment