Android 颜色矩阵——ColorMatrix

2024-04-28 16:48

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/943755

相关文章

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

hdu 4565 推倒公式+矩阵快速幂

题意 求下式的值: Sn=⌈ (a+b√)n⌉%m S_n = \lceil\ (a + \sqrt{b}) ^ n \rceil\% m 其中: 0<a,m<215 0< a, m < 2^{15} 0<b,n<231 0 < b, n < 2^{31} (a−1)2<b<a2 (a-1)^2< b < a^2 解析 令: An=(a+b√)n A_n = (a +

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

android应用中res目录说明

Android应用的res目录是一个特殊的项目,该项目里存放了Android应用所用的全部资源,包括图片、字符串、颜色、尺寸、样式等,类似于web开发中的public目录,js、css、image、style。。。。 Android按照约定,将不同的资源放在不同的文件夹中,这样可以方便的让AAPT(即Android Asset Packaging Tool , 在SDK的build-tools目

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR