Andorid酷炫钱包掉落动画

最近有点穷,只好写个酷炫的钱包掉落动画意淫一下。先贴效果,画饼充饥。

没有背景音乐我忍了是不是,还放不了gif动图只好截了个画面(http://www.github.com/JangGwa/GoldDrop)

《Andorid酷炫钱包掉落动画》 Paste_Image.png

项目里还有金币掉落的声音,酷炫的一匹。声音文件保存在/res/raw中,可以自己替换。

1.钱包的位置,大小,旋转,速度

DisplayMetrics metrics = DvAppUtil.getDisplayMetrics(Context);
if (metrics.widthPixels >= 1080) {    
flake.width = (int) (5 + (float) Math.random() * 80);    
float hwRatio = originalBitmap.getHeight() / originalBitmap.getWidth();    
flake.height = (int) (flake.width * hwRatio + 60);
} else {    
flake.width = (int) (5 + (float) Math.random() * 50); 
float hwRatio = originalBitmap.getHeight() / originalBitmap.getWidth();    
flake.height = (int) (flake.width * hwRatio + 40);
}
flake.x = (float) Math.random() * (xRange - flake.width);
flake.y = 0 - (flake.height + (float) Math.random() * flake.height);
flake.speed = 50 + (float) Math.random() * 150;
flake.rotation = (float) Math.random() * 180 - 90;
flake.rotationSpeed = (float) Math.random() * 90 - 45;

2.绘制

protected void onDraw(Canvas canvas) {    
super.onDraw(canvas);
for (int i = 0; i < numFlakes; ++i) {    
Flake flake = flakes.get(i);    
m.setTranslate(-flake.width / 2, -flake.height / 2);    
m.postRotate(flake.rotation);    
m.postTranslate(flake.width / 2 + flake.x, flake.height / 2 + flake.y);    
canvas.drawBitmap(flake.bitmap, m, null);
}
++frames;
long nowTime = System.currentTimeMillis();
long deltaTime = nowTime - startTime;
if (deltaTime > 1000) {    
float secs = (float) deltaTime / 1000f;    
fps = (float) frames / secs;    
startTime = nowTime;    
frames = 0;
  }
}

3.使用

// 将flakeView 添加到布局中
container.addView(flakeView);
// 设置背景
this.getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
// 设置同时出现在屏幕上的数量 建议64以内 过多会引起卡顿
flakeView.addFlakes(38);

附录

源码上传在GitHub上,欢迎大家一起学习

    原文作者:ingot_金果
    原文地址: https://www.jianshu.com/p/5722da90db5f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞