Android 自定义View以及ValueAnimator学习

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

     看了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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463846.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

百万记录级MySQL数据库及Discuz!论坛优化

最近&#xff0c;帮一个朋友优化一个拥有20万主题&#xff0c;100万帖子&#xff0c;3万多会员&#xff0c;平均在线人数2000人的Discuz!论坛&#xff0c;采用Linux2.6Apache2mod_php5MySQL5&#xff0c;服务器配置为双至强4G内存&#xff0c;优化前&#xff0c;系统平均负载&a…

固件工程师到底是干什么?

大家好&#xff0c;我是写代码的篮球球痴可能大家还不知道&#xff0c;我是一个写固件的工程师&#xff0c;今天和一个微信朋友聊天&#xff0c;他问我工作几年了&#xff0c;我说我工作10年了&#xff0c;而且这10年我都一直在从事嵌入式固件相关的工作。他猛的觉得很不可思议…

c++ STL 工程实践的15条建议

STL是c非常重要的一部分&#xff0c;它是很多大神的杰作&#xff0c;高效&#xff0c;稳定&#xff0c;可扩展性好&#xff0c;虽然STL确实存在难以调试&#xff0c;内存碎片的问题&#xff08;现在机器的内存越来越大&#xff0c;内存碎片的问题基本不太可能成为系统瓶颈&…

JS中关于构造函数、原型链、prototype、constructor、instanceof、__proto__属性

在Javascript不存在类&#xff08;Class&#xff09;的概念&#xff0c;javascript中不是基于类的&#xff0c;而是通过构造函数&#xff08;constructor&#xff09;和原型链&#xff08;prototype chains&#xff09;实现的。但是在ES6中引入了Class&#xff08;类&#xff0…

asp 下载函数

N久没搞ASP啦,今天一个网友问到这个问题.就在网上找了下. 也不知道这个有没有错误. 下载的基本思路是这个样子的. 代码如下: <%Const USE_STREAM 0 0.不用流(Adodb.Stream)下载 1.用流下载Const ALLOW_FILE_EXT "rar,zip,chm,doc,xls,swf,mp3,gif,jpg,jpeg,png,bmp&q…

如何实现按键的短按、长按检测?

在电子产品中经常用到按键&#xff0c;尤其是经常需要MCU判断短按和长按这两种动作&#xff0c;本篇我们来专门聊下这个话题。只谈理论太无聊&#xff0c;我们还是结合着实际应用来说明。之前写过一篇关于《CH573第一篇&#xff1a;实现自拍杆蓝牙遥控器1》的文章&#xff0c;例…

ECC内存简介

ECC内存&#xff0c;即应用了能够实现错误检查和纠正技术&#xff08;ECC&#xff09;的内存条。一般多应用在服务器及图形工作站上&#xff0c;这将使整个电脑系统在工作时更趋于安全稳定。ECC是“Error Checking and Correcting”的简写&#xff0c;中文名称是“错误检查和纠…

HTTP与HTTPS的安全性讨论

转载文章&#xff1a;http://blog.csdn.net/xifeijian/article/details/54667989 1、http为什么不安全&#xff1f; http协议属于明文传输协议&#xff0c;交互过程以及数据传输都没有进行加密&#xff0c;通信双方也没有进行任何认证&#xff0c;通信过程非常容易遭遇劫持、…

使用NVM管理Node - Windows

安装 NVM NVM 下载&#xff1a;https://github.com/coreybutler/nvm-windows 安装 Node 注意&#xff1a;如果没有FQ默认源可能安装npm失败&#xff0c;请参考下一节“安装 NPM”修改镜像源地址。 查看可用版本&#xff1a; nvm ls available 安装Node&#xff1a; nvm install…

博士也会毕业吗?

怪我读书少&#xff0c;一直以来我都认为博士是一种很神奇的物种&#xff0c;他们能学会很多我们学不会的东西&#xff0c;那些奇奇怪怪的数学题&#xff0c;还有那种要背上几天的思想政治课文&#xff0c;以及要听懂那种像鸟儿一样呼唤的讲课&#xff0c;所以&#xff0c;我一…

3C趋势价值对云计算发展的驱动作用

日前&#xff0c;AMD总裁邓元鋆在参加云博会时&#xff0c;向来访的媒体透露了AMD在云计算方面的相关策略及计划&#xff1b;邓元鋆表示&#xff0c;未来AMD将以用户最为关心的Consumerization&#xff08;消费导向&#xff09;、Cloud&#xff08;云计算&#xff09;、Converg…

工作笔记

数据类型向下转换&#xff0c;精度丢失&#xff0c;可能会出现错误结果&#xff1a; public class SimpleFormat {public static void main(String[] args) {int a1111111;short b(short)a;System.out.println(b);//输出&#xff1a;-3001 } } 工具类DateFormatUitls转载于:htt…

国外出差见闻之印度

前段时间到印度的马德拉斯市出差了3个月(4月到6月)&#xff0c;今天终于有时间将自己的出差心得和见闻记录下来&#xff0c;可能以后就会忘记。下午16:00时在深圳坐车到香港机场&#xff0c;上车没多久会让填一个入境香港的单子&#xff0c;然后是过海关&#xff0c;过海关时会…

雷军的演讲以及产品发布

8月11号是小米的发布会&#xff0c;还有雷军的年度演讲。因为工作冲突我没看直播&#xff0c;晚上回来看了公众号文章和知乎上的内容讨论&#xff0c;也看了发布的新产品。雷军那个年代能够做上程序员一定是非常牛逼的人&#xff0c;而雷军是这些牛逼人的公司总经理&#xff0c…

候,一片天空

考完试了,也放假了.想知道自己能做什么更想知道自己要做什么曾经多么想好好努力好好看书现在才发现是该好好努力好好看书时间过得太快了,快得让人害怕只是睡觉,只是跑来跑去只是不明白什么时候才能真正自己靠自己只是不知道怎么样才能过得充实而有意义只是不晓得如何才能守住现…

轻松理解UML用例图时序图类图的教程

摘自https://zhuanlan.zhihu.com/p/29874146 写在前面 当你老大扔给你这样的图&#xff0c;或者你需要完成某些功能而去看文档的时候发现以下类似这样的图会不会不&#xff08;一&#xff09;知&#xff08;脸&#xff09;所&#xff08;懵&#xff09;措&#xff08;逼&#x…

[Winodows Phone 7控件详解]控件拾遗

1.Panorama控件和Pivot控件前面讲过&#xff0c;没有必要再重复一遍了。参见我的博文&#xff1a;http://www.cnblogs.com/DebugLZQ/archive/2012/03/19/2406284.html 2.DeepZoom DeepZoom 是silverlight的特色功能之一&#xff0c;也同样被加到了windows phone 7中来。这个功能…

这个工具替代Notepad++,我很满意

用过notepad的人并且还一直坚持使用它的人一定觉得它是一个非常优秀的软件&#xff0c;这个软件用来看日志&#xff0c;看代码和文档非常方便&#xff0c;而且里面还集成了一个HEX分析的工具&#xff0c;当然还有列模式等等。不吹牛啊&#xff0c;很多软件只做到了功能&#xf…

c#实现Stack

1 publicclassNodes //结点类2 {3 publicNodes Next;4 publicobjectValue;5 publicNodes(objectvalue) : this(value, null) { }6 publicNodes(objectvalue, Nodes next)7 {8 Next next;9 Value value;10 }11 }1 publicclassStack2 {3 privateintcount 0;4 privateNodes first …

Kubectl 部署有状态应用(下)

接上文 《Kubectl 部署有状态应用&#xff08;上&#xff09;》创建完StatefulSet后&#xff0c;本文继续介绍StatefulSet 扩展、更新、删除等内容。 StatefulSet 中的 Pod 验证序数索引和稳定的网络身份 StatefulSet 中的 Pod 具有唯一的序数索引和稳定的网络身份。 查看 …