我正在画布上绘制缩放的位图,并希望在指定的时间淡出我的图像。
基本上,当我的角色图像越过画布的特定部分时,我要求角色图像在页面自动重定向到下一个Java类之前,逐渐消失(3秒)。
当前,我的图像只是重定向到新的java类,请在下面的代码中查看我如何创建图像。
Resources res = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, res.getDisplayMetrics());
imgSpacing = (int) px / 2;
int size = (int) ((PhoneWidth / 5) - px);
chrImg = BitmapFactory.decodeResource(getResources(), R.drawable.character);
chrImg = Bitmap.createScaledBitmap(chrImg, size, size, true);
然后在画布上的onDraw中:
if(indexX == mazeFinishX && indexY == mazeFinishY)
{
canvas.drawBitmap(finish, j * totalCellWidth, i * totalCellHeight, null);
// As soon as the character moves over this square they are automatically re-directed to new page
// This is where I want to fade the character image out before the re-direct
}
我在网上看过,但是还不能完全弄清楚如何使从游戏资源drawable文件夹中提取的我的可绘制图像起作用。谢谢
如果您认为有可能要更改渐变动画(例如缩放和/或旋转),那么应该使用动画XML。
但是,对于快速的位图淡入淡出,您可以重复发布延迟的无效消息。您可能希望将无效区域限制为字符位图所在的位置:
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
}
}
...
}
我建议将常量FADE_MILLISECONDS和FADE_STEP放入res / integers.xml中,以免对其进行硬编码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句