Hi there! I'm trying to manipulate a bitmap pixels using android
graphics api.
The idea is to create a heatmap using a black/white/alpha gradient.
Latter I'll change the color of each pixel depending on it's alpha
(red to 255 alpha, green to 0 alpha)
The problem I'm having is that all the pixels read are showing as
black opaque. Here's my code:
private void init() {
backbuffer = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
myCanvas = new Canvas(backbuffer);
Paint p = new Paint();
p.setStyle(Paint.Style.FILL);
p.setColor(Color.WHITE);
myCanvas.drawRect(0, 0, getWidth(), getHeight(), p);
}
@Override
protected void onDraw(Canvas canvas) {
if (backbuffer == null) {
init();
}
canvas.drawBitmap(backbuffer, 0, 0, new
Paint(Paint.ANTI_ALIAS_FLAG));
}
public void addPoint(float x, float y) {
RadialGradient g = new RadialGradient(x, y, 20,
Color.argb(25, 0, 0, 0), Color.argb(0, 255,
255, 255),
TileMode.CLAMP);
Paint gp = new Paint();
gp.setShader(g);
myCanvas.drawCircle(x, y, 20, gp);
addColor(x-20, y-20, 20*2);
invalidate();
}
private void addColor(float x, float y, float d) {
if (x + d > myCanvas.getWidth()) {
x = myCanvas.getWidth() - d;
}
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (y + d > myCanvas.getHeight()) {
y = myCanvas.getHeight() - d;
}
int r = 0, g = 0, b = 0, tmp = 0;
int[] pixels = new int[(int) (d * d)];
backbuffer.getPixels(pixels, 0, (int) d, (int) x, (int) y,
(int) d,
(int) d);
for (int i=0;i<pixels.length;i++) {
int alpha = pixels[i] >>> 24;
if (alpha <= 255 && alpha >= 235) {
tmp = 255 - alpha;
r = 255 - tmp;
g = tmp * 12;
} else if (alpha <= 234 && alpha >= 200) {
tmp = 234 - alpha;
r = 255 - (tmp * 8);
g = 255;
} else if (alpha <= 199 && alpha >= 150) {
tmp = 199 - alpha;
g = 255;
b = tmp * 5;
} else if (alpha <= 149 && alpha >= 100) {
tmp = 149 - alpha;
g = 255 - (tmp * 5);
b = 255;
} else
b = 255;
pixels[i] = Color.rgb(r, g, b);
}
}
Any ideas why transparent pixels, and different alpha values not being
shown?
Regards
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en