Android绘图机制 Demo(简单完成美图秀秀的滤镜)

2024-04-28 16:48

本文主要是介绍Android绘图机制 Demo(简单完成美图秀秀的滤镜),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android绘图机制 Demo(简单完成美图秀秀的滤镜)

1.xml

<?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=".MainActivity"android:orientation="vertical"><ImageViewandroid:id="@+id/image_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2" /><GridLayoutandroid:id="@+id/grid_layout"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="3"android:columnCount="5"android:rowCount="4"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_change"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="改变"/><Buttonandroid:id="@+id/btn_reset"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="重置"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_gray"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="灰度"/><Buttonandroid:id="@+id/btn_reverse"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="反转"/><Buttonandroid:id="@+id/btn_nostalgia"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="怀旧"/><Buttonandroid:id="@+id/btn_discoloration"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="去色"/><Buttonandroid:id="@+id/btn_high_saturation"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="饱和"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_negative"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="底片"/><Buttonandroid:id="@+id/btn_old_image"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="老照片"/></LinearLayout></LinearLayout>

2.java

package com.example.android_image;import androidx.appcompat.app.AppCompatActivity;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;public class MainActivity extends AppCompatActivity {@BindView(R.id.image_view)ImageView imageView;@BindView(R.id.grid_layout)GridLayout gridLayout;@BindView(R.id.btn_change)Button btnChange;@BindView(R.id.btn_reset)Button btnReset;@BindView(R.id.btn_gray)Button btnGray;@BindView(R.id.btn_reverse)Button btnReverse;@BindView(R.id.btn_nostalgia)Button btnNostalgia;@BindView(R.id.btn_discoloration)Button btnDiscoloration;@BindView(R.id.btn_high_saturation)Button btnHighSaturation;@BindView(R.id.btn_negative)Button btnNegative;@BindView(R.id.btn_old_image)Button btnOldImage;private Bitmap bitmap;private int mEtWidth;private int mEtHeight;private EditText[] editTexts = new EditText[20];private float[] colorMatrices = new float[20];@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);imageView.setImageBitmap(bitmap);//无法在onCreate()方法中获得视图的宽高值,//所以通过View的post方法,在视图创建完毕后获得其宽高值gridLayout.post(new Runnable() {@Overridepublic void run() {//获取宽高信息mEtWidth = gridLayout.getWidth() / 5;mEtHeight = gridLayout.getHeight() / 4;addEts();initMatrix();}});}//添加EditTextprivate void addEts() {for (int i = 0; i < 20; i++) {EditText editText = new EditText(MainActivity.this);editTexts[i] = editText;gridLayout.addView(editText, mEtWidth, mEtHeight);}}//初始化颜色矩阵为初始状态private void initMatrix() {for (int i = 0; i < 20; i++) {if (i % 6 == 0) {editTexts[i].setText(String.valueOf(1));} else {editTexts[i].setText(String.valueOf(0));}}}//获取矩阵值private void getMatrix() {for (int i = 0; i < 20; i++) {colorMatrices[i] = Float.valueOf(editTexts[i].getText().toString());}}//将矩阵值设置到图像private void setImageMatrix() {Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),Bitmap.Config.ARGB_8888);ColorMatrix colorMatrix = new ColorMatrix();colorMatrix.set(colorMatrices);Canvas canvas = new Canvas(bmp);Paint paint = new Paint();paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));canvas.drawBitmap(bitmap, 0, 0, paint);imageView.setImageBitmap(bmp);}//特效(ColorMatrices)private void specialEffect(int type) {switch (type) {case 1://灰度colorMatrices = new float[]{0.33f, 0.59f, 0.11f, 0, 0,0.33f, 0.59f, 0.11f, 0, 0,0.33f, 0.59f, 0.11f, 0, 0,0, 0, 0, 1, 0};break;case 2://反转colorMatrices = new float[]{-1, 0, 0, 1, 1,0, -1, 0, 1, 1,0, 0, -1, 1, 1,0, 0, 0, 1, 0};break;case 3://怀旧colorMatrices = new float[]{0.393f, 0.769f, 0.189f, 0, 0,0.349f, 0.686f, 0.168f, 0, 0,0.272f, 0.534f, 0.131f, 0, 0,0, 0, 0, 1, 0};break;case 4://去色colorMatrices = new float[]{1.5f, 1.5f, 1.5f, 0, -1,1.5f, 1.5f, 1.5f, 0, -1,1.5f, 1.5f, 1.5f, 0, -1,0, 0, 0, 1, 0};break;case 5://高饱和度colorMatrices = new float[]{1.438f, -0.122f, -0.016f, 0, -0.03f,-0.062f, 1.378f, -0.016f, 0, 0.05f,-0.062f, -0.122f, 1.483f, 0, -0.02f,0, 0, 0, 1, 0};break;}for (int i = 0; i < 20; i++) {editTexts[i].setText(String.valueOf(colorMatrices[i]));}}//底片(像素点)private Bitmap handleImageNegative(Bitmap bm) {int width = bm.getWidth();int height = bm.getHeight();int color;int r, g, b, a;Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);int[] oldPx = new int[width * height];int[] newPx = new int[width * height];bm.getPixels(oldPx, 0, width, 0, 0, width, height);for (int i = 0; i < width * height; i++) {color = oldPx[i];r = Color.red(color);g = Color.green(color);b = Color.blue(color);a = Color.alpha(color);r = 255 - r;g = 255 - g;b = 255 - b;if (r > 255) {r = 255;} else if (r < 0) {r = 0;}if (g > 255) {g = 255;} else if (g < 0) {g = 0;}if (b > 255) {b = 255;} else if (b < 0) {b = 0;}newPx[i] = Color.argb(a, r, g, b);}bmp.setPixels(newPx, 0, width, 0, 0, width, height);return bmp;}//老照片(像素点)private Bitmap handleImageOld(Bitmap bm) {int width = bm.getWidth();int height = bm.getHeight();int color;int r, g, b, a;Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//原始图片的像素int[] oldPx = new int[width * height];//新图片的像素int[] newPx = new int[width * height];bm.getPixels(oldPx, 0, width, 0, 0, width, height);for (int i = 0; i < width * height; i++) {//获得每个点的像素并进行分解color = oldPx[i];r = Color.red(color);g = Color.green(color);b = Color.blue(color);a = Color.alpha(color);//根据想要的底片效果进行设置r = (int) (0.393 * r + 0.769 * g + 0.189 * b);g = (int) (0.349 * r + 0.686 * g + 0.168 * b);b = (int) (0.272 * r + 0.534 * g + 0.131 * b);if (r > 255) {r = 255;} else if (r < 0) {r = 0;}if (g > 255) {g = 255;} else if (g < 0) {g = 0;}if (b > 255) {b = 255;} else if (b < 0) {b = 0;}//通过设置的颜色来合成新颜色newPx[i] = Color.argb(a, r, g, b);}//为新的位图设置像素bmp.setPixels(newPx, 0, width, 0, 0, width, height);return bmp;}@OnClick({R.id.btn_change, R.id.btn_reset,R.id.btn_gray, R.id.btn_reverse, R.id.btn_nostalgia,R.id.btn_discoloration, R.id.btn_high_saturation,R.id.btn_negative, R.id.btn_old_image})public void onViewClicked(View view) {switch (view.getId()) {case R.id.btn_change://作用矩阵效果getMatrix();setImageMatrix();break;case R.id.btn_reset://重置矩阵效果initMatrix();getMatrix();setImageMatrix();break;case R.id.btn_gray://灰度specialEffect(1);setImageMatrix();break;case R.id.btn_reverse://反转specialEffect(2);setImageMatrix();break;case R.id.btn_nostalgia://怀旧specialEffect(3);setImageMatrix();break;case R.id.btn_discoloration://去色specialEffect(4);setImageMatrix();break;case R.id.btn_high_saturation://高饱和度specialEffect(5);setImageMatrix();break;case R.id.btn_negative://底片imageView.setImageBitmap(handleImageNegative(bitmap));break;case R.id.btn_old_image://老照片imageView.setImageBitmap(handleImageOld(bitmap));break;}}}

3.运行效果;

在这里插入图片描述在这里插入图片描述

这篇关于Android绘图机制 Demo(简单完成美图秀秀的滤镜)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

一份LLM资源清单围观技术大佬的日常;手把手教你在美国搭建「百万卡」AI数据中心;为啥大模型做不好简单的数学计算? | ShowMeAI日报

👀日报&周刊合集 | 🎡ShowMeAI官网 | 🧡 点赞关注评论拜托啦! 1. 为啥大模型做不好简单的数学计算?从大模型高考数学成绩不及格说起 司南评测体系 OpenCompass 选取 7 个大模型 (6 个开源模型+ GPT-4o),组织参与了 2024 年高考「新课标I卷」的语文、数学、英语考试,然后由经验丰富的判卷老师评判得分。 结果如上图所

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

Eclipse+ADT与Android Studio开发的区别

下文的EA指Eclipse+ADT,AS就是指Android Studio。 就编写界面布局来说AS可以边开发边预览(所见即所得,以及多个屏幕预览),这个优势比较大。AS运行时占的内存比EA的要小。AS创建项目时要创建gradle项目框架,so,创建项目时AS比较慢。android studio基于gradle构建项目,你无法同时集中管理和维护多个项目的源码,而eclipse ADT可以同时打开

android 免费短信验证功能

没有太复杂的使用的话,功能实现比较简单粗暴。 在www.mob.com网站中可以申请使用免费短信验证功能。 步骤: 1.注册登录。 2.选择“短信验证码SDK” 3.下载对应的sdk包,我这是选studio的。 4.从头像那进入后台并创建短信验证应用,获取到key跟secret 5.根据技术文档操作(initSDK方法写在setContentView上面) 6.关键:在有用到的Mo

android一键分享功能部分实现

为什么叫做部分实现呢,其实是我只实现一部分的分享。如新浪微博,那还有没去实现的是微信分享。还有一部分奇怪的问题:我QQ分享跟QQ空间的分享功能,我都没配置key那些都是原本集成就有的key也可以实现分享,谁清楚的麻烦详解下。 实现分享功能我们可以去www.mob.com这个网站集成。免费的,而且还有短信验证功能。等这分享研究完后就研究下短信验证功能。 开始实现步骤(新浪分享,以下是本人自己实现

Android我的二维码扫描功能发展史(完整)

最近在研究下二维码扫描功能,跟据从网上查阅的资料到自己勉强已实现扫描功能来一一介绍我的二维码扫描功能实现的发展历程: 首页通过网络搜索发现做android二维码扫描功能看去都是基于google的ZXing项目开发。 2、搜索怎么使用ZXing实现自己的二维码扫描:从网上下载ZXing-2.2.zip以及core-2.2-source.jar文件,分别解压两个文件。然后把.jar解压出来的整个c

android 带与不带logo的二维码生成

该代码基于ZXing项目,这个网上能下载得到。 定义的控件以及属性: public static final int SCAN_CODE = 1;private ImageView iv;private EditText et;private Button qr_btn,add_logo;private Bitmap logo,bitmap,bmp; //logo图标private st

回调的简单理解

之前一直不太明白回调的用法,现在简单的理解下 就按这张slidingmenu来说,主界面为Activity界面,而旁边的菜单为fragment界面。1.现在通过主界面的slidingmenu按钮来点开旁边的菜单功能并且选中”区县“选项(到这里就可以理解为A类调用B类里面的c方法)。2.通过触发“区县”的选项使得主界面跳转到“区县”相关的新闻列表界面中(到这里就可以理解为B类调用A类中的d方法

Android多线程下载见解

通过for循环开启N个线程,这是多线程,但每次循环都new一个线程肯定很耗内存的。那可以改用线程池来。 就以我个人对多线程下载的理解是开启一个线程后: 1.通过HttpUrlConnection对象获取要下载文件的总长度 2.通过RandomAccessFile流对象在本地创建一个跟远程文件长度一样大小的空文件。 3.通过文件总长度/线程个数=得到每个线程大概要下载的量(线程块大小)。

自制的浏览器主页,可以是最简单的桌面应用,可以把它当成备忘录桌面应用

自制的浏览器主页,可以是最简单的桌面应用,可以把它当成备忘录桌面应用。如果你看不懂,请留言。 完整代码: <!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><ti