zoukankan      html  css  js  c++  java
  • Android图像处理之冰冻效果

                        原图                                                                          效果图



    代码:

    1. package com.color;  
    2.   
    3. import android.content.Context;  
    4. import android.graphics.Bitmap;  
    5. import android.graphics.BitmapFactory;  
    6. import android.graphics.Canvas;  
    7. import android.graphics.Color;  
    8. import android.graphics.Paint;  
    9. import android.util.AttributeSet;  
    10. import android.widget.ImageView;  
    11.   
    12. public class ColorView extends ImageView {  
    13.   
    14.     private Paint myPaint = null;  
    15.     private Bitmap bitmap = null;  
    16.     private int width, height;  
    17.     private int[] oldPixels;  
    18.     private int[] newPixels;  
    19.     private int color, color2;  
    20.     private int pixelsR, pixelsG, pixelsB, pixelsA, pixelsR2, pixelsG2,  
    21.             pixelsB2;  
    22.   
    23.     public ColorView(Context context, AttributeSet attrs) {  
    24.         super(context, attrs);  
    25.         bitmap = BitmapFactory.decodeResource(context.getResources(),  
    26.                 R.drawable.ww);  
    27.         width = bitmap.getWidth();  
    28.         height = bitmap.getHeight();  
    29.         oldPixels = new int[width * height];  
    30.         newPixels = new int[width * height];  
    31.         invalidate();  
    32.     }  
    33.   
    34.     @Override  
    35.     protected void onDraw(Canvas canvas) {  
    36.         super.onDraw(canvas);  
    37.         // 获取像素  
    38.         bitmap.getPixels(oldPixels, 0, width, 0, 0, width, height);  
    39.   
    40.         for (int i = 1; i < height * width; i++) {  
    41.             color = oldPixels[i];  
    42.             // 获取RGB分量  
    43.             pixelsA = Color.alpha(color);  
    44.             pixelsR = Color.red(color);  
    45.             pixelsG = Color.green(color);  
    46.             pixelsB = Color.blue(color);  
    47.             //R  
    48.             int pixel = pixelsR - pixelsG - pixelsB;  
    49.             pixel = pixel * 3 / 2;  
    50.             if (pixel < 0) {  
    51.                 pixel = -pixel;  
    52.             }  
    53.             if (pixel > 255){  
    54.                 pixel = 255;  
    55.             }  
    56.             pixelsR = pixel; // 计算后重置R值,以下类同  
    57.             //G  
    58.             pixel = pixelsG - pixelsR - pixelsB;  
    59.             pixel = pixel * 3 / 2;  
    60.             if (pixel < 0) {  
    61.                 pixel = -pixel;  
    62.             }  
    63.             if (pixel > 255){  
    64.                 pixel = 255;  
    65.             }  
    66.             pixelsG = pixel;  
    67.             //B  
    68.             pixel = pixelsB - pixelsR - pixelsG;  
    69.             pixel = pixel * 3 / 2;  
    70.             if (pixel < 0) {  
    71.                 pixel = -pixel;  
    72.             }  
    73.             if (pixel > 255){  
    74.                 pixel = 255;  
    75.             }  
    76.             pixelsB = pixel;  
    77.               
    78.             // 根据新的RGB生成新像素  
    79.             newPixels[i] = Color.argb(pixelsA, pixelsR, pixelsG, pixelsB);  
    80.   
    81.         }  
    82.         // 根据新像素生成新图片  
    83.         bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);  
    84.         canvas.drawBitmap(bitmap, 0, 0, myPaint);  
    85.     }  
    86. }  



    参考博文:点击打开链接



  • 相关阅读:
    模仿商品分类点击效果
    Element MenuNav刷新后点击菜单保留选中状态
    element后端管理布局
    Element NavMenu动态生成导航菜单
    谁的速度快!谁背锅(技术解析)
    “非科班自学”复盘两个月时间在年底成功拿下了字节、阿里offer,入职了字节!
    用了这么久,你真的明白 HttpClient的实现原理了吗?
    求你了,不要再在对外接口中使用枚举类型了
    Docker 实战总结(非常全面),收藏了!
    Service层和Dao层真的有必要每个类都加上接口吗?
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6722696.html
Copyright © 2011-2022 走看看