直播视频app源码使用ColorMatrix 对图片进行风格处理
直播视频app源码使用ColorMatrix 对图片进行风格处理的相关代码
界面xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="25dp"
android:src="@drawable/ncxy" />
<Spinner
android:id="@+id/sp_colorFilter_item"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/iv_img"
android:layout_centerHorizontal="true"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:entries="@array/colorMatrix"
android:gravity="center_horizontal" />
</RelativeLayout>
spinner 监听代码
spinner.setSelection(0, false);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
imageView.setImageResource(R.drawable.ncxy);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
switch (position) {
case 0:
break;
case 1:
imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixGrayscale()));
break;
case 2:
imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixSepia()));
break;
case 3:
imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixBinary()));
break;
case 4:
imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixInvert()));
break;
case 5:
imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixAlphaBlue()));
break;
case 6:
imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixAlphaPink()));
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
第一行是为了解决程序界面打开的时候自动调用监听;在监听里,每选择一个 item,就会调用一个新的方法返回一个 colorMatrix 对象,再通过 getBitmap 方法返回一个新的 bitmap ,设置给 imageview.下面是 getBitmap 方法代码:
/**
* 根据colorMaxtrix获取对应图片
*
* @param colorMatrix
* @return
*/
private Bitmap getBitmap(Bitmap original, ColorMatrix colorMatrix) {
Bitmap bitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(original, 0, 0, paint);
return bitmap;
}
每个 item 对应的方法如下:
private ColorMatrix getColorMatrixGrayscale() {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
return colorMatrix;
}
private ColorMatrix getColorMatrixSepia() {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrix colorScale = new ColorMatrix();
colorScale.setScale(1, 1, 0.8f, 1);
// Convert to grayscale, then apply brown color
colorMatrix.postConcat(colorScale);
return colorMatrix;
}
private ColorMatrix getColorMatrixBinary() {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
float m = 255f;
float t = -255 * 128f;
ColorMatrix threshold = new ColorMatrix(new float[]{
m, 0, 0, 1, t,
0, m, 0, 1, t,
0, 0, m, 1, t,
0, 0, 0, 1, 0
});
// Convert to grayscale, then scale and clamp
colorMatrix.postConcat(threshold);
return colorMatrix;
}
private ColorMatrix getColorMatrixInvert() {
return new ColorMatrix(new float[]{
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0
});
}
private ColorMatrix getColorMatrixAlphaBlue() {
return new ColorMatrix(new float[]{
0, 0, 0, 0, 0,
0.3f, 0, 0, 0, 50,
0, 0, 0, 0, 255,
0.2f, 0.4f, 0.4f, 0, -30
});
}
private ColorMatrix getColorMatrixAlphaPink() {
return new ColorMatrix(new float[]{
0, 0, 0, 0, 255,
0, 0, 0, 0, 0,
0.2f, 0, 0, 0, 50,
0.2f, 0.2f, 0.2f, 0, -20
});
}
以上就是直播视频app源码使用ColorMatrix 对图片进行风格处理的相关代码, 更多内容欢迎关注之后的文章