欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发中使用颜色矩阵改变图片颜色,透明度及亮度的方法

程序员文章站 2023-12-09 13:37:21
本文实例讲述了android开发中使用颜色矩阵改变图片颜色,透明度及亮度的方法。分享给大家供大家参考,具体如下: 一、如图 二、代码实现 public c...

本文实例讲述了android开发中使用颜色矩阵改变图片颜色,透明度及亮度的方法。分享给大家供大家参考,具体如下:

一、如图

Android开发中使用颜色矩阵改变图片颜色,透明度及亮度的方法

二、代码实现

public class colorimageactivity extends activity {
 private imageview mimageview;
 private seekbar msbred,msbgreen,msbblue,msbalpha,msblight;
 //修改后的图片
 private bitmap mmodbitmap;
 //画布
 private canvas mcanvas;
 //画笔
 private paint mpaint;
 //颜色矩阵
 private colormatrix mcolormatrix;
 //图片矩阵
 private matrix mmatrix;
 //原图
 private bitmap mbitmap;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_image_color);
  initviews();
 }
 private void initviews() {
  mimageview = (imageview) findviewbyid(r.id.imageview1);
  msbred = (seekbar) findviewbyid(r.id.seekbar1);
  msbgreen = (seekbar) findviewbyid(r.id.seekbar2);
  msbblue = (seekbar) findviewbyid(r.id.seekbar3);
  msbalpha = (seekbar) findviewbyid(r.id.seekbar4);
  msblight = (seekbar) findviewbyid(r.id.seekbar5);
  msbred.setonseekbarchangelistener(new redchangelistener());
  msbgreen.setonseekbarchangelistener(new greenchangelistener());
  msbblue.setonseekbarchangelistener(new bluechangelistener());
  msbalpha.setonseekbarchangelistener(new alphachangelistener());
  msblight.setonseekbarchangelistener(new lightchangelistener());
  //创建原图
  mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.mm0);
  //创建修改的图
  mmodbitmap = bitmap.createbitmap(mbitmap.getwidth()+20, mbitmap.getheight()+20, mbitmap.getconfig());
  mcanvas = new canvas(mmodbitmap);
  mpaint = new paint();
  mpaint.setcolor(color.black);
  mpaint.setantialias(true);
  mmatrix = new matrix();
  mcolormatrix = new colormatrix();
  mpaint.setcolorfilter(new colormatrixcolorfilter(mcolormatrix));
  //绘制原图
  mcanvas.drawbitmap(mbitmap, mmatrix, mpaint);
  mimageview.setimagebitmap(mmodbitmap);
 }
 private class redchangelistener implements onseekbarchangelistener{
  @override
  public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) {
  }
  @override
  public void onstarttrackingtouch(seekbar seekbar) {
  }
  @override
  public void onstoptrackingtouch(seekbar seekbar) {
   int progess = seekbar.getprogress();
   mcolormatrix.set(new float[]{
     progess /128.0f,0,0,0,0,
     0,1,0,0,0,
     0,0,1,0,0,
     0,0,0,1,0,
   });
   mpaint.setcolorfilter(new colormatrixcolorfilter(mcolormatrix));
   mcanvas.drawbitmap(mbitmap, mmatrix, mpaint);
   mimageview.setimagebitmap(mbitmap);
  }
 }
 private class greenchangelistener implements onseekbarchangelistener{
  @override
  public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) {
  }
  @override
  public void onstarttrackingtouch(seekbar seekbar) {
  }
  @override
  public void onstoptrackingtouch(seekbar seekbar) {
   int progess = seekbar.getprogress();
   mcolormatrix.set(new float[]{
     1,0,0,0,0,
     0,progess/128.0f,0,0,0,
     0,0,1,0,0,
     0,0,0,1,0,
   });
   mpaint.setcolorfilter(new colormatrixcolorfilter(mcolormatrix));
   mcanvas.drawbitmap(mbitmap, mmatrix, mpaint);
   mimageview.setimagebitmap(mmodbitmap);
  }
 }
 private class bluechangelistener implements onseekbarchangelistener{
  @override
  public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) {
  }
  @override
  public void onstarttrackingtouch(seekbar seekbar) {
  }
  @override
  public void onstoptrackingtouch(seekbar seekbar) {
   int progess = seekbar.getprogress();
   mcolormatrix.set(new float[]{
     1,0,0,0,0,
     0,1,0,0,0,
     0,0,progess/128.0f,0,0,
     0,0,0,1,0,
   });
   mpaint.setcolorfilter(new colormatrixcolorfilter(mcolormatrix));
   mcanvas.drawbitmap(mbitmap, mmatrix, mpaint);
   mimageview.setimagebitmap(mmodbitmap);
  }
 }
 private class alphachangelistener implements onseekbarchangelistener{
  @override
  public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) {
  }
  @override
  public void onstarttrackingtouch(seekbar seekbar) {
  }
  @override
  public void onstoptrackingtouch(seekbar seekbar) {
   int progess = seekbar.getprogress();
   mcolormatrix.set(new float[]{
     1,0,0,0,0,
     0,1,0,0,0,
     0,0,1,0,0,
     0,0,0,progess/50.0f,0,
   });
   mpaint.setcolorfilter(new colormatrixcolorfilter(mcolormatrix));
   mcanvas.drawbitmap(mbitmap, mmatrix, mpaint);
   mimageview.setimagebitmap(mmodbitmap);
  }
 }
 //更改亮度
 private class lightchangelistener implements onseekbarchangelistener{
  @override
  public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) {
  }
  @override
  public void onstarttrackingtouch(seekbar seekbar) {
  }
  @override
  public void onstoptrackingtouch(seekbar seekbar) {
   int progess = seekbar.getprogress();
   mcolormatrix.set(new float[]{
     progess/128.0f,0,0,0,0,
     0,progess/128.0f,0,0,0,
     0,0,progess/128.0f,0,0,
     0,0,0,1,0,
   });
   mpaint.setcolorfilter(new colormatrixcolorfilter(mcolormatrix));
   mcanvas.drawbitmap(mbitmap, mmatrix, mpaint);
   mimageview.setimagebitmap(mmodbitmap);
  }
 }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。