探秘Android粒子系统库:Leonids
Leonids 是一款针对标准Android UI设计的轻量级粒子系统库,它的小巧令人印象深刻——LeonidsLib.jar仅81Kb大小。你可以通过Google Play商店下载Leonids演示应用,亲身体验其功能。
快速上手
在Android项目中集成Leonids非常简单:
Android Studio/Gradle
只需将以下依赖添加到你的build.gradle文件:
dependencies {
compile 'com.plattysoft.leonids:LeonidsLib:1.3.2'
}
如果遇到问题,记得更新jcenter仓库:
repositories {
jcenter{
url "http://jcenter.bintray.com/"
}
}
Eclipse/Jar 文件
将LeonidsLib.jar放入你的app的libs目录即可。
为什么选择Leonids?
粒子系统通常用于游戏中的爆炸、火焰、烟雾等效果,但它们同样可以为普通应用增加生动性和趣味性。尽管游戏引擎都支持粒子系统,但Android UI却缺乏相应的支持。Leonids就是为填补这一空白而生,让使用标准Android UI的开发者也能轻松实现粒子效果。
简单使用示例
创建并启动一个一次性粒子系统只需要三行代码:
new ParticleSystem(this, numParticles, drawableResId, timeToLive)
.setSpeedRange(0.2f, 0.5f)
.oneShot(anchorView, numParticles);
在这里,你需要提供粒子数量、粒子图片资源ID以及粒子存活时间。设置速度范围后,调用oneShot()
,传入发射点视图和发射数量,即可实现粒子发射。
发射器
通过设置发射器,你可以控制粒子持续不断地被发射出来,如下所示(以Confeti为例):
new ParticleSystem(this, 80, R.drawable.confeti2, 10000)
.setSpeedModuleAndAngleRange(0f, 0.3f, 180, 180)
.setRotationSpeed(144)
.setAcceleration(0.00005f, 90)
.emit(findViewById(R.id.emiter_top_right), 8);
new ParticleSystem(this, 80, R.drawable.confeti3, 10000)
.setSpeedModuleAndAngleRange(0f, 0.3f, 0, 0)
.setRotationSpeed(144)
.setAcceleration(0.00005f, 90)
.emit(findViewById(R.id.emiter_top_left), 8);
功能丰富
Leonids 提供了一系列方法来满足各种需求,包括构造函数、配置方法和发射方法。例如,你可以自定义粒子的速度、旋转、缩放和加速,还可以实现复杂的淡出动画。
粒子系统的灵活性允许你在任何UI元素周围创建独特的效果,无论是简单的庆祝效果还是复杂的游戏场景, Leonids 都能轻松胜任。
现在,是时候让您的应用跃动起来,尝试一下Leonids带给你的无限可能吧!