使用 ColorMatrix 对图片进行风格处理
2017-08-22 22:22
169 查看
前言
当我们对图片进行编辑(或者美化)的时候,有一项就是对图片进行风格设置,比如:复古,黑白等,看下面效果图ColorMatrix
代码
界面xml布局<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_margin="25dp" android:src="@drawable/ncxy" /> <Spinner android:id="@+id/sp_colorFilter_item" android:layout_width="match_parent" android:layout_height="45dp" android:layout_below="@id/iv_img" android:layout_centerHorizontal="true" android:layout_marginLeft="45dp" android:layout_marginRight="45dp" android:entries="@array/colorMatrix" android:gravity="center_horizontal" /> </RelativeLayout>
spinner 监听代码
spinner.setSelection(0, false); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { imageView.setImageResource(R.drawable.ncxy); Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); switch (position) { case 0: break; case 1: imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixGrayscale())); break; case 2: imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixSepia())); break; case 3: imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixBinary())); break; case 4: imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixInvert())); break; case 5: imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixAlphaBlue())); break; case 6: imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixAlphaPink())); break; } } @Override public void onNothingSelected(AdapterView<?> parent) { } });
第一行是为了解决程序界面打开的时候自动调用监听;在监听里,每选择一个 item,就会调用一个新的方法返回一个 colorMatrix 对象,再通过 getBitmap 方法返回一个新的 bitmap ,设置给 imageview.下面是 getBitmap 方法代码:
/** * 根据colorMaxtrix获取对应图片 * * @param colorMatrix * @return */ private Bitmap getBitmap(Bitmap original, ColorMatrix colorMatrix) { Bitmap bitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(original, 0, 0, paint); return bitmap; }
每个 item 对应的方法如下:
private ColorMatrix getColorMatrixGrayscale() { ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); return colorMatrix; } private ColorMatrix getColorMatrixSepia() { ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrix colorScale = new ColorMatrix(); colorScale.setScale(1, 1, 0.8f, 1); // Convert to grayscale, then apply brown color colorMatrix.postConcat(colorScale); return colorMatrix; } private ColorMatrix getColorMatrixBinary() { ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); float m = 255f; float t = -255 * 128f; ColorMatrix threshold = new ColorMatrix(new float[]{ m, 0, 0, 1, t, 0, m, 0, 1, t, 0, 0, m, 1, t, 0, 0, 0, 1, 0 }); // Convert to grayscale, then scale and clamp colorMatrix.postConcat(threshold); return colorMatrix; } private ColorMatrix getColorMatrixInvert() { return new ColorMatrix(new float[]{ -1, 0, 0, 0, 255, 0, -1, 0, 0, 255, 0, 0, -1, 0, 255, 0, 0, 0, 1, 0 }); } private ColorMatrix getColorMatrixAlphaBlue() { return new ColorMatrix(new float[]{ 0, 0, 0, 0, 0, 0.3f, 0, 0, 0, 50, 0, 0, 0, 0, 255, 0.2f, 0.4f, 0.4f, 0, -30 }); } private ColorMatrix getColorMatrixAlphaPink() { return new ColorMatrix(new float[]{ 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0.2f, 0, 0, 0, 50, 0.2f, 0.2f, 0.2f, 0, -20 }); }
以上就是完整代码.
相关文章推荐
- 图片未正常加载时使用onerror 来进行后续处理。
- 一起学android之利用ColorMatrix进行图片的各种特效处理(32)
- 使用python进行图像处理-调整图片大小
- 使用JAVA如何对图片进行格式检查以及安全检查处理
- 【数字图像处理】使用kmeans算法对TrueColor图片进行优化
- 对图片进行缩放处理——Matrix对象的使用
- IOS中使用像素位图(CGImageRef)对图片进行处理
- 在Python中使用PIL模块对图片进行高斯模糊处理的教程
- 在Python中使用PIL模块对图片进行高斯模糊处理的教程
- 使用JAVA如何对图片进行格式检查以及安全检查处理
- [置顶] 简述如何嵌套使用Viewpager,点击图片放大查看图片,并且使用PhotoView进行图片处理
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- IOS中使用像素位图(CGImageRef)对图片进行处理
- 使用Thumbnails对一个文件夹下的所有图片进行压缩处理
- 使用ImageMagick + Jmagick进行图片操作(续2)——实现高质量的图片处理
- Android中使用Bitmap对图片进行特效处理
- IOS中使用像素位图(CGImageRef)对图片进行处理
- 使用JAVA如何对图片进行格式检查以及安全检查处理
- IOS中使用像素位图(CGImageRef)对图片进行处理