gpt4 book ai didi

android - 问题实现 9 个旧的 Androids Flakes

转载 作者:行者123 更新时间:2023-11-30 03:57:10 24 4
gpt4 key购买 nike

我是 Android 新手,我正在尝试将 9 Old Androids flakes 演示实现到我的应用程序中。但是,当我将图像资源从演示使用的图像更改为我选择的应用程序之一时,应用程序崩溃并显示 IllegalArgumentException:宽度和高度必须 > 0。我的 logcat 显示我 createscaledbitmap 和 createFlake 所在的行导致了问题.我不知道为什么会这样,演示图像的宽度为 75px x 90px,而我的图像宽度为 166px x 72px。非常感谢任何帮助。

FLAKE VIEW 类(我创建 Flake 的地方)

void addFlakes(int quantity) {
for (int i = 0; i < quantity; ++i) {
flakes.add(Flake.createFlake(getWidth(), droid)); //This line causes exception
}
setNumFlakes(numFlakes + quantity);
}

/**
* Subtract the specified number of droidflakes. We just take them off the end of the
* list, leaving the others unchanged.
*/
void subtractFlakes(int quantity) {
for (int i = 0; i < quantity; ++i) {
int index = numFlakes - i - 1;
flakes.remove(index);
}
setNumFlakes(numFlakes - quantity);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Reset list of droidflakes, then restart it with 8 flakes
flakes.clear();
numFlakes = 0;
addFlakes(8); //This line causes exception
// Cancel animator in case it was already running
animator.cancel();
// Set up fps tracking and start the animation
startTime = System.currentTimeMillis();
prevTime = startTime;
frames = 0;
animator.start();
}

FLAKE CLASS(我创建缩放位图的地方)

static Flake createFlake(float xRange, Bitmap originalBitmap) {
Flake flake = new Flake();
// Size each flake with a width between 5 and 55 and a proportional height
flake.width = (int)(5 + (float)Math.random() * 50);
float hwRatio = originalBitmap.getHeight() / originalBitmap.getWidth();
flake.height = (int)(flake.width * hwRatio);

// Position the flake horizontally between the left and right of the range
flake.x = (float)Math.random() * (xRange - flake.width);
// Position the flake vertically slightly off the top of the display
flake.y = 0 - (flake.height + (float)Math.random() * flake.height);

// Each flake travels at 50-200 pixels per second
flake.speed = 50 + (float) Math.random() * 150;

// Flakes start at -90 to 90 degrees rotation, and rotate between -45 and 45
// degrees per second
flake.rotation = (float) Math.random() * 180 - 90;
flake.rotationSpeed = (float) Math.random() * 90 - 45;

// Get the cached bitmap for this size if it exists, otherwise create and cache one
flake.bitmap = bitmapMap.get(flake.width);
if (flake.bitmap == null) {
flake.bitmap = Bitmap.createScaledBitmap(originalBitmap, //This line causes exception
(int)flake.width, (int)flake.height, true);
bitmapMap.put(flake.width, flake.bitmap);
}
return flake;
}

最佳答案

由于你的图片宽度大于高度,所以基本上需要将这三行代码反转过来。

flake.width = (int)(5 + (float)Math.random() * 50);
float hwRatio = originalBitmap.getHeight() / originalBitmap.getWidth();
flake.height = (int)(flake.width * hwRatio);

对此

flake.height = (int)(5 + (float)Math.random() * 50);
float hwRatio = originalBitmap.getWidth() / originalBitmap.getheight();
flake.width = (int)(flake.height * hwRatio);

关于android - 问题实现 9 个旧的 Androids Flakes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13168196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com