L
Lawrence D'Oliveiro
Here’s an example of a routine that draws into a Bitmap via a Canvas, and
then does accesses to pixels in the Bitmap. Typically when doing the
equivalent thing with other drawing APIs, there is a “flush†call you use to
ensure that all pending drawing that might affect those pixels has in fact
been done. E.g.
<http://cairographics.org/manual/cairo-cairo-surface-t.html#cairo-surface-flush>.
The nearest I can find to this in android.graphics.Bitmap is prepareToDraw. Does it
in fact do what I think it does?
static android.graphics.Paint FillWithColor
(
int TheColor
)
{
final android.graphics.Paint ThePaint = new android.graphics.Paint();
ThePaint.setStyle(android.graphics.Paint.Style.FILL);
ThePaint.setColor(TheColor);
return
ThePaint;
} /*FillWithColor*/
static boolean PointInShape
(
android.graphics.PointF Where,
android.graphics.Path TheShape
)
/* does the specified point lie within the shape. */
{
final android.graphics.Bitmap HitBits = android.graphics.Bitmap.createBitmap
(
/*width =*/ 8, /* actually 1x1 pixel would be enough */
/*height =*/ 8,
/*config =*/ android.graphics.Bitmap.Config.ARGB_8888
/* 1 bit per pixel would be enough, tried using ALPHA_8, didn't work */
);
final android.graphics.Canvas HitDraw = new android.graphics.Canvas(HitBits);
HitDraw.drawARGB(255, 0, 0, 0);
final android.graphics.Matrix HitMatrix = HitDraw.getMatrix();
HitMatrix.postTranslate(- Where.x, - Where.y); /* so Where is mapped to (0, 0) */
HitDraw.setMatrix(HitMatrix);
HitDraw.drawPath(TheShape, FillWithColor(0xffffffff));
HitBits.prepareToDraw();
return
(HitBits.getPixel(0, 0) & 0x00ffffff) != 0;
} /*PointInShape*/
then does accesses to pixels in the Bitmap. Typically when doing the
equivalent thing with other drawing APIs, there is a “flush†call you use to
ensure that all pending drawing that might affect those pixels has in fact
been done. E.g.
<http://cairographics.org/manual/cairo-cairo-surface-t.html#cairo-surface-flush>.
The nearest I can find to this in android.graphics.Bitmap is prepareToDraw. Does it
in fact do what I think it does?
static android.graphics.Paint FillWithColor
(
int TheColor
)
{
final android.graphics.Paint ThePaint = new android.graphics.Paint();
ThePaint.setStyle(android.graphics.Paint.Style.FILL);
ThePaint.setColor(TheColor);
return
ThePaint;
} /*FillWithColor*/
static boolean PointInShape
(
android.graphics.PointF Where,
android.graphics.Path TheShape
)
/* does the specified point lie within the shape. */
{
final android.graphics.Bitmap HitBits = android.graphics.Bitmap.createBitmap
(
/*width =*/ 8, /* actually 1x1 pixel would be enough */
/*height =*/ 8,
/*config =*/ android.graphics.Bitmap.Config.ARGB_8888
/* 1 bit per pixel would be enough, tried using ALPHA_8, didn't work */
);
final android.graphics.Canvas HitDraw = new android.graphics.Canvas(HitBits);
HitDraw.drawARGB(255, 0, 0, 0);
final android.graphics.Matrix HitMatrix = HitDraw.getMatrix();
HitMatrix.postTranslate(- Where.x, - Where.y); /* so Where is mapped to (0, 0) */
HitDraw.setMatrix(HitMatrix);
HitDraw.drawPath(TheShape, FillWithColor(0xffffffff));
HitBits.prepareToDraw();
return
(HitBits.getPixel(0, 0) & 0x00ffffff) != 0;
} /*PointInShape*/