android开发笔记之多媒体—图片的颜色处理

1.拿到原图

baseBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pre);

2.拿到一张与原图同样大小的纸

copyBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());

3.把画纸固定在画布上

canvas = new Canvas(copyBitmap);

4.拿到画笔

paint = new Paint();

5.给画笔设置颜色过滤器

//vactor的取值范围0-2
        float[] colors = new float[]{
            red_vactor ,0 ,0 ,0 ,0,   
            0 ,green_vactor ,0 ,0 ,0,   
            0 ,0 ,blue_vactor ,0 ,0,   
            0 ,0 ,0 ,1 ,0
        };
        //给画笔设置颜色过滤器
        paint.setColorFilter(new ColorMatrixColorFilter(colors));

6.处理规则

Matrix matrix = new Matrix();

7.照着原图画

canvas.drawBitmap(baseBitmap, matrix, paint);

8.把图片显示在ImageView组件上

iv.setImageBitmap(copyBitmap);

效果图:

核心代码:

public class MainActivity extends Activity implements OnSeekBarChangeListener{

    private Bitmap baseBitmap,copyBitmap;//原图和复制的图
    private Canvas canvas;              //画布
    private Paint paint;                //画笔
    private SeekBar red_seekbar,blue_seekbar,green_seekbar; //拖动条
    private ImageView iv;                   
    private float red_vactor,blue_vactor,green_vactor;//用来记录RGB三种颜色的量的
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        red_seekbar = (SeekBar) findViewById(R.id.red_seekbar);
        blue_seekbar = (SeekBar) findViewById(R.id.blue_seekbar);
        green_seekbar = (SeekBar) findViewById(R.id.green_seekbar);
        iv = (ImageView) findViewById(R.id.iv);
        red_seekbar.setOnSeekBarChangeListener(this);
        blue_seekbar.setOnSeekBarChangeListener(this);
        green_seekbar.setOnSeekBarChangeListener(this);
    }
    //进度改变的时候调用
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {

    }
    //开始拖动的时候调用
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }
    //停止拖动的时候调用
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        float progress = seekBar.getProgress(); 
        float count = progress / 50f;       //使拖动条的值为0f-2f
        switch (seekBar.getId()) {
        case R.id.red_seekbar:
            this.red_vactor = count;
            break;
        case R.id.green_seekbar:
            this.green_vactor = count;
            break;
        case R.id.blue_seekbar:
            this.blue_vactor = count;
            break;
        default:
            break;
        }
        //主体代码
        //1.拿到原图
        baseBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pre);
        //2.拿到一张与原图一样大小的纸
        copyBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());
        //3.把画纸固定在画板上
        canvas = new Canvas(copyBitmap);
        //4.拿到画笔
        paint = new Paint();
        //vactor的取值范围0-2
        float[] colors = new float[]{
            red_vactor ,0 ,0 ,0 ,0,   
            0 ,green_vactor ,0 ,0 ,0,   
            0 ,0 ,blue_vactor ,0 ,0,   
            0 ,0 ,0 ,1 ,0
        };
        //5.给画笔设置颜色过滤器
        paint.setColorFilter(new ColorMatrixColorFilter(colors));
        //6.处理规则
        Matrix matrix = new Matrix();
        //7.照着原图画
        canvas.drawBitmap(baseBitmap, matrix, paint);
        //8.把画好的图显示在ImageView组件上
        iv.setImageBitmap(copyBitmap);
    }
}

你可能感兴趣的:(Android开发,多媒体)