炫酷动画效果
来源:互联网 发布:手机淘宝注册步骤 编辑:程序博客网 时间:2024/04/28 03:24
先上下最终实现的效果图:
制作的gif效果有点闪哈,真机上效果要好一点的
接下来进入正题:
1.绘制波浪
主要利用斐波那契函数绘制二条曲线,然后不停地偏移一些距离实现波浪移动的效果
以下代码是直接使用某位大神的代码进行的修改
protected void onDraw(Canvas canvas) { canvas.setDrawFilter(mDrawFilter); mAbovePath.reset(); mBelowWavePath.reset(); φ -= 0.1f; float y, y2; ω = 2 * Math.PI / getWidth(); mAbovePath.moveTo(getLeft(), getBottom()); mBelowWavePath.moveTo(getLeft(), getBottom()); for (float x = 0; x <= getWidth(); x += 20) { /** * y=Asin(ωx+φ)+k * A—振幅越大,波形在y轴上最大与最小值的差值越大 * ω—角速度, 控制正弦周期(单位角度内震动的次数) * φ—初相,反映在坐标系上则为图像的左右移动。这里通过不断改变φ,达到波浪移动效果 * k—偏距,反映在坐标系上则为图像的上移或下移。 */ y = (float) (height / 8 * Math.cos(ω * x + φ) + height / 8 * 3 + height / 2); y2 = (float) (height / 8 * Math.sin(ω * x + φ) + height / 2); mAbovePath.lineTo(x, y); mBelowWavePath.lineTo(x, y2); //回调 把y坐标的值传出去(在activity里面接收让小机器人随波浪一起摇摆) mWaveAnimationListener.OnWaveAnimation(y); } for(int i=0;i<boards.size();i++){ boards.get(i).drawBoard(canvas); } mAbovePath.lineTo(getRight(), getBottom()); mBelowWavePath.lineTo(getRight(), getBottom()); canvas.drawPath(mAbovePath, mAboveWavePaint); canvas.drawPath(mBelowWavePath, mBelowWavePaint); postInvalidateDelayed(20); }
2.绘制小船
新建一个小船类,根据x,y坐标在上面绘制一个bitmap,然后每次随机的向左偏移一段距离,实现移动。
public class Board { int width;Context context;int type; int left; public Board(int width,Context context, int type) { this.width = width; this.left=(int)(r.nextInt(100)*width/100); this.context = context; this.type = type; } Random r=new Random(); public void drawBoard(Canvas canvas) { Paint paint = new Paint(); int resource=R.mipmap.board1; switch (type){ case 0: resource=R.mipmap.board1; break; case 1: resource=R.mipmap.board2; break; case 2: resource=R.mipmap.board3; break; case 3: resource=R.mipmap.board4; break; case 4: resource=R.mipmap.board5; break; case 5: resource=R.mipmap.board6; break; } Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resource); canvas.drawBitmap(bitmap, left, WaveView.getHeight(left)-bitmap.getHeight()/6*5, paint); left-=r.nextInt(3)*width/1000; if(left<=-bitmap.getWidth()){ left=width; } }}
在WaveView中提供根据X坐标获取Y坐标的方法
public static float getHeight(int left) { return (float) (height / 8 * Math.cos(ω * left + φ) + height / 8 * 3 + height / 2); }
最后再WaveView的OnDraw方法中同时绘制所有的小船
for(int i=0;i<boards.size();i++){ boards.get(i).drawBoard(canvas); }
3.绘制小鱼
小鱼绘制比较复杂,是直接照搬国外大牛做的,我稍微修改了下,给鱼随机产生颜色和移动速度
fishDrawable = new FishDrawable(context, random.nextInt(255), random.nextInt(255), random.nextInt(255));
然后在布局中添加10条
RelativeLayout fishpool= (RelativeLayout) findViewById(R.id.fishpool); int i=0; do{ fishpool.addView(new FishDrawableView(this)); i++; }while (i<10);
到此,就完工了!
感兴趣的可以在下方链接进行下载,感觉还可以,帮忙给个star吧!
Github传送门
阅读全文
0 0
- 炫酷动画效果
- 炫酷动画效果
- ViewPager+属性动画 实现炫酷视差动画效果
- Android酷炫动画效果之进度加载动画
- Android实现炫酷SVG动画效果
- Android实现炫酷SVG动画效果
- Android实现炫酷SVG动画效果
- Android实现炫酷SVG动画效果
- 安卓动画炫酷效果项目
- 超酷HTML5 动画效果
- iOS开发之iOS中的动画实现,通过CAShapeLayer、CAShapeLayer,CAReplicatorLayer实现炫酷的动画、雷达效果,波纹效果,咻一咻效果。iOS核心动画实现。
- 一些收集的iOS酷炫动画效果
- android viewpager tab indicator炫酷的Tab动画效果
- 创建酷炫动画效果的10个JavaScript库
- 第二章 创建炫酷的动画效果
- CSS3 transform介绍|如何设计炫酷的动画效果
- CSS3 transition介绍 | 如何设计炫酷的动画效果?
- jQuery/CSS3炫酷动画效果插件 animate
- kafka问题总结
- 基于FPGA的串口通讯设计
- j2ee的web项目,调试,一定要使用Chrome谷歌浏览器的f12的console进行查看,特别是针对js的错误,没有它暴露不出来的错误
- C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用
- 超时重试思考-非幂等请求
- 炫酷动画效果
- Keil MDK不支持结构体对指定元素初始化
- 关于不结束进程强删文件一点点看法
- Django 数据分页处理
- vue中props传值
- 物流链 WMS仓库条码管理系统提高效率
- [BIT0429]-Java多态:父类引用指向子类对象的原理
- Java-变量与常量
- java.util.Random.nextBoolean()方法实例