android开发笔记之多媒体—图片的颜色处理
博客专区 > Mr_Nice 的博客 > 博客详情
android开发笔记之多媒体—图片的颜色处理
Mr_Nice 发表于1年前
android开发笔记之多媒体—图片的颜色处理
  • 发表于 1年前
  • 阅读 0
  • 收藏 0
  • 点赞 2
  • 评论 0

腾讯云实验室 1小时搭建人工智能应用,让技术更容易入门 免费体验 >>>   

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);
    }
}
共有 人打赏支持
粉丝 0
博文 47
码字总数 39947
×
Mr_Nice
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: