I am drawing a scaled bitmap onto a canvas and would like to fade my image out at a specified time.
Basically, when my character image goes over a certain section of the
If you think there's a possibility you will want to change the fade animation down the road, such as scaling and/or rotating, then you should go with an animation XML.
But for just a quick bitmap fade, you can repeatedly post delayed invalidation messages. You probably want to limit your invalidated area to just where your character bitmap is:
private static final int FADE_MILLISECONDS = 3000; // 3 second fade effect
private static final int FADE_STEP = 120; // 120ms refresh
// Calculate our alpha step from our fade parameters
private static final int ALPHA_STEP = 255 / (FADE_MILLISECONDS / FADE_STEP);
// Initializes the alpha to 255
private Paint alphaPaint = new Paint();
// Need to keep track of the current alpha value
private int currentAlpha = 255;
@Override
protected void onDraw(Canvas canvas) {
...
if(indexX == mazeFinishX && indexY == mazeFinishY) {
// Drawing your wormhole?
int x = j * totalCellWidth;
int y = i * totalCellHeight;
canvas.drawBitmap(finish, x, y, null);
if (currentAlpha > 0) {
// Draw your character at the current alpha value
canvas.drawBitmap(chrImg, x, y, alphaPaint);
// Update your alpha by a step
alphaPaint.setAlpha(currentAlpha);
currentAlpha -= ALPHA_STEP;
// Assuming you hold on to the size from your createScaledBitmap call
postInvalidateDelayed(FADE_STEP, x, y, x + size, y + size);
} else {
// No character draw, just reset your alpha paint
currentAlpha = 255;
alphaPaint.setAlpha(currentAlpha);
// Now do your redirect
}
}
...
}
I'd recommend putting the constants FADE_MILLISECONDS and FADE_STEP into res/integers.xml just so they aren't hard-coded.