【问题标题】:How to darken the Canvas or the Image drawn on a Canvas?如何使画布或画布上绘制的图像变暗?
【发布时间】:2011-11-30 14:38:13
【问题描述】:

我派生了Canvas 类,在paint 方法中我绘制了一个Image。单击Command 时,我想在画布中绘制一个矩形和矩形内的字符串,并且图像仍将显示在矩形后面,因此我想使图像变暗一点,因为我想制作视觉效果就像显示 LWUIT Dialog 时的一样(隐藏表单的色调)。那么在这种情况下如何使画布变暗一点呢?

【问题讨论】:

    标签: java-me midp lcdui


    【解决方案1】:

    假设图像的宽度和高度已知,我可能首先使用 Image.getRGB 来获取图像像素的 ARGB 值。

    然后,我会缩放 RGB 值以使其变暗。

    int[] darken(int[] argb, int percentage) {
        int[] result = new int[argb.length];
        for (int i = 0; i <argb.length; i++) {
            result[i] = darkenArgb(argb[i], percentage);
        }
        return result;
    }
    
    private int darkenArgb(int argb, int percentage) {
        return darkenByte(argb, 3, 100) // keep alpha as-is
                | darkenByte(argb, 2, percentage)
                | darkenByte(argb, 1, percentage)
                | darkenByte(argb, 0, percentage);
    }
    
    private int darkenByte(int argb, int index, int percentage) {
        if (percentage < 0 || percentage > 100) {
            throw new IllegalArgumentException("unexpected percentage: ["
                   + percentage + "]");
        }
        if (index < 0 || index > 3) {
            throw new IllegalArgumentException("unexpected index: [" + index + "]");
        }
        int result = (argb >> index) & 0xFF;
        result = result * percentage / 100;
        return result << index;
    }
    

    从使用darken方法获得的数组中,可以使用Image.createRGBImage制作暗化图像

    【讨论】:

    • 方法darkenpercentage的值应该是多少?
    • @pheromix 你必须通过实验找出答案。我会尝试至少 25、50 和 75 来测试它的感觉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 2018-12-09
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多