Android图像处理:怀旧效果
图片怀旧效果的算法:
我们用颜色矩阵(ColorMatrix)来完成我们的怀旧效果。如果有不知道ColorMatrix的原理的话可以参考:Android学习笔记之图像颜色处理(ColorMatrix)
这就是那个颜色矩阵。我们可以利用上面的计算方法来改变我们的颜色矩阵的值从而达到我们想要的效果。
上面的计算方法可以转换为:
M =
在Android中,颜色矩阵M是以一维数组m=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]的方式进行存储的。
M = [0.393,0.768,0.189,0,0 , 0.349 , 0.686, 0.168, 0 , 0, 0.272, 0.534, 0.131, 0 , 0, 0, 0 , 0 , 0,0]
具体操作:
自定义view
ColorView.java
[java]
package com.color;
import android.content.Context;
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.util.AttributeSet;
import android.widget.ImageView;
public class ColorView extends ImageView {
private Paint myPaint = null;
private Bitmap bitmap = null;
private ColorMatrix myColorMatrix = null;
//设置颜色值
private float[] colorArray = {(float) 0.393,(float) 0.768,(float) 0.189,0,0,
(float) 0.349,(float) 0.686,(float) 0.168,0,0,
(float) 0.272,(float) 0.534,(float) 0.131,0,0,
0,0,0,1,0};
public ColorView(Context context, AttributeSet attrs)
{
super(context, attrs);
bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ww);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//新建画笔对象
myPaint = new Paint();
//描画(原始图片)
canvas.drawBitmap(bitmap,0, 0, myPaint);
//新建颜色矩阵对象
myColorMatrix = new ColorMatrix();
//设置颜色矩阵的值
myColorMatrix.set(colorArray);
//设置画笔颜色过滤器
myPaint.setColorFilter(new ColorMatrixColorFilter(myColorMatrix));
//描画(处理后的图片)
canvas.drawBitmap(bitmap,0,0,myPaint);
invalidate();
}
}
package com.color;
import android.content.Context;
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.util.AttributeSet;
import android.widget.ImageView;
public class ColorView extends ImageView {
private Paint myPaint = null;
private Bitmap bitmap = null;
private ColorMatrix myColorMatrix = null;
//设置颜色值
private float[] colorArray = {(float) 0.393,(float) 0.768,(float) 0.189,0,0,
(float) 0.349,(float) 0.686,(float) 0.168,0,0,
(float) 0.272,(float) 0.534,(float) 0.131,0,0,
0,0,0,1,0};
public ColorView(Context context, AttributeSet attrs)
{
super(context, attrs);
bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ww);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//新建画笔对象
myPaint = new Paint();
//描画(原始图片)
canvas.drawBitmap(bitmap,0, 0, myPaint);
//新建颜色矩阵对象
myColorMatrix = new ColorMatrix();
//设置颜色矩阵的值
myColorMatrix.set(colorArray);
//设置画笔颜色过滤器
myPaint.setColorFilter(new ColorMatrixColorFilter(myColorMatrix));
//描画(处理后的图片)
canvas.drawBitmap(bitmap,0,0,myPaint);
invalidate();
}
}
main.xml布局文件
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/colorView_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.color.ColorView
android:id="@+id/myColorView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/colorView_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"