Android实例
下面,我们着手写一个demo,模拟一个 4×5 的颜色矩阵来体验一下上面对颜色矩阵的分析。效果如下:

关键代码是将 4×5 矩阵转换成一维数组,然后再将这一维数组设置到ColorMatrix类里去,请看代码:

//将矩阵设置到图像 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); }
这个demo里面的代码比较简单,我在这里就全部贴出来了,先上xml布局:
<?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" tools:context="com。example。deeson。mycolormatrix。MainActivity" android:orientation="vertical"> <ImageView android:id="@+id/iv_photo" android:layout_width="300dp" android:layout_height="0dp" android:layout_weight="3" android:layout_gravity="center_horizontal"/> <GridLayout android:id="@+id/matrix_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="4" 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>
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-84905-2.html
允许一妻多夫
第二