/** * 简易调色板 步骤: 1.使用SeekBar来控制颜色的显示比例 * * * @author 刘楠 * * 2016-3-3下午10:58:24 */ public class MainActivity extends Activity {
private SeekBar seekbarRed;
private ImageView iv;
private Paint paint;
private Matrix matrix;
private Canvas canvas;
private SeekBar seekbarGreen;
private SeekBar seekbarBlue;
private SeekBar seekbarAlpha;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbarRed = (SeekBar) findViewById(R.id.seekbar_red);
seekbarGreen = (SeekBar) findViewById(R.id.seekbar_green);
seekbarBlue = (SeekBar) findViewById(R.id.seekbar_blue);
seekbarAlpha = (SeekBar) findViewById(R.id.seekbar_alpha);
iv = (ImageView) findViewById(R.id.iv);
// 通过这种方法读取的bitmap是只读的,不可修改
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.pre14);
final Bitmap buffImage = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
canvas = new Canvas(buffImage);
paint = new Paint();
matrix = new Matrix();
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
seekbarRed.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
int progress = seekBar.getProgress();
float value = progress / 50.0f;
ColorMatrix cm = new ColorMatrix();
/*
*
* 颜色矩阵
*/
cm.set(new float[] { 1 * value, 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(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress,
boolean fromUser) {
float value = progress / 50.0f;
ColorMatrix cm = new ColorMatrix();
/*
*
* 颜色矩阵
*/
cm.set(new float[] { 1 * value, 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(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
}
});
seekbarGreen.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
int progress = seekBar.getProgress();
float value = progress / 50.0f;
ColorMatrix cm = new ColorMatrix();
/*
*
* 颜色矩阵
*/
cm.set(new float[] { 1, 0, 0, 0, 0,// 红色
0, 1 * value, 0, 0, 0,// 绿色
0, 0, 1, 0, 0,// 蓝色
0, 0, 0, 1, 0,// 透明度
});
// 给画笔添加过滤器
// 给画笔添加颜色过滤器
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress,
boolean fromUser) {
float value = progress / 50.0f;
ColorMatrix cm = new ColorMatrix();
/*
*
* 颜色矩阵
*/
cm.set(new float[] { 1, 0, 0, 0, 0,// 红色
0, 1 * value, 0, 0, 0,// 绿色
0, 0, 1, 0, 0,// 蓝色
0, 0, 0, 1, 0,// 透明度
});
// 给画笔添加过滤器
// 给画笔添加颜色过滤器
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
}
});
seekbarBlue.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
int progress = seekBar.getProgress();
float value = progress / 50.0f;
ColorMatrix cm = new ColorMatrix();
/*
*
* 颜色矩阵
*/
cm.set(new float[] { 1, 0, 0, 0, 0,// 红色
0, 1, 0, 0, 0,// 绿色
0, 0, 1 * value, 0, 0,// 蓝色
0, 0, 0, 1, 0,// 透明度
});
// 给画笔添加过滤器
// 给画笔添加颜色过滤器
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress,
boolean fromUser) {
float value = progress / 50.0f;
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] { 1, 0, 0, 0, 0,// 红色
0, 1, 0, 0, 0,// 绿色
0, 0, 1 * value, 0, 0,// 蓝色
0, 0, 0, 1, 0,// 透明度
});
// 给画笔添加过滤器
// 给画笔添加颜色过滤器
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
}
});
seekbarAlpha.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
int progress = seekBar.getProgress();
float value = progress / 50.0f;
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] { 1, 0, 0, 0, 0,// 红色
0, 1, 0, 0, 0,// 绿色
0, 0, 1, 0, 0,// 蓝色
0, 0, 0, 1 * value, 0,// 透明度
});
// 给画笔添加过滤器
// 给画笔添加颜色过滤器
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress,
boolean fromUser) {
float value = progress / 50.0f;
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] { 1, 0, 0, 0, 0,// 红色
0, 1, 0, 0, 0,// 绿色
0, 0, 1, 0, 0,// 蓝色
0, 0, 0, 1 * value, 0,// 透明度
});
// 给画笔添加过滤器
// 给画笔添加颜色过滤器
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(buffImage);
}
});
}
} |