package com.example.test;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
public class TestImage2 extends AppCompatActivity {
private ImageView mIv;
private SeekBar sb1,sb2,sb3,sb4;
private Bitmap bitmap2;
private Canvas canvas;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_image2);
mIv= (ImageView) findViewById(R.id.iv_textimage);
sb1= (SeekBar) findViewById(R.id.sb1);
sb2= (SeekBar) findViewById(R.id.sb2);
sb3= (SeekBar) findViewById(R.id.sb3);
sb4= (SeekBar) findViewById(R.id.sb4);
final Bitmap bitmap1= BitmapFactory.decodeResource(getResources(),R.drawable.test);
bitmap2=Bitmap.createBitmap(bitmap1.getWidth(),bitmap1.getHeight(),bitmap1.getConfig());
canvas=new Canvas(bitmap2);
paint=new Paint();
paint.setColor(Color.BLACK);
final Matrix matrix=new Matrix();
final ColorMatrix cm=new ColorMatrix();
canvas.drawBitmap(bitmap1,matrix,paint);
mIv.setImageBitmap(bitmap2);
sb1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override//开始拖动
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override//停止拖动
public void onStopTrackingTouch(SeekBar seekBar) {
int progress=seekBar.getProgress();
cm.set(new float[]{
progress/128.0f,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));//设置图形颜色矩阵
canvas.drawBitmap(bitmap1,matrix,paint);
mIv.setImageBitmap(bitmap2);
}
});
sb2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress=seekBar.getProgress();
cm.set(new float[]{
1,0,0,0,0,
0,progress/128.0f,0,0,0,
0,0,1,0,0,
0,0,0,1,0
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));//设置图形颜色矩阵
canvas.drawBitmap(bitmap1,matrix,paint);
mIv.setImageBitmap(bitmap2);
}
});
sb3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress=seekBar.getProgress();
cm.set(new float[]{
1,0,0,0,0,
0,1,0,0,0,
0,0,progress/128.0f,0,0,
0,0,0,1,0
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));//设置图形颜色矩阵
canvas.drawBitmap(bitmap1,matrix,paint);
mIv.setImageBitmap(bitmap2);
}
});
//更改饱和度
sb4.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress=seekBar.getProgress();
cm.set(new float[]{
progress/128.0f,0,0,0,0,
0,progress/128.0f,0,0,0,
0,0,progress/128.0f,0,0,
0,0,0,1,0
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));//设置图形颜色矩阵
canvas.drawBitmap(bitmap1,matrix,paint);
mIv.setImageBitmap(bitmap2);
}
});
}
}
<?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:orientation="vertical"
tools:context="com.example.test.TestImage2">
<ImageView
android:id="@+id/iv_textimage"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<SeekBar
android:id="@+id/sb1"
android:max="255"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/sb2"
android:max="255"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/sb3"
android:max="255"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/sb4"
android:max="255"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>