Chinaunix首页 | 论坛 | 博客
  • 博客访问: 837563
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: 嵌入式

2011-08-17 16:00:48

本来是要转这个blog的(),但又觉得内容不完全好,那只好改改自己用了。
透明度渐变

实现透明度渐变有两种方法,一是使用蒙板;二是直接修改像素数据,修改每个 像素的 alpha 值。

对于蒙板法,事先做好一张透明度渐变的图,这就是我们的蒙板,在绘制完图像 之后把蒙板图片绘制上去,这样就产生了透明度渐变的效果。

对于第二种方法,我们需要首先读出图片的数据,然后修改每个像素的 alpha 值。下面的代码片段的功能是逐行增加 alpha 值,产生的效果是自上向下由暗 变亮。

int alpha = 0x00000000;

mBitmap.getPixels (pix, 0, w, 0, 0, w, h);

for (int y = 0; y for (int x=0; x int index = y * w + x;
int r = (pix[index] >> 16) & 0xff;
int g = (pix[index] >> 8) & 0xff;
int b = pix[index] & 0xff;

pix[index] = alpha | (r<<16) | (g<<8) | b;
}
alpha = alpha + 0x01000000;

}蒙板:
这个是很方便,两个图合在一起就行了,但是如果有背景就杯具了,蒙板必须和背景是一样才行。
像素:
这个嘛,操作麻烦一点,还好。
拿到像素后怎么操作呢,先看一下这些像素是什么:
public void getPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)
Since: 

Returns in pixels[] a copy of the data in the bitmap. Each value is a packed int representing a . The stride parameter allows the caller to allow for gaps in the returned pixels array between rows. For normal packed results, just pass width for the stride value.

第一个像素值都是一个Color的值,那看看Color中怎么表示的:
Class Overview

The Color class defines methods for creating and converting color ints. Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributes from red, gree, blue, and opaque-white would be 0xFFFFFFFF

一个int,最高的8位是alpha值,我们改这个就可以了。
阅读(880) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~