




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android 的漂浮动画,下雪动画效果先看下效果:1.先得了解下canvas.drawBitmap(mBitmap, mSrcRect, mDestRect, mBitPaint);在绘制图片时,使用,参数分别,图片bitmap,绘制bitmap自己的区域,绘制bitmap在手机上显示位置区域,画笔;mSrcRect,mDestRect都是Rect(int left, int top, int right, int bottom) 的对象;2.思路a.漂浮的图片,可能是小球,星星,雪花之类的,根据需求,选若干个不同小图片,先称之他们漂浮的星星;b.根据效果,漂浮图片设置其实有关数据,像坐标,大小,透明度,移动速度进水,移动方向等;c.然后初始化以上数据,生成批量小球,进行绘制;d.开设个线程或handle造成定时器,来不断刷新,同时修改漂浮星星属性,3.代码a.定义一个继承View的类,初始化画笔,所需若干星星,设置不同速度java view plain copy 在CODE上查看代码片派生到我的代码片private void initPaint() paint = new Paint(Paint.ANTI_ALIAS_FLAG); / 防抖动 paint.setDither(true); / 开启图像过滤 paint.setFilterBitmap(true); java view plain copy 在CODE上查看代码片派生到我的代码片/* * 设置动画目标,三张大小不同,样式不一,为了美观 * init bitmap info */ private void initBitmapInfo() mStarOne = (BitmapDrawable) mResources.getDrawable(R.drawable.star2).getBitmap(); mStarOneWidth = mStarOne.getWidth(); mStarOneHeight = mStarOne.getHeight(); mStarTwo = (BitmapDrawable) mResources.getDrawable(R.drawable.star1).getBitmap(); mStarTwoWidth = mStarTwo.getWidth(); mStarTwoHeight = mStarTwo.getHeight(); mStarThree = (BitmapDrawable) mResources.getDrawable(R.drawable.star3).getBitmap(); mStarThreeWidth = mStarThree.getWidth(); mStarThreeHeight = mStarThree.getHeight(); java view plain copy 在CODE上查看代码片派生到我的代码片/定义三种不同快慢的漂浮速度 private void initData(Context context) mResources = getResources(); DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics(); mTotalWidth = dm.widthPixels; mTotalHeight = dm.heightPixels; Log.i(TAG, mTotalWidth= + mTotalWidth + -1-mTotalHeight= + mTotalHeight); /设置三个不同大小的速度值 mFloatTransLowSpeed = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.5f, mResources.getDisplayMetrics(); mFloatTransMidSpeed = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.75f, mResources.getDisplayMetrics(); mFloatTransFastSpeed = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, mResources.getDisplayMetrics(); b.初始化星星,因为在构造方法里把一些基本数据初始化结束后,接着会进行测量,我把初始化星星方法放在onMeasure方法了java view plain copy 在CODE上查看代码片派生到我的代码片/* * 初始化星星信息 */ private void initStarInfo() StarInfo starInfo = null; Random random = new Random(); for (int i = 0; i mFloatCount; i+) / 获取星星大小比例 float starSize = getStarSize(0.4f, 0.8f); /小球的坐标 float starLocation = STAR_LOCATIONi; starInfo = new StarInfo(); / 初始化星星大小 starInfo.sizePercent = starSize; / 初始化漂浮速度 int randomSpeed = random.nextInt(3); switch (randomSpeed) case 0: starInfo.speed = mFloatTransLowSpeed; break; case 1: starInfo.speed = mFloatTransMidSpeed; break; case 2: starInfo.speed = mFloatTransFastSpeed; break; default: starInfo.speed = mFloatTransMidSpeed; break; / 初始化星星透明度 starInfo.alpha = getStarSize(0.3f, 0.8f); / 初始化星星位置 starInfo.xLocation = (int) (starLocation0 * mTotalWidth); starInfo.yLocation = (int) (starLocation1 * mTotalHeight); Log.i(TAG, xLocation = + starInfo.xLocation + -yLocation = + starInfo.yLocation); Log.i(TAG, stoneSize = + starSize + -stoneAlpha = + starInfo.alpha); / 初始化星星位置 starInfo.direction = getStarDirection(); mStarInfos.add(starInfo); STAR_LOCATION数组的人为的确定星星占手机屏幕大小比例的位置,自己试过随机生成一些数据,但是有时就扎堆了,应该找个手机屏幕上随机不重复生成点坐标的算法,正在思考,有会的,给我说下,学习下c.设置星星的移动方向,这里只是常态化的左右上下,对角线的方向,自己可以添加其他轨迹方向java view plain copy 在CODE上查看代码片派生到我的代码片/* * 不同移动轨迹,除过左右上下,也可以定义其他方向,如对角线,曲线之类的 * 初始化星星运行方向 */ private int getStarDirection() int randomInt; Random random = new Random(); if(floatTyep=100) randomInt = random.nextInt(3); else randomInt=floatTyep; int direction = 0; switch (randomInt) case 0: direction = LEFT; break; case 1: direction = RIGHT; break; case 2: direction = TOP; break; case 3: direction = BOTTOM; break; case 4: direction = FREE_POINT; break; default: break; return direction; d.重复绘制时,修改小球的运动轨迹方向,添加case类型,比如一些正余弦轨迹,在手机上菱形运行,折线轨迹等;java view plain copy 在CODE上查看代码片派生到我的代码片private void resetStarFloat(StarInfo starInfo) switch (starInfo.direction) case LEFT: if (starInfo.xLocation mTotalWidth+20) starInfo.xLocation = 0; else starInfo.xLocation += starInfo.speed; break; case TOP: if (starInfo.yLocation mTotalHeight+30) starInfo.yLocation = 0; else starInfo.yLocation += starInfo.speed; break; case FREE_POINT: if (starInfo.yLocation mTotalHeight+30) starInfo.yLocation = 0; else starInfo.yLocation += starInfo.speed; if (starInfo.xLocation -20) starInfo.xLocation = mTotalWidth; else starInfo.xLocation -= starInfo.speed; break; default: break; 上面的20,30是随便加的,是为了让星星跑到手机屏幕之外,再重新进入界面,否则直接运动到屏幕边界,重新开始,会闪的一下,效果不好;e.进行绘制java view plain copy 在CODE上查看代码片派生到我的代码片Override protected void onDraw(Canvas canvas) super.onDraw(anvas); for (int i = 0; i mStarInfos.size(); i+) StarInfo starInfo = mStarInfos.get(i); drawStarDynamic(i, starInfo, canvas, paint); private void drawStarDynamic(int count, StarInfo starInfo, Canvas canvas, Paint paint) resetStarFloat(starInfo); float starAlpha = starInfo.alpha; int xLocation = starInfo.xLocation; int yLocation = starInfo.yLocation; float sizePercent = starInfo.sizePercent; xLocation = (int) (xLocation / sizePercent); yLocation = (int) (yLocation / sizePercent); Bitmap bitmap = null; Rect srcRect = null; Rect destRect = new Rect(); mStarOneSrcRect = new Rect(0, 0, mStarOneWidth, mStarOneHeight); if (count % 3 = 0) bitmap = mStarOne; srcRect = mStarOneSrcRect; destRect.set(xLocation, yLocation, xLocation + mStarOneWidth, yLocation + mStarOneHeight); else if (count % 2 = 0) bitmap = mStarThree; srcRect = mStarThreeSrcRect; destRect.set(xLocation, yLocation, xLocation + mStarThreeWidth, yLocation + mStarThreeHeight); else bitmap = mStarTwo; srcRect = mStarTwoSrcRect; destRect.set(xLocation, yLocation, xLocation + mStarTwoWidth, yLocation + mStarTwoHeight); paint.setAlpha(int) (starAlpha * 255); canvas.save(); canvas.scale(sizePercent, sizePercent); canvas.drawBitmap(bitmap, srcRect, destRect, paint); canvas.restore(); f.定时重会,实现动的效果java view plain copy 在CODE上查看代码片派生到我的代码片Handler handler=new Handler() Override public void handleMessage(Message msg) super.handleMessage(msg); if(isRuning) postInvalidate(); handler.sendMessageDelayed(Message.obtain(),50); ; java view plain copy 在CODE上查看代码片派生到我的代码片public void startAnimationFloat() isRuning=ue; handler.sendMessage(Message.obtain(); public void stopAnimationFloat() isRuning=false; public void restartAnimationFloat() startAnimationFloat(); 基本就这些,然后在activity布局里使用FloatView,设置不同运动方向轨迹即可;java view plain cop
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025福建泉州交发集团(第二批)校园招聘26人考试参考题库及答案解析
- 电气工程施工技术方案与标准
- 腰痛中医特色优势病种诊疗方案及案例
- 幼儿生活自理能力提升教学方案
- 2025广西南宁住房公积金管理中心招聘外聘人员(第三场)1人考试参考题库及答案解析
- 2025年河北衡水市第十二中学教师招聘考试参考题库及答案解析
- 2025云南西双版纳州面向应届医学专业高校毕业生招聘大学生乡村医生4人考试参考题库及答案解析
- 幼儿园护理老师考试题库及答案解析
- 2025河北保定市涞源县招聘社区工作者32人考试参考题库及答案解析
- 三年级数学分数教学设计参考方案
- 麻精药品管理培训
- 顾客特殊要求培训课件
- 九年级英语宾语从句专项训练题及答案
- 医疗仪器设备效益考核办法
- 生产产能提升激励方案
- 车间5S管理培训
- ICU糖尿病酮症酸中毒护理
- 公司绿色可持续发展规划报告
- 高速铁路桥隧养护维修 课件 2 桥隧养护维修工作的基本方法和基本内容
- 战略规划六步法
- 2024年废旧溴化锂出售合同范本
评论
0/150
提交评论