今天我们接着上次讲的内容,介绍另一种改变图片色彩的方法:像素;
今天通过一个例子来熟悉像素操作:实现图片的 底片,怀旧,雕塑,这三种比较有意思的效果。

首先,上图,勾引一下你。

然后开始:

我们知道,图片是由众多排列紧密的像素点构成,每一个像素点都包含四个信息,即R,G,B,A;于是,我们通过改变每个像素的这几个值,就可以达到改变图片色彩的目的:这就是我们的主要思路,下面我们开始代码的编写工作。

新建一个xml文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >    <LinearLayout  android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" >        <ImageView  android:id="@+id/image1" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" />        <ImageView  android:id="@+id/image2" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" />    </LinearLayout>    <LinearLayout  android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" >        <ImageView  android:id="@+id/image3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" />        <ImageView  android:id="@+id/image4" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" />    </LinearLayout></LinearLayout>

很简单,四个图片,均匀分布,分别用来显示:原图,底片,怀旧,雕塑

在贴代码之前,首先发几张图帮助理解:



下面是实现三个效果的代码,并有详细的注释:

首先:底片

public Bitmap handleNegative(Bitmap bt) {        int width = bt.getWidth();        int height = bt.getHeight();        int color;        int r, g, b, a;        Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);        // 存放原来的像素值        int oldPix[] = new int[width * height];        int newPix[] = new int[width * height];        // 将像素值赋值给oldpix;        bt.getPixels(oldPix, 0, width, 0, 0, width, height);        // 循环取出每个像素,并对其进行更改        for (int i = 0; i < oldPix.length; i++) {            // 分别取出这个像素值对应的RGB值            color = oldPix[i];            r = Color.red(color);            g = Color.green(color);            b = Color.red(color);            a = Color.alpha(color);            // 应用底片变换公式            r = 255 - r;            g = 255 - g;            b = 255 - b;            // 检查越界            if (r < 0) {                r = 0;            } else if (r > 255) {                r = 255;            }            if (g < 0) {                g = 0;            } else if (g > 255) {                g = 255;            }            if (b < 0) {                b = 0;            } else if (b > 255) {                b = 255;            }            newPix[i] = Color.argb(a, r, g, b);        }        bmp.setPixels(newPix, 0, width, 0, 0, width, height);        return bmp;    }

其次:怀旧

public Bitmap handleOld(Bitmap bt) {        int width = bt.getWidth();        int height = bt.getHeight();        int color;        int r, g, b, a;        Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);        // 存放原来的像素值        int oldPix[] = new int[width * height];        int newPix[] = new int[width * height];        // 将像素值赋值给oldpix;        bt.getPixels(oldPix, 0, width, 0, 0, width, height);        // 循环取出每个像素,并对其进行更改        for (int i = 0; i < oldPix.length; i++) {            // 分别取出这个像素值对应的RGB值            color = oldPix[i];            r = Color.red(color);            g = Color.green(color);            b = Color.red(color);            a = Color.alpha(color);            // 应用底片变换公式            r = (int) (0.393*r+0.769*r+0.189*r);            g = (int) (0.349*g+0.686*g+0.189*g);            b = (int) (0.272*b+0.534*b+0.131*b);            // 检查越界            if (r < 0) {                r = 0;            } else if (r > 255) {                r = 255;            }            if (g < 0) {                g = 0;            } else if (g > 255) {                g = 255;            }            if (b < 0) {                b = 0;            } else if (b > 255) {                b = 255;            }            newPix[i] = Color.argb(a, r, g, b);        }        bmp.setPixels(newPix, 0, width, 0, 0, width, height);        return bmp;    }

最后:雕塑

public Bitmap handleRelief(Bitmap bt) {        int width = bt.getWidth();        int height = bt.getHeight();        int color,color1;        int r, g, b, a;        int r1,g1,b1;        Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);        // 存放原来的像素值        int oldPix[] = new int[width * height];        int newPix[] = new int[width * height];        // 将像素值赋值给oldpix;        bt.getPixels(oldPix, 0, width, 0, 0, width, height);        // 循环取出每个像素,并对其进行更改        for (int i = 0; i < oldPix.length; i++) {            // 分别取出这个像素值对应的RGB值            color = oldPix[i];            r = Color.red(color);            g = Color.green(color);            b = Color.red(color);            a = Color.alpha(color);            //注意这里的处理:防止数组越界            color1 = oldPix[i==0?0:i-1];            r1 = Color.red(color1);            g1 = Color.green(color1);            b1 = Color.red(color1);            // 应用底片变换公式            r = r1-r+127;            g = g1-g+127;            b = b1-b+127;            // 检查越界            if (r < 0) {                r = 0;            } else if (r > 255) {                r = 255;            }            if (g < 0) {                g = 0;            } else if (g > 255) {                g = 255;            }            if (b < 0) {                b = 0;            } else if (b > 255) {                b = 255;            }            newPix[i] = Color.argb(a, r, g, b);        }        bmp.setPixels(newPix, 0, width, 0, 0, width, height);        return bmp;    }

很容易理解了。

最后:Demo地址 http://download.csdn.net/detail/nsgsbs/8533269

更多相关文章

  1. Android屏幕适配指南(根据官方翻译总结)
  2. 独家消息:Sony S1 Android(安卓)平板正式命名为 Tablet S,将于九月
  3. android没有告诉你的usr模式和eng模式的区别
  4. android中常用的数据单位和尺寸
  5. 二十一、Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
  6. Android(安卓)关于适配
  7. Android中DP,PX,DPI及分辨率的关系
  8. Android像素单位dp、sp、px、pt的区别和比较
  9. Android(安卓)UI总结 Android(安卓)和H5 字体大小适配

随机推荐

  1. Android中Activity的四种启动模式
  2. 学习android随手记 之 android:name andr
  3. android开发之Parcelable使用详解
  4. Android下USB Accessory的实现分析 (三)---
  5. android nfc中MifareClassic格式的读写
  6. android-1-Android简单入门
  7. android开发中常用的设计模式汇总
  8. android 如何调用选择文件模块
  9. android怎么引入第三方包
  10. Android中为什么主线程不会因为Looper.lo