1

私はこのイメージを持っています:

ここに画像の説明を入力

範囲が[0、π]のα値もあります。基本的に、それは見える角度を表します。

画像に動的透明マスクを適用したいので、α が π/2 に等しい場合、左半分だけが表示されます。

このプロセスを考えて、各ピクセルの可視性を計算しました。

private boolean[][] getVisibilityArray(final int height, final int width, final double value) {

    final boolean[][] visibility = new boolean[width][height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            final double xInSqrt = (width / 2) - x;
            final double yInSqrt = height - y;
            final double sumInSqrt = xInSqrt * xInSqrt + yInSqrt * yInSqrt;
            final double hipotenusa = Math.sqrt(sumInSqrt);
            final double adyacente = Math.abs((width / 2) - x);
            final double cos = adyacente / hipotenusa;
            double angle = Math.acos(cos);
            if (x > width / 2) {
                angle = Math.PI - angle;
            }
            visibility[x][y] = angle <= value;
        }
    }
    return visibility;
}

ただし、ビットマップを生成して元のビットマップにマスクを適用することは、私の理解を超えています。

どうすればこの効果を達成できますか?

4

1 に答える 1

0

結局、答えは当初考えていたよりも明白でした。

final boolean[][] visibility = getVisibilityArray(currentGauge.getHeight(),
                    currentGauge.getWidth(), angle);
final Bitmap clone = Bitmap.createBitmap(currentGauge.getWidth(),
                    currentGauge.getHeight(), Config.ARGB_8888);
for (int y = 0; y < currentGauge.getHeight(); y++) {
    for (int x = 0; x < currentGauge.getWidth(); x++) {
        if (!visibility[x][y]) {
            clone.setPixel(x, y, 0);
        } else {
            clone.setPixel(x, y, currentGauge.getPixel(x, y));
        }
    }
}

Clone には目的のビットマップが含まれています。マスキングなしで、不要なピクセルを 0 (0x00000000 は不透明度 0% の黒) に設定するだけで透明になります。

于 2013-09-18T10:18:07.937 に答える