admin管理员组文章数量:1573363
refreshBubbles();
postInvalidate();
} catch (InterruptedException e) {
System.out.println("Bubble线程结束");
break;
}
}
}
};
mBubbleThread.start();
}
#### 1.4 关闭线程
由于线程运行时监听了 interrupt 中断,这里直接使用 interrupt 通知线程中断就可以了。
// 停止气泡线程
private void stopBubbleSync() {
if (null == mBubbleThread) return;
mBubbleThread.interrupt();
mBubbleThread = null;
}
#### 1.5 创建气泡
为了防止气泡数量过多而占用太多的性能,因此在创建气泡之前需要先判断当前已经有多少个气泡,如果已经有足够多的气泡了,则不再创建新的气泡。
同时,为了让气泡产生过程看起来更合理,在气泡数量没有达到上限之前,会随机的创建气泡,以防止气泡扎堆出现,因此设立了一个随机项,生成的随机数大于 0.95 的时候才生成气泡,让气泡生成过程慢一些。
创建气泡的过程也很简单,就是随机的在设定范围内生成一些属性,然后放到 List 中而已。
PS:这里使用了一些硬编码和魔数,属于不太好的习惯。不过由于应用场景固定,这些参数需要调整的概率比较小,影响也不大。
// 尝试创建气泡
private void tryCreateBubble() {
if (null == mContentRectF) return;
if (mBubbles.size() >= mBubbleMaxSize) {
return;
}
if (random.nextFloat() < 0.95) {
return;
}
Bubble bubble = new Bubble();
int radius = random.nextInt(mBubbleMaxRadius - mBubbleMinRadius);
radius += mBubbleMinRadius;
float speedY = random.nextFloat() * mBubbleMaxSpeedY;
while (speedY < 1) {
speedY = random.nextFloat() * mBubbleMaxSpeedY;
}
bubble.radius = radius;
bubble.speedY = speedY;
bubble.x = mWaterRectF.centerX();
bubble.y = mWaterRectF.bottom - radius - mBottleBorder / 2;
float speedX = random.nextFloat() - 0.5f;
while (speedX ==
版权声明:本文标题:Android超简单气泡效果,附带学习经验 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1727745792a1127781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论