Android 颜色矩阵——ColorMatrix

Posted 春招进大厂的梦想家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 颜色矩阵——ColorMatrix相关的知识,希望对你有一定的参考价值。

android 颜色矩阵——ColorMatrix

1.xml文件

通过GridLayout来进行布局,动态的添加EditText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ColorMatrix.ColorMatrixActivity"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"/>

    <GridLayout
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:columnCount="5"
        android:rowCount="4">

    </GridLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:onClick="btnChange"
            android:layout_weight="1"
            android:text="change"
            android:textAllCaps="false"
            tools:ignore="OnClick" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:onClick="btnReset"
            android:layout_weight="1"
            android:text="Reset"
            android:textAllCaps="false"
            tools:ignore="OnClick" />
    </LinearLayout>

</LinearLayout>

四行五列的网格布局来实现矩阵,后面的代码中将动态的添加EditText

2.java——动态添加EditText

动态创建EditText,添加到GridLayout并初始化为初始矩阵

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_matrix);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        mImageView = findViewById(R.id.imageview);
        mGroup = findViewById(R.id.group);
        mImageView.setImageBitmap(bitmap);

        mGroup.post(new Runnable() {
            @Override
            public void run() {
                //获取宽高信息
                mEtWidth = mGroup.getWidth() / 5;
                mEtHeight = mGroup.getHeight() / 4;
                addEts();
                initMatrix();
            }
        });
    }

    //添加EditText
    private void addEts(){
        for (int i = 0; i < 20; i++) {
            EditText editText = new EditText(ColorMatrixActivity.this);
            mEts[i] = editText;
            mGroup.addView(editText,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));
            }
        }
    }

需要注意的是,我们无法在onCreate()方法中获得视图的宽高值,所以通过View的post方法,在视图创建完毕后获得其宽高值

3.获得修改后的EditText的值,并将矩阵值设置给颜色矩阵

//获取矩阵的值
    private void getMatrix(){
        for (int i = 0; i < 20; i++) {
            mColorMatrix[i] = Float.valueOf(mEts[i].getText().toString());
        }
    }

    //将矩阵值设置到图像
    private void setImageMatrix(){
        Bitmap bmp = Bitmap.createBitmap(
                bitmap.getWidth(),
                bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(mColorMatrix);

        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap,0,0,paint);
        mImageView.setImageBitmap(bmp);
    }

4.为两个按钮添加点击事件

//按钮的点击事件
    /*
    一个是将先有矩阵作用到图像
    另外一个是将先有的矩阵恢复到初始矩阵的状态,即还原图像
     */
    public void btnChange(View view){
        getMatrix();
        setImageMatrix();
    }

    public void btnReset(View view){
        initMatrix();
        getMatrix();
        setImageMatrix();
    }

5.全部java代码

package com.example.toucheventactivity.ColorMatrix;

import androidx.appcompat.app.AppCompatActivity;

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.view.View;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;

import com.example.toucheventactivity.R;

/**
 * @author 23737
 * 2021.6.13
 */

/*
动态创建EditText,添加到GridLayout并初始化为初始矩阵的代码
 */
public class ColorMatrixActivity extends AppCompatActivity {

    private Bitmap bitmap;
    private ImageView mImageView;
    private GridLayout mGroup;
    private int mEtWidth,mEtHeight;
    EditText[] mEts = new EditText[20];
    float[] mColorMatrix = new float[20];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_matrix);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        mImageView = findViewById(R.id.imageview);
        mGroup = findViewById(R.id.group);
        mImageView.setImageBitmap(bitmap);

        mGroup.post(new Runnable() {
            @Override
            public void run() {
                //获取宽高信息
                mEtWidth = mGroup.getWidth() / 5;
                mEtHeight = mGroup.getHeight() / 4;
                addEts();
                initMatrix();
            }
        });
    }

    //添加EditText
    private void addEts(){
        for (int i = 0; i < 20; i++) {
            EditText editText = new EditText(ColorMatrixActivity.this);
            mEts[i] = editText;
            mGroup.addView(editText,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++) {
            mColorMatrix[i] = Float.valueOf(mEts[i].getText().toString());
        }
    }

    //将矩阵值设置到图像
    private void setImageMatrix(){
        Bitmap bmp = Bitmap.createBitmap(
                bitmap.getWidth(),
                bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(mColorMatrix);

        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap,0,0,paint);
        mImageView.setImageBitmap(bmp);
    }

    //按钮的点击事件
    /*
    一个是将先有矩阵作用到图像
    另外一个是将先有的矩阵恢复到初始矩阵的状态,即还原图像
     */
    public void btnChange(View view){
        getMatrix();
        setImageMatrix();
    }

    public void btnReset(View view){
        initMatrix();
        getMatrix();
        setImageMatrix();
    }
}

6.运行效果:

在这里插入图片描述

以上是关于Android 颜色矩阵——ColorMatrix的主要内容,如果未能解决你的问题,请参考以下文章

Android Matrix矩阵详解

Android图像处理之图形特效处理

Android的矩阵:ColorMatrix

如何在 .NET 中使用 ColorMatrix 更改亮度、颜色、饱和度、色调

Android - ColorMatrix 处理图像对比度

Android Matrix 的理解与应用