Android 图片的颜色处理实例代码
程序员文章站
2024-03-06 19:06:32
仿造美图秀秀移动鼠标调整seekbar,调整图片的颜色
项目布局如下:
仿造美图秀秀移动鼠标调整seekbar,调整图片的颜色
项目布局如下:
<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:orientation="vertical" tools:context=".mainactivity" > <seekbar android:id="@+id/sb1" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" /> <seekbar android:id="@+id/sb2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" /> <seekbar android:id="@+id/sb3" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" /> <imageview android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
效果如下:
逻辑部分代码如下:
package com.wuyudong.resize; import android.os.bundle; 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.view.menu; import android.widget.imageview; import android.widget.seekbar; import android.widget.seekbar.onseekbarchangelistener; public class mainactivity extends activity { private imageview iv1; private seekbar sb1, sb2, sb3; private bitmap alterbitmap; private canvas canvas; private paint paint; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); iv1 = (imageview) findviewbyid(r.id.iv); // 设置第一个bitmap的图标 final bitmap bitmap1 = bitmapfactory.decoderesource(getresources(), r.drawable.ic_launcher); // 新建一个bitmap alterbitmap = bitmap.createbitmap(bitmap1.getwidth(), bitmap1.getheight(), bitmap1.getconfig()); // 以alterbitmap为模板新建画布 canvas = new canvas(alterbitmap); // 新建画笔并设置属性 paint = new paint(); paint.setcolor(color.black); // 新建矩阵 final matrix matrix = new matrix(); // 指定颜色矩阵 final colormatrix cm = new colormatrix(); /** * set this colormatrix to identity: * [ 1 0 0 0 0 - red vector * 0 1 0 0 0 - green vector * 0 0 1 0 0 - blue vector * 0 0 0 1 0 ] - alpha vector */ paint.setcolorfilter(new colormatrixcolorfilter(cm)); paint.setantialias(true); // 设置画布 canvas.drawbitmap(bitmap1, matrix, paint); iv1.setimagebitmap(alterbitmap); sb1 = (seekbar) findviewbyid(r.id.sb1); sb2 = (seekbar) findviewbyid(r.id.sb2); sb3 = (seekbar) findviewbyid(r.id.sb3); sb1.setonseekbarchangelistener(new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { int progress = seekbar.getprogress(); cm.set(new float[]{ progress/128.0f, 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(bitmap1, matrix, paint); iv1.setimagebitmap(alterbitmap); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } }); sb2.setonseekbarchangelistener(new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { int progress = seekbar.getprogress(); cm.set(new float[]{ progress/128.0f, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, progress/128.0f, 0, 0, 0, 0, 0, 1, 0, }); paint.setcolorfilter(new colormatrixcolorfilter(cm)); canvas.drawbitmap(bitmap1, matrix, paint); iv1.setimagebitmap(alterbitmap); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } }); 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/128.0f, 0, 0, 0, 0, 0, 1, 0, }); paint.setcolorfilter(new colormatrixcolorfilter(cm)); canvas.drawbitmap(bitmap1, matrix, paint); iv1.setimagebitmap(alterbitmap); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } }); } }
运行效果:
出处:http://www.cnblogs.com/wuyudong/
上一篇: java判断字符串是否为数字的方法小结