Android實(shí)現(xiàn)濾鏡效果ColorMatrix
本文實(shí)例為大家分享了Android實(shí)現(xiàn)濾鏡效果的具體代碼,供大家參考,具體內(nèi)容如下
1.效果圖
2.矩陣算法
package net.surina.myapplication15; 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.text.InputType; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.GridLayout; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import java.util.LinkedList; import java.util.Stack; /** * @author Deeson * 參考代碼:https://github.com/DeesonWoo/MyColorMatrixDemo */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { Bitmap bitmap; ImageView iv_photo; GridLayout matrixLayout; //每個(gè)edittext的寬高 int mEtWidth; int mEtHeight; //保存20個(gè)edittext EditText[] mEts = new EditText[20]; //一維數(shù)組保存20個(gè)矩陣值 float[] mColorMatrix = new float[20]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.girl); iv_photo = (ImageView) findViewById(R.id.iv_photo); matrixLayout = (GridLayout) findViewById(R.id.matrix_layout); Button btn_change = (Button) findViewById(R.id.btn_change); Button btn_reset = (Button) findViewById(R.id.btn_reset); btn_change.setOnClickListener(this); btn_reset.setOnClickListener(this); iv_photo.setImageBitmap(bitmap); //我們無(wú)法在onCreate()方法中獲得視圖的寬高值,所以通過(guò)View的post()方法,在視圖創(chuàng)建完畢后獲得其寬高值 matrixLayout.post(new Runnable() { @Override public void run() { mEtWidth = matrixLayout.getWidth() / 5; mEtHeight = matrixLayout.getHeight() / 4; addEts(); initMatrix(); } }); } //動(dòng)態(tài)添加edittext private void addEts() { for (int i = 0; i < 20; i++) { EditText et = new EditText(this); et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); mEts[i] = et; matrixLayout.addView(et, 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++) { String matrix = mEts[i].getText().toString(); boolean isNone = null == matrix || "".equals(matrix); mColorMatrix[i] = isNone ? 0.0f : Float.valueOf(matrix); if (isNone) { mEts[i].setText("0"); } } } //將矩陣設(shè)置到圖像 private void setImageMatrix() { Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.set(mColorMatrix);//將一維數(shù)組設(shè)置到ColorMatrix Canvas canvas = new Canvas(bmp); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bitmap, 0, 0, paint); iv_photo.setImageBitmap(bmp); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_change: break; case R.id.btn_reset: //重置矩陣效果 initMatrix(); break; } //作用矩陣效果 getMatrix(); setImageMatrix(); } }
3.布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/iv_photo" android:layout_width="300dp" android:layout_height="0dp" android:layout_weight="4" android:layout_gravity="center_horizontal" android:scaleType="fitCenter" android:src="@drawable/girl" /> <GridLayout android:id="@+id/matrix_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:columnCount="5" android:rowCount="4"> </GridLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_change" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="change"/> <Button android:id="@+id/btn_reset" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="reset"/> </LinearLayout> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用Handler實(shí)現(xiàn)定時(shí)器與倒計(jì)時(shí)器功能
Handler的最常見應(yīng)用場(chǎng)景之一便是通過(guò)Handler在子線程中間接更新UI。這篇文章主要介紹了Android使用Handler實(shí)現(xiàn)定時(shí)器與倒計(jì)時(shí)器功能,需要的朋友可以參考下2018-02-02Android實(shí)現(xiàn)iPhone晃動(dòng)撤銷輸入功能 Android仿微信搖一搖功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)iPhone晃動(dòng)撤銷輸入功能,Android仿微信搖一搖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android將應(yīng)用調(diào)試log信息保存在SD卡的方法
Android將應(yīng)用調(diào)試log信息保存在SD卡的方法大家都知道嗎,下面腳本之家小編給大家分享Android將應(yīng)用調(diào)試log信息保存在SD卡的方法,感興趣的朋友參考下2016-04-04Android使用Intent傳大數(shù)據(jù)簡(jiǎn)單實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android使用Intent傳大數(shù)據(jù)簡(jiǎn)單實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03HttpClient通過(guò)Post上傳文件的實(shí)例代碼
這篇文章主要介紹了HttpClient通過(guò)Post上傳文件的實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08Android自定義View實(shí)現(xiàn)仿駕考寶典顯示分?jǐn)?shù)效果(收藏)
本文通過(guò)自定義view和屬性動(dòng)畫結(jié)合在一起實(shí)現(xiàn)實(shí)現(xiàn)仿駕考寶典顯示分?jǐn)?shù)效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-03-03Android中仿IOS提示框的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android中仿IOS提示框的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android開發(fā)實(shí)現(xiàn)拍照功能的方法實(shí)例解析
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)拍照功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android拍照功能的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10