是否有可能从Android中的位图删除透明像素 [英] Is it possible to remove transparent pixels from bitmap in android

查看:174
本文介绍了是否有可能从Android中的位图删除透明像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,如果图像未填充imageView,我正在截屏,然后将透明像素添加到位图.是否可以从位图中删除透明像素或截取没有透明像素的屏幕截图.

In my application i am taking screenshot if the image doesn't fill imageView then transparent pixels are added to bitmap.Is it possible to remove transparent pixels from bitmap or take screenshot without transparent pixels.Thanks in advance.

推荐答案

此方法快很多:

static Bitmap trim(Bitmap source) {
    int firstX = 0, firstY = 0;
    int lastX = source.getWidth();
    int lastY = source.getHeight();
    int[] pixels = new int[source.getWidth() * source.getHeight()];
    source.getPixels(pixels, 0, source.getWidth(), 0, 0, source.getWidth(), source.getHeight());
    loop:
    for (int x = 0; x < source.getWidth(); x++) {
        for (int y = 0; y < source.getHeight(); y++) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                firstX = x;
                break loop;
            }
        }
    }
    loop:
    for (int y = 0; y < source.getHeight(); y++) {
        for (int x = firstX; x < source.getWidth(); x++) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                firstY = y;
                break loop;
            }
        }
    }
    loop:
    for (int x = source.getWidth() - 1; x >= firstX; x--) {
        for (int y = source.getHeight() - 1; y >= firstY; y--) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                lastX = x;
                break loop;
            }
        }
    }
    loop:
    for (int y = source.getHeight() - 1; y >= firstY; y--) {
        for (int x = source.getWidth() - 1; x >= firstX; x--) {
            if (pixels[x + (y * source.getWidth())] != Color.TRANSPARENT) {
                lastY = y;
                break loop;
            }
        }
    }
    return Bitmap.createBitmap(source, firstX, firstY, lastX - firstX, lastY - firstY);
}

这篇关于是否有可能从Android中的位图删除透明像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆