Android实现滤镜效果ColorMatrix
程序员文章站
2022-03-31 14:14:10
本文实例为大家分享了android实现滤镜效果的具体代码,供大家参考,具体内容如下1.效果图2.矩阵算法package net.surina.myapplication15; import andro...
本文实例为大家分享了android实现滤镜效果的具体代码,供大家参考,具体内容如下
1.效果图
2.矩阵算法
package net.surina.myapplication15; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.colormatrix; import android.graphics.colormatrixcolorfilter; import android.graphics.paint; import android.os.bundle; import android.text.inputtype; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.gridlayout; import android.widget.imageview; import androidx.appcompat.app.appcompatactivity; import java.util.linkedlist; import java.util.stack; /** * @author deeson * 参考代码:https://github.com/deesonwoo/mycolormatrixdemo */ public class mainactivity extends appcompatactivity implements view.onclicklistener { bitmap bitmap; imageview iv_photo; gridlayout matrixlayout; //每个edittext的宽高 int metwidth; int metheight; //保存20个edittext edittext[] mets = new edittext[20]; //一维数组保存20个矩阵值 float[] mcolormatrix = new float[20]; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.girl); iv_photo = (imageview) findviewbyid(r.id.iv_photo); matrixlayout = (gridlayout) findviewbyid(r.id.matrix_layout); button btn_change = (button) findviewbyid(r.id.btn_change); button btn_reset = (button) findviewbyid(r.id.btn_reset); btn_change.setonclicklistener(this); btn_reset.setonclicklistener(this); iv_photo.setimagebitmap(bitmap); //我们无法在oncreate()方法中获得视图的宽高值,所以通过view的post()方法,在视图创建完毕后获得其宽高值 matrixlayout.post(new runnable() { @override public void run() { metwidth = matrixlayout.getwidth() / 5; metheight = matrixlayout.getheight() / 4; addets(); initmatrix(); } }); } //动态添加edittext private void addets() { for (int i = 0; i < 20; i++) { edittext et = new edittext(this); et.setinputtype(inputtype.type_class_number | inputtype.type_number_flag_decimal); mets[i] = et; matrixlayout.addview(et, metwidth, metheight); } } //初始化颜色矩阵 private void initmatrix() { for (int i = 0; i < 20; i++) { if (i % 6 == 0) { mets[i].settext(string.valueof(1)); } else { mets[i].settext(string.valueof(0)); } } } //获取矩阵值 private void getmatrix() { for (int i = 0; i < 20; i++) { string matrix = mets[i].gettext().tostring(); boolean isnone = null == matrix || "".equals(matrix); mcolormatrix[i] = isnone ? 0.0f : float.valueof(matrix); if (isnone) { mets[i].settext("0"); } } } //将矩阵设置到图像 private void setimagematrix() { bitmap bmp = bitmap.createbitmap(bitmap.getwidth(), bitmap.getheight(), bitmap.config.argb_8888); colormatrix colormatrix = new colormatrix(); colormatrix.set(mcolormatrix);//将一维数组设置到colormatrix canvas canvas = new canvas(bmp); paint paint = new paint(); paint.setcolorfilter(new colormatrixcolorfilter(colormatrix)); canvas.drawbitmap(bitmap, 0, 0, paint); iv_photo.setimagebitmap(bmp); } @override public void onclick(view v) { switch (v.getid()) { case r.id.btn_change: break; case r.id.btn_reset: //重置矩阵效果 initmatrix(); break; } //作用矩阵效果 getmatrix(); setimagematrix(); } }
3.布局
<?xml version="1.0" encoding="utf-8"?> <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"> <imageview android:id="@+id/iv_photo" android:layout_width="300dp" android:layout_height="0dp" android:layout_weight="4" android:layout_gravity="center_horizontal" android:scaletype="fitcenter" android:src="@drawable/girl" /> <gridlayout android:id="@+id/matrix_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:columncount="5" android:rowcount="4"> </gridlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <button android:id="@+id/btn_change" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="change"/> <button android:id="@+id/btn_reset" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="reset"/> </linearlayout> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 红斑狼疮有哪些危害?
下一篇: word2010 矩阵公式创建