欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android图片特效:黑白特效、圆角效果、高斯模糊

程序员文章站 2023-12-12 19:07:52
1.黑白效果 复制代码 代码如下:/**     * 将彩色图转换为黑白图     * &...

1.黑白效果

复制代码 代码如下:

/**
     * 将彩色图转换为黑白图
     *
     * @param 位图
     * @return 返回转换好的位图
     */
    public static bitmap converttoblackwhite(bitmap bmp) {
        int width = bmp.getwidth(); // 获取位图的宽
        int height = bmp.getheight(); // 获取位图的高

        int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组

        bmp.getpixels(pixels, 0, width, 0, 0, width, height);
        int alpha = 0xff << 24;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int grey = pixels[width * i + j];

                int red = ((grey & 0x00ff0000) >> 16);
                int green = ((grey & 0x0000ff00) >> 8);
                int blue = (grey & 0x000000ff);

                grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
                grey = alpha | (grey << 16) | (grey << 8) | grey;
                pixels[width * i + j] = grey;
            }
        }
        bitmap newbmp = bitmap.createbitmap(width, height, config.rgb_565);
        newbmp.setpixels(pixels, 0, width, 0, 0, width, height);
        return newbmp;
    }

Android图片特效:黑白特效、圆角效果、高斯模糊

2.图片圆角

复制代码 代码如下:

/**
     * 转换成圆角
     *
     * @param bmp
     * @param roundpx
     * @return
     */
    public static bitmap converttoroundedcorner(bitmap bmp, float roundpx) {

        bitmap newbmp = bitmap.createbitmap(bmp.getwidth(), bmp.getheight(),
                config.argb_8888);
        // 得到画布
        canvas canvas = new canvas(newbmp);

        final int color = 0xff424242;
        final paint paint = new paint();
        final rect rect = new rect(0, 0, bmp.getwidth(), bmp.getheight());
        final rectf rectf = new rectf(rect);

        paint.setantialias(true);
        canvas.drawargb(0, 0, 0, 0);
        paint.setcolor(color);
        // 第二个和第三个参数一样则画的是正圆的一角,否则是椭圆的一角
        canvas.drawroundrect(rectf, roundpx, roundpx, paint);

        paint.setxfermode(new porterduffxfermode(mode.src_in));
        canvas.drawbitmap(bmp, rect, rect, paint);

        return newbmp;
    }

Android图片特效:黑白特效、圆角效果、高斯模糊

3.高斯模糊

复制代码 代码如下:

/**
     * 高斯模糊
     *
     * @param bmp
     * @return
     */
    public static bitmap converttoblur(bitmap bmp) {
        // 高斯矩阵
        int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };

        int width = bmp.getwidth();
        int height = bmp.getheight();
        bitmap newbmp = bitmap.createbitmap(width, height,
                bitmap.config.rgb_565);

        int pixr = 0;
        int pixg = 0;
        int pixb = 0;

        int pixcolor = 0;

        int newr = 0;
        int newg = 0;
        int newb = 0;

        int delta = 16; // 值越小图片会越亮,越大则越暗

        int idx = 0;
        int[] pixels = new int[width * height];
        bmp.getpixels(pixels, 0, width, 0, 0, width, height);
        for (int i = 1, length = height - 1; i < length; i++) {
            for (int k = 1, len = width - 1; k < len; k++) {
                idx = 0;
                for (int m = -1; m <= 1; m++) {
                    for (int n = -1; n <= 1; n++) {
                        pixcolor = pixels[(i + m) * width + k + n];
                        pixr = color.red(pixcolor);
                        pixg = color.green(pixcolor);
                        pixb = color.blue(pixcolor);

                        newr = newr + pixr * gauss[idx];
                        newg = newg + pixg * gauss[idx];
                        newb = newb + pixb * gauss[idx];
                        idx++;
                    }
                }

                newr /= delta;
                newg /= delta;
                newb /= delta;

                newr = math.min(255, math.max(0, newr));
                newg = math.min(255, math.max(0, newg));
                newb = math.min(255, math.max(0, newb));

                pixels[i * width + k] = color.argb(255, newr, newg, newb);

                newr = 0;
                newg = 0;
                newb = 0;
            }
        }

        newbmp.setpixels(pixels, 0, width, 0, 0, width, height);

        return newbmp;
    }

上一篇:

下一篇: