本文主要是介绍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"><ImageViewandroid:id="@+id/imageview"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"/><GridLayoutandroid:id="@+id/group"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="3"android:columnCount="5"android:rowCount="4"></GridLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:onClick="btnChange"android:layout_weight="1"android:text="change"android:textAllCaps="false"tools:ignore="OnClick" /><Buttonandroid: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并初始化为初始矩阵
@Overrideprotected 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() {@Overridepublic void run() {//获取宽高信息mEtWidth = mGroup.getWidth() / 5;mEtHeight = mGroup.getHeight() / 4;addEts();initMatrix();}});}//添加EditTextprivate 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];@Overrideprotected 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() {@Overridepublic void run() {//获取宽高信息mEtWidth = mGroup.getWidth() / 5;mEtHeight = mGroup.getHeight() / 4;addEts();initMatrix();}});}//添加EditTextprivate 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!