经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android实现滤镜效果ColorMatrix
来源:jb51  时间:2021/5/10 8:45:53  对本文有异议

本文实例为大家分享了Android实现滤镜效果的具体代码,供大家参考,具体内容如下

1.效果图

2.矩阵算法

  1. package net.surina.myapplication15;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.Canvas;
  5. import android.graphics.ColorMatrix;
  6. import android.graphics.ColorMatrixColorFilter;
  7. import android.graphics.Paint;
  8. import android.os.Bundle;
  9. import android.text.InputType;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.GridLayout;
  14. import android.widget.ImageView;
  15. import androidx.appcompat.app.AppCompatActivity;
  16. import java.util.LinkedList;
  17. import java.util.Stack;
  18. /**
  19. * @author Deeson
  20. * 参考代码:https://github.com/DeesonWoo/MyColorMatrixDemo
  21. */
  22. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  23. Bitmap bitmap;
  24. ImageView iv_photo;
  25. GridLayout matrixLayout;
  26. //每个edittext的宽高
  27. int mEtWidth;
  28. int mEtHeight;
  29. //保存20个edittext
  30. EditText[] mEts = new EditText[20];
  31. //一维数组保存20个矩阵值
  32. float[] mColorMatrix = new float[20];
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_main);
  37. bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.girl);
  38. iv_photo = (ImageView) findViewById(R.id.iv_photo);
  39. matrixLayout = (GridLayout) findViewById(R.id.matrix_layout);
  40. Button btn_change = (Button) findViewById(R.id.btn_change);
  41. Button btn_reset = (Button) findViewById(R.id.btn_reset);
  42. btn_change.setOnClickListener(this);
  43. btn_reset.setOnClickListener(this);
  44. iv_photo.setImageBitmap(bitmap);
  45. //我们无法在onCreate()方法中获得视图的宽高值,所以通过View的post()方法,在视图创建完毕后获得其宽高值
  46. matrixLayout.post(new Runnable() {
  47. @Override
  48. public void run() {
  49. mEtWidth = matrixLayout.getWidth() / 5;
  50. mEtHeight = matrixLayout.getHeight() / 4;
  51. addEts();
  52. initMatrix();
  53. }
  54. });
  55. }
  56. //动态添加edittext
  57. private void addEts() {
  58. for (int i = 0; i < 20; i++) {
  59. EditText et = new EditText(this);
  60. et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
  61. mEts[i] = et;
  62. matrixLayout.addView(et, mEtWidth, mEtHeight);
  63. }
  64. }
  65. //初始化颜色矩阵
  66. private void initMatrix() {
  67. for (int i = 0; i < 20; i++) {
  68. if (i % 6 == 0) {
  69. mEts[i].setText(String.valueOf(1));
  70. } else {
  71. mEts[i].setText(String.valueOf(0));
  72. }
  73. }
  74. }
  75. //获取矩阵值
  76. private void getMatrix() {
  77. for (int i = 0; i < 20; i++) {
  78. String matrix = mEts[i].getText().toString();
  79. boolean isNone = null == matrix || "".equals(matrix);
  80. mColorMatrix[i] = isNone ? 0.0f : Float.valueOf(matrix);
  81. if (isNone) {
  82. mEts[i].setText("0");
  83. }
  84. }
  85. }
  86. //将矩阵设置到图像
  87. private void setImageMatrix() {
  88. Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
  89. ColorMatrix colorMatrix = new ColorMatrix();
  90. colorMatrix.set(mColorMatrix);//将一维数组设置到ColorMatrix
  91. Canvas canvas = new Canvas(bmp);
  92. Paint paint = new Paint();
  93. paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
  94. canvas.drawBitmap(bitmap, 0, 0, paint);
  95. iv_photo.setImageBitmap(bmp);
  96. }
  97. @Override
  98. public void onClick(View v) {
  99. switch (v.getId()) {
  100. case R.id.btn_change:
  101. break;
  102. case R.id.btn_reset:
  103. //重置矩阵效果
  104. initMatrix();
  105. break;
  106. }
  107. //作用矩阵效果
  108. getMatrix();
  109. setImageMatrix();
  110. }
  111. }

3.布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical">
  8. <ImageView
  9. android:id="@+id/iv_photo"
  10. android:layout_width="300dp"
  11. android:layout_height="0dp"
  12. android:layout_weight="4"
  13. android:layout_gravity="center_horizontal"
  14. android:scaleType="fitCenter"
  15. android:src="@drawable/girl"
  16. />
  17. <GridLayout
  18. android:id="@+id/matrix_layout"
  19. android:layout_width="match_parent"
  20. android:layout_height="0dp"
  21. android:layout_weight="2"
  22. android:columnCount="5"
  23. android:rowCount="4">
  24. </GridLayout>
  25. <LinearLayout
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:orientation="horizontal">
  29. <Button
  30. android:id="@+id/btn_change"
  31. android:layout_width="0dp"
  32. android:layout_height="wrap_content"
  33. android:layout_weight="1"
  34. android:text="change"/>
  35. <Button
  36. android:id="@+id/btn_reset"
  37. android:layout_width="0dp"
  38. android:layout_height="wrap_content"
  39. android:layout_weight="1"
  40. android:text="reset"/>
  41. </LinearLayout>
  42. </LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号