使用 ColorMatrix 对图片进行风格处理

本文介绍了一种使用ColorMatrix实现图片风格转换的方法,包括灰度、复古、二值化等多种效果,并提供了详细的代码实现。

前言

当我们对图片进行编辑(或者美化)的时候,有一项就是对图片进行风格设置,比如:复古,黑白等,看下面效果图

colorMatrix

ColorMatrix

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
        });
    }

以上就是完整代码.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code小生

有头像,我们容易成为朋友

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值