Android位图模糊与alpha

时间:2013-01-01 20:39:11

标签: android bitmap alpha blur

我发现了如何模糊我的位图,但仍有黑色光环。我知道它可以通过预先模糊图像alpha来修复,但我不知道该怎么做......

感谢您的帮助!

圆 - 方法:

private void drawCircle() {
    ImageView img = (ImageView) findViewById(R.id.brushPreview);
    Bitmap bmp = Bitmap.createBitmap(260, 260, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmp);
    Paint mPaint = new Paint();
    SeekBar[] seekBar = { (SeekBar) findViewById(R.id.seekBarSize), (SeekBar) findViewById(R.id.seekBarTrans), (SeekBar) findViewById(R.id.seekBarHard) };

    mPaint.setDither(true);
    mPaint.setColor(0xFFfff000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(seekBar[0].getProgress());
    c.drawPoint(130, 130, mPaint);

    bmp = getBlurredBitmap(bmp, seekBar[2].getProgress());

    img.setImageBitmap(bmp);
}

模糊-方法

public Bitmap getBlurredBitmap(Bitmap original, int radius) {
    if (radius < 1)
        return original;

    int width = original.getWidth();
    int height = original.getHeight();
    int wm = width - 1;
    int hm = height - 1;
    int wh = width * height;
    int div = radius + radius + 1;
    int a[] = new int[wh];
    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int asum, rsum, gsum, bsum, x, y, i, p, p1, p2, yp, yi, yw;
    int vmin[] = new int[Math.max(width, height)];
    int vmax[] = new int[Math.max(width, height)];
    int dv[] = new int[256 * div];
    for (i = 0; i < 256 * div; i++)
        dv[i] = i / div;

    int[] blurredBitmap = new int[wh];
    original.getPixels(blurredBitmap, 0, width, 0, 0, width, height);

    yw = yi = 0;

    for (y = 0; y < height; y++) {
        asum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = blurredBitmap[yi + Math.min(wm, Math.max(i, 0))];
            asum += (p & 0xff) >> 24;
            rsum += (p & 0xff0000) >> 16;
            gsum += (p & 0x00ff00) >> 8;
            bsum += (p & 0x0000ff);
        }
        for (x = 0; x < width; x++) {
            a[yi] = dv[asum];
            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
                vmax[x] = Math.max(x - radius, 0);
            }
            p1 = blurredBitmap[yw + vmin[x]];
            p2 = blurredBitmap[yw + vmax[x]];

            asum += ((p1 >> 24) & 0xff) - ((p2 >> 24) & 0xff);
            rsum += ((p1 & 0x00ff0000) - (p2 & 0x00ff0000)) >> 16;
            gsum += ((p1 & 0x0000ff00) - (p2 & 0x0000ff00)) >> 8;
            bsum += (p1 & 0x000000ff) - (p2 & 0x000000ff);
            yi++;
        }
        yw += width;
    }

    for (x = 0; x < width; x++) {
        asum = rsum = gsum = bsum = 0;
        yp = -radius * width;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;
            asum += a[yi];
            rsum += r[yi];
            gsum += g[yi];
            bsum += b[yi];
            yp += width;
        }
        yi = x;
        for (y = 0; y < height; y++) {
            blurredBitmap[yi] = (dv[asum]<<24) | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum];
            if (x == 0) {
                vmin[y] = Math.min(y + radius + 1, hm) * width;
                vmax[y] = Math.max(y - radius, 0) * width;
            }
            p1 = x + vmin[y];
            p2 = x + vmax[y];

            asum += a[p1] - a[p2];
            rsum += r[p1] - r[p2];
            gsum += g[p1] - g[p2];
            bsum += b[p1] - b[p2];

            yi += width;
        }
    }

    return Bitmap.createBitmap(blurredBitmap, width, height, Bitmap.Config.ARGB_8888);
}

0 个答案:

没有答案