Android編程之圖片顏色處理方法
本文實(shí)例講述了Android編程之圖片顏色處理方法。分享給大家供大家參考,具體如下:
你想做到跟美圖秀秀一樣可以處理自己的照片,美化自己的照片嗎?其實(shí)你也可以自己做一個(gè)這樣的軟件,廢話不多說(shuō)了,直接上圖,上代碼了!
效果圖如下:
沒(méi)處理前:

處理之后:

MainActivity.java的代碼如下:
package net.loonggg.test;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private SeekBar sb1, sb2, sb3, sb4, sb5;
private ImageView iv;
private Bitmap bitmap, updateBitmap;
private Canvas canvas;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
sb1 = (SeekBar) findViewById(R.id.sb1);
sb2 = (SeekBar) findViewById(R.id.sb2);
sb3 = (SeekBar) findViewById(R.id.sb3);
sb4 = (SeekBar) findViewById(R.id.sb4);
sb5 = (SeekBar) findViewById(R.id.sb5);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b);
updateBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
canvas = new Canvas(updateBitmap);
paint = new Paint();
final ColorMatrix cm = new ColorMatrix();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
final Matrix matrix = new Matrix();
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(updateBitmap);
/**
* RGB三原色 紅色值的設(shè)置
*/
sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 紅色值
0, 1, 0, 0, 0,// 綠色值
0, 0, 1, 0, 0,// 藍(lán)色值
0, 0, 0, 1, 0 // 透明度
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(updateBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
/**
* RGB三原色 綠色值的設(shè)置
*/
sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
cm.set(new float[] { 1, 0, 0, 0, 0,// 紅色值
0, progress / 128f, 0, 0, 0,// 綠色值
0, 0, 1, 0, 0,// 藍(lán)色值
0, 0, 0, 1, 0 // 透明度
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(updateBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
/**
* RGB三原色 藍(lán)色值的設(shè)置
*/
sb3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
cm.set(new float[] { 1, 0, 0, 0, 0,// 紅色值
0, 1, 0, 0, 0,// 綠色值
0, 0, progress / 128f, 0, 0,// 藍(lán)色值
0, 0, 0, 1, 0 // 透明度
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(updateBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
/**
* RGB三原色 三個(gè)值都改變?yōu)樵O(shè)置飽和度(亮度)
*/
sb4.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 紅色值
0, progress / 128f, 0, 0, 0,// 綠色值
0, 0, progress / 128f, 0, 0,// 藍(lán)色值
0, 0, 0, 1, 0 // 透明度
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(updateBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
/**
* RGB三原色 設(shè)置透明度
*/
sb5.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
cm.set(new float[] { 1, 0, 0, 0, 0,// 紅色值
0, 1, 0, 0, 0,// 綠色值
0, 0, 1, 0, 0,// 藍(lán)色值
0, 0, 0, progress / 128f, 0 // 透明度
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(updateBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
}
}
布局文件代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CDCDCD"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="紅色:"
android:textColor="#FF0000"
android:textSize="24sp" />
<SeekBar
android:id="@+id/sb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綠色:"
android:textColor="#00FF00"
android:textSize="24sp" />
<SeekBar
android:id="@+id/sb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="藍(lán)色:"
android:textColor="#0000FF"
android:textSize="24sp" />
<SeekBar
android:id="@+id/sb3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="飽和度:"
android:textColor="#000000"
android:textSize="16.5sp" />
<SeekBar
android:id="@+id/sb4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度:"
android:textColor="#000000"
android:textSize="16.5sp" />
<SeekBar
android:id="@+id/sb5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
</LinearLayout>
到這里就完了,看明白了嗎?
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android studio 3.0上進(jìn)行多渠道打包遇到的問(wèn)題小結(jié)(超簡(jiǎn)潔版)
這篇文章主要介紹了Android studio 3.0上進(jìn)行多渠道打包遇到的問(wèn)題小結(jié)(超簡(jiǎn)潔版),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
Android控件實(shí)現(xiàn)直播App點(diǎn)贊飄心動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了FlowLikeView控件實(shí)現(xiàn)直播App特效之點(diǎn)贊飄心動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android自定義processor實(shí)現(xiàn)bindView功能的實(shí)例
下面小編就為大家分享一篇Android自定義processor實(shí)現(xiàn)bindView功能的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android checkbox的listView(多選,全選,反選)具體實(shí)現(xiàn)方法
由于listview的一些特性,剛開(kāi)始寫這種需求的功能的時(shí)候都會(huì)碰到一些問(wèn)題,重點(diǎn)就是存儲(chǔ)每個(gè)checkbox的狀態(tài)值,在這里分享出了完美解決方法:2013-06-06
Android滾動(dòng)菜單ListView實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Android滾動(dòng)菜單ListView實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Android startActivityForResult()代替方案示例
這篇文章主要為大家介紹了Android startActivityForResult()代替方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android clipChildren屬性實(shí)例詳解
本文主要介紹Android clipChildren的屬性,這里對(duì)clipChildren屬性做了一個(gè)小例子,展示了效果圖和實(shí)例代碼,方便大家觀看理解2016-07-07
Android評(píng)分控件RatingBar使用實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了Android評(píng)分控件RatingBar使用實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android內(nèi)存溢出及內(nèi)存泄漏原因進(jìn)解析
這篇文章主要介紹了Android內(nèi)存溢出及內(nèi)存泄漏原因解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
這篇文章主要給大家介紹了關(guān)于如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

