package net.hnjdzy.imagecolor; import android.os.Bundle; import android.app.Activity; 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.view.Menu; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends Activity { private ImageView iv; private SeekBar sb1,sb2,sb3; private Canvas canvas; private Paint paint; private ColorMatrix cm; private Matrix matrix; private Bitmap b1,b2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); sb1 = (SeekBar) findViewById(R.id.seekBar1); sb2 = (SeekBar) findViewById(R.id.seekBar2); sb3 = (SeekBar) findViewById(R.id.seekBar3); b1 = BitmapFactory.decodeResource(getResources(), R.drawable.fq); b2 = Bitmap.createBitmap(b1.getWidth(), b1.getHeight(), b1.getConfig()); canvas = new Canvas(b2); paint = new Paint(); paint.setColor(Color.BLACK); matrix = new Matrix(); paint.setAntiAlias(true); //颜色矩阵 cm = new ColorMatrix(); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(b1, matrix, paint); iv.setImageBitmap(b2); //改变RGB中R的值 sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @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(b1, matrix, paint); iv.setImageBitmap(b2); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }); //改变RGB中G的值 sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @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(b1, matrix, paint); iv.setImageBitmap(b2); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }); //改变RGB中B的值 sb3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @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(b1, matrix, paint); iv.setImageBitmap(b2); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }); } }
执行效果图如下: