欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2022-02-08 11:53:12|栏目: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程序设计有所帮助。

上一篇:外层竖向ScrollView,里层横向ScrollView滑动冲突的解决方法

栏    目:Android代码

下一篇:Android 中的两端对齐实例详解

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

本文地址:http://www.codeinn.net/misctech/192780.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有