我是
靠谱客的博主
丰富烤鸡,最近开发中收集的这篇文章主要介绍
怀旧效果,觉得挺不错的,现在分享给大家,希望可以做个参考。
图片怀旧效果的算法:
我们用颜色矩阵(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
- 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布局文件
- <?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>
怀旧效果 原图
方法2:
将每个像素点的RGB值先分离出来,然后再按照上面的三个算式分别重新计算出RGB值然后做为当前点的RGB值
ColorView.java
- package com.color;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- 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 int width,height;
- public ColorView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ww);
- width = bitmap.getWidth();
- height = bitmap.getHeight();
- invalidate();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
-
- int pixColor = 0;
- int pixR = 0;
- int pixG = 0;
- int pixB = 0;
- int newR = 0;
- int newG = 0;
- int newB = 0;
- int[] pixels = new int[width * height];
-
-
-
-
-
-
-
-
-
-
- bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
-
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- pixColor = pixels[width * i + j];
-
- pixR = Color.red(pixColor);
-
- pixG = Color.green(pixColor);
-
- pixB = Color.blue(pixColor);
-
- newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);
- newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);
- newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);
-
-
- int newColor = Color.argb(255, newR > 255 ? 255 : newR, newG > 255 ? 255 : newG, newB > 255 ? 255 : newB);
- pixels[width * i + j] = newColor;
- }
- }
-
- bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
-
- canvas.drawBitmap(bitmap,0,0,myPaint);
- }
- }
发表评论 取消回复