Android编程之图片颜色处理方法
程序员文章站
2024-02-10 14:55:34
本文实例讲述了android编程之图片颜色处理方法。分享给大家供大家参考,具体如下:
你想做到跟美图秀秀一样可以处理自己的照片,美化自己的照片吗?其实你也可以自己做一个这...
本文实例讲述了android编程之图片颜色处理方法。分享给大家供大家参考,具体如下:
你想做到跟美图秀秀一样可以处理自己的照片,美化自己的照片吗?其实你也可以自己做一个这样的软件,废话不多说了,直接上图,上代码了!
效果图如下:
没处理前:
处理之后:
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三原色 红色值的设置 */ 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,// 蓝色值 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三原色 绿色值的设置 */ 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,// 蓝色值 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三原色 蓝色值的设置 */ 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,// 蓝色值 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三原色 三个值都改变为设置饱和度(亮度) */ 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,// 蓝色值 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三原色 设置透明度 */ 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,// 蓝色值 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="蓝色:" 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>
到这里就完了,看明白了吗?
希望本文所述对大家android程序设计有所帮助。