2019独角兽企业重金招聘Python工程师标准>>>

看了ApiDemo里面的BoucingBall,觉得挺好的,所以特地学习了一下,将代码注释后贴到这里,以便以后学习。

class BallView extends View implements ValueAnimator.AnimatorUpdateListener{private static final int RED = 0xffFF8080;private static final int BLUE = 0xff8080FF;private static final int CYAN = 0xff80ffff;private static final int GREEN = 0xff80ff80;private ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();public BallView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public BallView(Context context) {this(context, null);}public BallView(Context context, AttributeSet attrs) {super(context, attrs);//自定义View的背景切换动画ValueAnimator colorAnim = ObjectAnimator.ofInt(this,"backgroundColor", RED, BLUE,CYAN,GREEN);colorAnim.setDuration(3000L);colorAnim.setEvaluator(new ArgbEvaluator());colorAnim.setRepeatCount(ValueAnimator.INFINITE);colorAnim.setRepeatMode(ValueAnimator.REVERSE);colorAnim.start();}@Overrideprotected void onDraw(Canvas canvas) {//在画布上绘制图形for(ShapeHolder holder:balls){canvas.save();canvas.translate(holder.getX(), holder.getY());holder.getmShapeDrawable().draw(canvas);canvas.restore();}}@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getAction() != MotionEvent.ACTION_DOWN&& event.getAction() != MotionEvent.ACTION_MOVE) {return false;}ShapeHolder newBall = addBall(event.getX(), event.getY());float startY = newBall.getY();float endY = getHeight() - 50f;float h = getHeight();float eventy = event.getY();int duration = (int) (500 * ((h - eventy) / h));//设置加速掉落的效果ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y",startY, endY);bounceAnim.setDuration(duration);bounceAnim.setInterpolator(new AccelerateInterpolator());//当掉落到底部的时候,球体压扁,高度降低ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall, "x",newBall.getX(), newBall.getX() - 25);squashAnim1.setDuration(duration /4);squashAnim1.setRepeatCount(1);squashAnim1.setRepeatMode(ValueAnimator.REVERSE);squashAnim1.setInterpolator(new DecelerateInterpolator());ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall,"width", newBall.getWidth(), newBall.getWidth() + 50);squashAnim2.setDuration(duration /4);squashAnim2.setRepeatCount(1);squashAnim2.setRepeatMode(ValueAnimator.REVERSE);squashAnim2.setInterpolator(new DecelerateInterpolator());ValueAnimator strechAnim1 = ObjectAnimator.ofFloat(newBall, "y",endY, endY + 25);strechAnim1.setDuration(duration /4);strechAnim1.setRepeatCount(1);strechAnim1.setRepeatMode(ValueAnimator.REVERSE);strechAnim1.setInterpolator(new DecelerateInterpolator());ValueAnimator strechAnim2 = ObjectAnimator.ofFloat(newBall,"height",newBall.getHeight(), newBall.getHeight() - 25);strechAnim2.setDuration(duration /4);strechAnim2.setRepeatCount(1);strechAnim2.setRepeatMode(ValueAnimator.REVERSE);strechAnim2.setInterpolator(new DecelerateInterpolator());ValueAnimator bounceBack = ObjectAnimator.ofFloat(newBall, "y",endY, startY+25);bounceBack.setDuration(duration);bounceAnim.setInterpolator(new DecelerateInterpolator());AnimatorSet set = new AnimatorSet();set.play(bounceAnim).before(squashAnim1);set.play(squashAnim1).with(squashAnim2);set.play(squashAnim1).with(strechAnim1);set.play(squashAnim1).with(strechAnim2);set.play(bounceBack).after(strechAnim2);//逐渐消失ValueAnimator fadeAnimator = ObjectAnimator.ofFloat(newBall,"alpha", 1F, 0F);fadeAnimator.setDuration(600L);fadeAnimator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {// TODO Auto-generated method stubballs.remove(((ObjectAnimator) animation).getTarget());}});AnimatorSet animationSet = new AnimatorSet();animationSet.play(set).before(fadeAnimator);animationSet.start();return true;}/*** 生成一个球* @param x* @param y* @return*/public ShapeHolder addBall(float x, float y) {// 构造一个圆形的图案OvalShape oval = new OvalShape();oval.resize(50f, 50f);ShapeDrawable draw = new ShapeDrawable(oval);ShapeHolder holder = new ShapeHolder(draw);holder.setX(x - 25);holder.setY(y - 25);int red = (int) (Math.random() * 255);int green = (int) (Math.random() * 255);int blue = (int) (Math.random() * 255);int color = 0xff000000 | red << 16 | green << 8 | blue;int darkColor = 0xff000000 | red / 4 << 16 | green / 4 << 8 | blue/ 4;Paint paint = draw.getPaint();RadialGradient gradient = new RadialGradient(12.5f, 12.5f, 50f,color, darkColor, Shader.TileMode.CLAMP);paint.setShader(gradient);holder.setmRadialGradient(gradient);holder.setmPaint(paint);balls.add(holder);return holder;}@Overridepublic void onAnimationUpdate(ValueAnimator animation) {// TODO Auto-generated method stubinvalidate();}
}

转载于:https://my.oschina.net/fengcunhan/blog/52763

Android 自定义View以及ValueAnimator学习相关推荐

  1. android自定义View学习(一)----创建一个视图类

    创建一个视图类 精心设计的自定义视图与其他精心设计的类非常相似.它使用易于使用的界面封装了一组特定的功能,它可以高效地使用CPU和内存,等等.不过,作为一个设计良好的班级,自定义视图应该: 符合And ...

  2. 自定义圆形倒计时Android,Android自定义View倒计时圆

    本文实例为大家分享了Android自定义View倒计时圆的具体代码,供大家参考,具体内容如下 创建attr 创建DisplayUtil 类 import android.content.Context ...

  3. android 自定义view 动画效果,Android自定义view实现阻尼效果的加载动画

    效果: 需要知识: 1. 二次贝塞尔曲线 2. 动画知识 3. 基础自定义view知识 先来解释下什么叫阻尼运动 阻尼振动是指,由于振动系统受到摩擦和介质阻力或其他能耗而使振幅随时间逐渐衰减的振动,又 ...

  4. Android自定义View之画圆环(手把手教你如何一步步画圆环)

    关于自定义View: 好了,吐槽时间到.自定义view是Android开发知识体系中的重点,也是难点.好多小伙伴(也包括我)之前对自定义view也是似懂非懂.那种感觉老难受了.因此作为社会主义好青年, ...

  5. android+直播点赞,Android自定义View实现直播点赞特效的方法

    Android自定义View实现直播点赞特效的方法 发布时间:2020-07-30 09:24:13 来源:亿速云 阅读:77 作者:小猪 这篇文章主要讲解了Android自定义View实现直播点赞特 ...

  6. oracle number型步数,Android自定义View仿QQ计步器

    自定义计步器 Android自定义View是Android开发中比较重要的一项,也是很多开发者比较怕的一个东西.其实只要认真去学习,自定义View其实没有那么可怕:相反的,我们还能从自定义View中找 ...

  7. android自定义view圆环,Android自定义View实现圆环进度条

    本文实例为大家分享了android自定义view实现圆环进度条的具体代码,供大家参考,具体内容如下 效果展示 动画效果 view实现 1.底层圆环是灰色背景 2.上层圆环是红色背景 3.使用动画画一条 ...

  8. android功能相同的view,Android自定义View实现扫描效果

    本文实例为大家分享了Android自定义View实现扫描效果的具体代码,供大家参考,具体内容如下 演示效果如下: 实现内容: 1.控制动画是竖向或者横向 2.控制动画初始是从底部/左边开始,或者从上边 ...

  9. android自定义view获取控件,android 自定义控件View在Activity中使用findByViewId得到结果为null...

    转载:http://blog.csdn.net/xiabing082/article/details/48781489 1.  大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...

最新文章

  1. IP Header包
  2. 面板没了_LCD面板疯狂涨价 白菜价的55寸4K电视没了
  3. Java通过Class的对象来获取泛型的class示例
  4. 21天Jmeter打卡Day13 逻辑控制器之if和简单控制
  5. js实现简单的图片上传
  6. LaTeX常用的希腊字符、数学符号、矩阵、公式、排版、中括号、大括号以及插入图片等操作手册
  7. 智能家居弱电布线设计注意事项
  8. ubuntu14.04 下基于Nginx搭建mp4/flv流媒体服务器
  9. 小米note3的开发者选项在哪里?怎么进入开发者模式?如何显示布局边界?
  10. 移动硬盘读不出来的问题
  11. C# winform实现百度地图导航
  12. 2022-2028年全球与中国防水翘板开关行业产销需求与投资预测分析
  13. 武汉大学计算机学院 优秀夏令营,武汉大学计算机学院2014年优秀大学生暑期夏令营通知...
  14. Python获取全部股票代码信息(A/B/H/美/英股)
  15. Mysql中事务及事务隔离级别详解
  16. CSDN高校俱乐部申请流程和要求
  17. 2021最新版Dubbo入门教程
  18. Python借助爬虫Requests+BS4爬取人民教育出版社义务教育下学期课本(一)
  19. 使用拉易网制作图文并茂的精美HTML邮件模板(终)
  20. KCon 2022 演讲 PPT 新鲜出炉!

热门文章

  1. screen命令使用说明
  2. valgrind工具使用详解
  3. __attribute__ 详解
  4. 上班族如何当老板 五大模式任你选
  5. 开源代码的使用 二次开发
  6. 我身边的手机应用开发者
  7. x264函数调用关系图
  8. GCC for Win32开发环境介绍
  9. Docker logs 查看实时日志(日志最后的N行、某刻后日志)
  10. 修改jquery文件上传插件uploadify的英文为中文