1 packagecom.himi.xiuxiu;2
3 importandroid.app.Activity;4 importandroid.graphics.Bitmap;5 importandroid.graphics.BitmapFactory;6 importandroid.graphics.Canvas;7 importandroid.graphics.ColorMatrix;8 importandroid.graphics.ColorMatrixColorFilter;9 importandroid.graphics.Matrix;10 importandroid.graphics.Paint;11 importandroid.os.Bundle;12 importandroid.widget.ImageView;13 importandroid.widget.SeekBar;14 importandroid.widget.SeekBar.OnSeekBarChangeListener;15
16 public class MainActivity extends Activity implementsOnSeekBarChangeListener {17 privateSeekBar sb_red;18 privateSeekBar sb_green;19 privateSeekBar sb_blue;20 privateImageView iv;21
22 privateBitmap srcbitmap;23 privateBitmap alertbitmap;24 privateCanvas canvas;25 privatePaint paint;26 @Override27 protected voidonCreate(Bundle savedInstanceState) {28 super.onCreate(savedInstanceState);29 setContentView(R.layout.activity_main);30 sb_red =(SeekBar) findViewById(R.id.sb_red);31 sb_green =(SeekBar) findViewById(R.id.sb_green);32 sb_blue =(SeekBar) findViewById(R.id.sb_blue);33 iv =(ImageView) findViewById(R.id.iv);34 //获得原图的位图
35 srcbitmap =BitmapFactory.decodeResource(getResources(), R.drawable.pre19);36 //获得原图的拷贝副本
37 alertbitmap =Bitmap.createBitmap(srcbitmap.getWidth(), srcbitmap.getHeight(), srcbitmap.getConfig());38 //创建一个画布
39 canvas = newCanvas(alertbitmap);40 //创建画笔
41 paint = newPaint();42
43 ColorMatrix cm = newColorMatrix();44 cm.set(new float[] {45 1, 0, 0, 0, 0,46 0, 1, 0, 0, 0,47 0, 0, 1, 0, 0,48 0, 0, 0, 1, 0
49 });50
51 paint.setColorFilter(newColorMatrixColorFilter(cm));52 canvas.drawBitmap(srcbitmap, newMatrix(), paint);53 iv.setImageBitmap(alertbitmap);54
55 sb_red.setOnSeekBarChangeListener(this);56 sb_green.setOnSeekBarChangeListener(this);57 sb_blue.setOnSeekBarChangeListener(this);58
59 }60 public void onProgressChanged(SeekBar seekBar, intprogress,61 booleanfromUser) {62 //TODO 自动生成的方法存根
63
64 }65 public voidonStartTrackingTouch(SeekBar seekBar) {66 //TODO 自动生成的方法存根
67
68 }69 //停止滑动进度条,才设置颜色变化,节省内存
70 public voidonStopTrackingTouch(SeekBar seekBar) {71 float red=1;72 float green=1;73 float blue=1;74 switch(seekBar.getId()) {75 caseR.id.sb_red:76 red = seekBar.getProgress()/128.0f;77 break;78 caseR.id.sb_green:79 green = seekBar.getProgress()/128.0f;80 break;81 caseR.id.sb_blue:82 blue = seekBar.getProgress()/128.0f;83 break;84
85 default:86 break;87 }88 ColorMatrix cm = newColorMatrix();89 cm.set(new float[] { red, 0, 0, 0, 0,90 0, green, 0, 0, 0,91 0, 0, blue, 0, 0,92 0, 0, 0, 1, 0});93
94 paint.setColorFilter(newColorMatrixColorFilter(cm));95 canvas.drawBitmap(srcbitmap, newMatrix(), paint);96 iv.setImageBitmap(alertbitmap);97 }98
99 }