您好,登錄后才能下訂單哦!
小編給大家分享一下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開發中如何使用顏色矩陣改變圖片顏色,透明度及亮度”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。