上一篇文章ColorMatrix的用法本质上就是改变颜色数组(4x5),我们可以直接改变这个数组,完成颜色过滤效果,常见的复古、清新、黑白,就是特定的数组元素搞定的
public static Bitmap handlerImagebyMatrixArray(Bitmap srcBitmap, float[] colorMatrixArray) {
// **************************** 一、构建画布,新的bitmap画布 ****************************
// 先复制一张跟原图一模一样的bitmap,不能在原图上做修改
Bitmap newBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.ARGB_8888);
// 注意这里是new了一个新的Canvas,把newBitmap与他绑定在一起
Canvas canvas = new Canvas(newBitmap);
// **************************** 二、构建画笔,画笔要设置ColorMatrixColorFilter ****************************
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.set(colorMatrixArray);
//构建一支画笔
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// 给画笔设置这个ColorMatrix
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
// **************************** 三、开始作画了, ****************************
// 使用上面混合了ColorMatrix那只笔,
// 在与newBitmap关联的的canvas上作画
// 画什么,画的是原图srcBitmap
canvas.drawBitmap(srcBitmap, 0, 0, paint);
// 返回newBitmap
return newBitmap;
}
package performance.brilliant.com.brilliantperformance.activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
import performance.brilliant.com.brilliantperformance.R;
import performance.brilliant.com.brilliantperformance.view.ImageHelper;
public class ColorMatrixArrayActivity extends AppCompatActivity {
float[] mFloats = new float[20];
private Random mRandom;
private TextView mTextView;
private ImageView mImageView;
private Bitmap mBitmap;
private Bitmap mSrc;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_matrix_array);
mRandom = new Random();
mTextView = findViewById(R.id.tv_array);
mImageView = findViewById(R.id.ivc);
mSrc = BitmapFactory.decodeResource(getResources(), R.drawable.img_hudie);
}
public void generateArray(View view) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 20; i++) {
float v = mRandom.nextFloat();
v = (float) (Math.round(v * 100000f) / 1000) / 100;//保留两位小数
mFloats[i] = v;
stringBuilder.append(v + "\t\t\t");
if (i == 4 || i == 9 || i == 14) {
stringBuilder.append("\n");
}
}
mTextView.setText(stringBuilder);
changeColor(mFloats);
}
private void changeColor(final float[] floats) {
new Thread(new Runnable() {
@Override
public void run() {
mBitmap = ImageHelper.handlerImagebyMatrixArray
(mSrc, floats);
mHandler.post(new Runnable() {
@Override
public void run() {
mImageView.setImageBitmap(mBitmap);
}
});
}
}).start();
}
/**
* 初始化效果
* @param view
*/
public void reset(View view) {
mFloats = new float[]{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
};
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 20; i++) {
stringBuilder.append(mFloats[i] + "\t\t\t");
if (i == 4 || i == 9 || i == 14) {
stringBuilder.append("\n");
}
}
mTextView.setText(stringBuilder);
changeColor(mFloats);
}
/**
* 老照片效果
* @param view
*/
public void old_pic(View view) {
mFloats = 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,
1, 1, 1, 0, 0
};
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 20; i++) {
stringBuilder.append(mFloats[i] + "\t\t\t");
if (i == 4 || i == 9 || i == 14) {
stringBuilder.append("\n");
}
}
mTextView.setText(stringBuilder);
changeColor(mFloats);
}
/**
* 底片效果
* @param view
*/
public void dipian(View view) {
mFloats = new float[]{
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 1, 0, 255
};
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 20; i++) {
stringBuilder.append(mFloats[i] + "\t\t\t");
if (i == 4 || i == 9 || i == 14) {
stringBuilder.append("\n");
}
}
mTextView.setText(stringBuilder);
changeColor(mFloats);
}
}