[译]用MIDP 2.0 实现图片渐入渐出效果

May 2004

用MIDP 2.0 实现图片渐入渐出效果

原文出处:http://developer.sonyericsson.com/site/global/techsupport/tipstrickscode/java/p_java_0501.jsp

 

译者: clapton_xpAThotmailDOTcom
2004 年 8 月


 

本文介绍如何通过改变图像的alpha值使其呈现渐变效果,文后附有示例MIDlet代码。

MIDP 2.0 中Image类有一个新的方法, getRGB(...) ,它可以将image的alpha以及RGB值转换为一个int数组。 我们可以用该方法以及获得的数组改变图片的alpha值。

 

j2me 里的int是4 bytes的,image中的每个像素以ARGB值的方式描述,每个值可以为0-255。 如果 alpha 值为 0, 对应的像素就为透明,反之如果alpha 值为255, 像素为完全不透明。

 

从int数组中获得每个像素的颜色可以使用AND '&' 操作符。我们可以先得到像素的颜色,然后再附加上我们想要得到的渐变效果。

 

FF = 11111111 = 255
0xFFFFFFFF - Alpha = 255, Red =255 Green = 255, Blue = 255
(0xFFFFFFFF & 0x00FFFFFF) = 0x00FFFFFF

 

上面的代码我们可以得到int数组的RGB颜色值,alpha值为0。

 

现在我们有了alpha值为0的RGB颜色值,我们只需在其上附加新的alpha值。

 

如果我们想把颜色的alpha值设为255,我们需要使用左移操作符。

 

操作方法:
(00000000 00000000 00000000 11111111) to
(11111111 00000000 00000000 00000000)
use the shift left '<<' operator.
(0xFF << 24) = 0xFF000000.

 

clapton_xpAThotmailDOTcom

 

用这种方法我们可以改变颜色的alpha值或者实现遮盖效果。

用image的getRGB(...) 方法获得图像的所有像素的ARGB值,并放入一个int数组。
用下面的blend方法改变数组中每个值(像素)的alpha值。
用image的 createRGBImage(...) 方法从我们修改过的int数组创建一个新的image。
下面是使用 getRGB 和 createRGBImage 方法的例子:

 

    public static void blend(int[] raw, int alphaValue){
        int len = raw.length;
       
        // Start loop through all the pixels in the image.
        for(int i=0; i            int a = 0;
            int color = (raw[i] & 0x00FFFFFF); // get the color
of the pixel.
            a = alphaValue;     // set the alpha value we want
to use 0-255.

            a = (a<<24);    // left shift the alpha value 24 bits.
            // if color = 00000000 11111111 11111111 00000000
(0xFFFF00 = Yellow)
            // and alpha= 01111111 00000000 00000000 00000000
            // then c+a = 01111111 11111111 11111111 00000000
            // and the pixel will be blended.
            color += a;
            raw[i] = color;
        }
    }

 

 

下载源代码>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值