当前位置: 首页 > 编程日记 > 正文

Animation 模拟纸盒的爆破

用简单的Animation动作模拟爆破的瞬间,仔细的调整各种参数效果会更像

原理:用定义好的纸张从onTouch中心点向四面八方散开,散开过程中,使用不用的速度、大小、方向、旋转角度、透明度(这里纸张可以加上火焰,效果会更好,纸张燃烧代表透明度)、位置、3D旋转  ;

//3D翻转 ,模拟力量方向俯冲
Rotate3d rota3d = new Rotate3d((int) (Math.random()*90), (float) (Math.random()*90),
(float) (Math.random()*x), (int) (Math.random()*y), 310.0f, true);

//3D翻转
//Rotate3d rota3d = new Rotate3d((int) (Math.random()*90),
//(float) (Math.random()*90,0, 0, 310.0f, true);

3D动作,float centerX, float centerY 的位置能够模拟出力量的方向,如果都为0,均从中心散出

这里虽然好象有点扯淡,不过效果还是蛮像的

  1 public class MainActivity extends Activity {
2 private FrameLayout fl;
3 //定义Bitmap对象
4 Bitmap mBit = null;
5 //AnimationSet animationSet;
6 List<ExplosionView> exvArrayList = new ArrayList<ExplosionView>();
7 List<AnimationSet> animationArrayList = new ArrayList<AnimationSet>();
8 DisplayMetrics dm;
9 private float dmw=0,dmh=0;
10 //定义碎片的形状
11 private int s1 = R.drawable.s1,s2=R.drawable.s2,s3=R.drawable.s3,s4=R.drawable.s4,
12 s5=R.drawable.s5,s6=R.drawable.s6,s7=R.drawable.s7,s8=R.drawable.s8,
13 s9=R.drawable.s9,s10=R.drawable.s10,s11=R.drawable.s11,s12=R.drawable.s12,
14 s13=R.drawable.s13,s14=R.drawable.s14;
15 private int[] chip = {s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14};
16 private int x=1;
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20
21 requestWindowFeature(Window.FEATURE_NO_TITLE);
22 getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
23 WindowManager.LayoutParams. FLAG_FULLSCREEN);
24
25 dm = new DisplayMetrics();
26 getWindowManager().getDefaultDisplay().getMetrics(dm);
27 //获得手机的宽度和高度像素单位为px
28 dmw = dm.widthPixels;
29 dmh = dm.heightPixels;
30
31 fl = new FrameLayout(this);
32
33 //添加50个爆炸碎片
34 for (int i = 0; i < 50; i++) {
35 int width=getResources().getDrawable(chip[x]).getIntrinsicWidth();
36 int height=getResources().getDrawable(chip[x]).getIntrinsicHeight();
37 fl.addView(createExv(chip[x],width,height));
38 if(x==13){
39 x=0;
40 }
41 x++;
42 }
43 fl.setOnTouchListener(new LayoutListener());
44 setContentView(fl);
45 }
46
47 public ExplosionView createExv(int chipx,int w,int h){
48 ExplosionView exv = new ExplosionView(this,w,h);
49 exv.setImageResource(chipx);
50 exv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
51 exv.setVisibility(View.INVISIBLE);
52 exvArrayList.add(exv);
53 //animationArrayList.add(animationSet);
54 return exv;
55 }
56
57 class LayoutListener implements OnTouchListener{
58
59 public boolean onTouch(View v, MotionEvent event) {
60 float x = event.getX();
61 float y = event.getY();
62
63 // Iterator it1 = exvArrayList.iterator();
64 // while(it1.hasNext()){
65 ////System.out.println(it1.next());
66 // ExplosionView tempExplosion = (ExplosionView) it1.next();
67 // tempExplosion.setVisibility(View.VISIBLE);
68 // tempExplosion.setLocation((int)y-20, (int)x-20);
69 // tempExplosion.startAnimation(animation);
70 // }
71 for(int i = 0;i < exvArrayList.size(); i ++){
72 //System.out.println(exvArrayList.get(i));
73 ExplosionView tempExplosion = (ExplosionView) exvArrayList.get(i);
74 tempExplosion.setVisibility(View.VISIBLE);
75 tempExplosion.setLocation((int)y, (int)x);
76 AnimationSet animationSet = new AnimationSet(false);
77
78 //设置碎片的透明程度变化
79 AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
80 //透明动作:减速
81 alpha.setInterpolator(new DecelerateInterpolator());
82
83
84 //设置碎片的旋转角度
85 RotateAnimation rotate = new RotateAnimation(0, (int) (Math.random()*330+30),
86 Animation.RELATIVE_TO_SELF, 0.5f,
87 Animation.RELATIVE_TO_SELF, 0.5f);
88 //旋转动作:减速
89 rotate.setInterpolator(new DecelerateInterpolator());
90
91
92 //设置碎片的移动变化
93 TranslateAnimation translate = new TranslateAnimation(
94 0,
95 (float) ( dmw/2-(Math.random()*(dmw))),
96 0,
97 (float) ( dmw/2-(Math.random()*(dmw))));
98 //移动动作:减速
99 translate.setInterpolator(new DecelerateInterpolator());
100
101 //设置碎片的大小变化
102 ScaleAnimation scaleAnimation = new ScaleAnimation(
103 1, (float)(Math.random()),
104 1, (float)(Math.random()),
105 Animation.RELATIVE_TO_SELF, 0,
106 Animation.RELATIVE_TO_SELF, 0);
107 //大小动作:加速度
108 scaleAnimation.setInterpolator(new AccelerateInterpolator());
109
110 //3D翻转 ,模拟力量方向俯冲
111 Rotate3d rota3d = new Rotate3d((int) (Math.random()*90), (float) (Math.random()*90),
112 (float) (Math.random()*x), (int) (Math.random()*y), 310.0f, true);
113 //Rotate3d rota3d = new Rotate3d((int) (Math.random()*90), (float) (Math.random()*90),
114 // 0, 0, 310.0f, true);
115
116
117 animationSet.addAnimation(alpha);
118 animationSet.addAnimation(rotate);
119 animationSet.addAnimation(translate);
120 animationSet.addAnimation(rota3d);
121 animationSet.addAnimation(scaleAnimation);
122
123 //动画持续时间,使每个碎片的动作速度都不一样
124 //animationSet.setDuration(5000);
125 animationSet.setDuration((int) (Math.random()*4000+100));
126 //开始停顿时间
127 animationSet.setStartOffset(0);
128 animationSet.setFillAfter(true);
129
130 tempExplosion.startAnimation(animationSet);
131 }
132
133 return false;
134 }
135 }
136
137 class ExplosionView extends ImageView{
138 private int exvw,exvh;
139 public ExplosionView(Context context,int ew,int eh) {
140 super(context);
141 this.exvw=ew;
142 this.exvh=eh;
143 }
144 //handle the location of the explosion
145 public void setLocation(int top,int left){
146 this.setFrame(left, top, left+exvw, top+exvh);
147 }
148 }
149
150 class Rotate3d extends Animation{
151 private final float mFromDegrees;
152 private final float mToDegrees;
153 private final float mCenterX;
154 private final float mCenterY;
155 private final float mDepthZ;
156 private final boolean mReverse;
157 private Camera mCamera;
158
159 public Rotate3d(float fromDegrees, float toDegrees,float centerX, float centerY,
160 float depthZ, boolean reverse) {
161 mFromDegrees = fromDegrees;
162 mToDegrees = toDegrees;
163 mCenterX = centerX;
164 mCenterY = centerY;
165 mDepthZ = depthZ;
166 mReverse = reverse;
167 }
168
169 @Override
170 public void initialize(int width, int height, int parentWidth, int parentHeight) {
171 super.initialize(width, height, parentWidth, parentHeight);
172 mCamera = new Camera();
173 }
174
175 @Override
176 protected void applyTransformation(float interpolatedTime, Transformation t) {
177 final float fromDegrees = mFromDegrees;
178 float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
179 final float centerX = mCenterX;
180 final float centerY = mCenterY;
181 final Camera camera = mCamera;
182 final Matrix matrix = t.getMatrix();
183 camera.save();
184 if (mReverse) {
185 camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
186 } else {
187 camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
188 }
189
190 camera.rotateY(degrees);
191 camera.getMatrix(matrix);
192 camera.restore();
193 matrix.preTranslate(-centerX, -centerY);
194 matrix.postTranslate(centerX, centerY);
195 }
196 }

这里没结合到物理学,如果要做得更仔细,可以加有手机的重力感应和撞击屏幕边缘的反弹效果

转载于:https://www.cnblogs.com/-run/archive/2012/02/07/2341075.html

相关文章:

代码恒久远,GitHub 永流传

作者 | 唐小引题图 | GitHub来源 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;这两天&#xff0c;在 GitHub 上积极贡献代码的许多开发者都收到了「Arctic Code Vault Contributor」的荣誉勋章的通知&#xff0c;并非常兴奋地晒起了朋友圈。因为这标志着自己在 GitHu…

不允许后退的方法

由于项目的需要不允许系统在提交之后&#xff0c;按IE的后退按钮进行再次提交。试试了一下在 .NET中通过如下语句 Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);就可以使页面的缓存失效&#xff0c;每次都需要获取新页面。 <script>history.for…

3个题目熟悉类和对象基础

1、按要求编写Java应用程序&#xff1a; &#xff08;1&#xff09;编写西游记人物类&#xff08;XiYouJiRenWu&#xff09;其中属性有&#xff1a;身高&#xff08;height&#xff09;&#xff0c;名字&#xff08;name&#xff09;&#xff0c;武器&#xff08;weapon&#x…

按下回车表示确定提交

<body οnkeydοwn"if (event.keyCode13) {document.all.button2.click()}"> 下面的五种方法都可以帮你解决这种问题1.<script languagejavascript>function document.onkeydown(){ if (event.keyCode 13) { if (event.srcElement docum…

A股暴跌,户均亏2万!刚写好的辞职信又撕了……

仅用1天&#xff0c;A股市值单日蒸发达到3.5万亿&#xff0c;人均亏了超2万&#xff01;“芯片龙头”企业中芯国际正式登陆科创板&#xff0c;使得半导体板块整体跌幅较小。中芯国际上市首日涨幅超200%&#xff0c;收报82.92元&#xff0c;总市值达6137.57亿元&#xff0c;成为…

DataList在无数据记录时显示类似GridView空模板(EmptyDataTemplate)

在FooterTemplate加个Label并根据repeater.Items.Count判断是否有记录。HTML代码&#xff1a; <FooterTemplate> <asp:Label ID"lblEmpty" Text"No data recprd exist !" runat"server" Visible<%#bool.Parse((DataList1.I…

QButtonGroup 的使用

1、3以后尽量手写&#xff0c;因为没有现在的控件了 2、 1 // lyy : 2016/8/26 12:17:41 说明:存放radioButton2 QButtonGroup *buttonGroup;3 // lyy : 2016/8/26 11:11:55 说明:radioButton4 buttonGroup new QButtonGroup();5 buttonGroup->addButt…

机器学习算法易受攻击?阿里“安全基建”这样应对AI的不安全

出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;数字基建的浪潮之下&#xff0c;诸多行业领域都在加速融合5G、大数据中心、AI等新技术&#xff0c;向数字化转型。近日&#xff0c;多位全国政协委员、院士和安全行业专家提出&#xff0c;应尽快出台安全基建国家标准&…

不可以输入中文

function checkCH() {var strtxtPwd.value;var c /[/u4e00-/u9fa5]/;if(c.test(str)){alert("不能输入中文。");return false;}return true; }

tomcat虚拟路径的几种配置方法

tomcat配置虚拟路径的4种方法在tomcat中&#xff0c;webapp文件夹默认为web应用的根目录。1.在tomcat\conf\server.xml中找到 host标签&#xff0c;在host标签中增加新的元素<Context/>并设置 Context的元素&#xff0c;其中path元素所设置的值就是在浏览器中所要访问的虚…

hi35183e增加exfat文件系统的支持

64G-128G的tf卡文件系统格式为exfat&#xff0c;而hi3518e默认只支持fat32格式的tf卡。为了挂载64G以上的tf卡&#xff0c;只能将sd卡先格式化成FAT32。鉴于exfat性能比FAT32强&#xff0c;因此考虑移植exfat驱动到海思3518e平台,这样就不用强制格式化tf卡。 拷贝驱动源码到内核…

XML的简单读取与写入

作者&#xff1a;网际浪子专栏&#xff08;曾用名littlehb&#xff09; http://blog.csdn.net/littlehb/我用的是一种很笨的方法&#xff0c;但可以帮助初学者了解访问XML节点的过程。已知有一个XML文件&#xff08;bookstore.xml&#xff09;如下&#xff1a; <?xml ver…

美国 AI 博士一针见血:Python 这样学最容易成为高手!

我见过市面上很多的 Python 讲解教程和书籍&#xff0c;他们大都这样讲 Python 的&#xff1a;先从 Python 的发展历史开始&#xff0c;介绍 Python 的基本语法规则&#xff0c;Python 的 list, dict, tuple 等数据结构&#xff0c;然后再介绍字符串处理和正则表达式&#xff0…

区域链实践第一步——区域链测试环境搭建

区域链光速发展&#xff0c;在许多人的期许下&#xff0c;已经成为了互联网下一个革新点。区块链会成就的未来价值互联网&#xff0c;是新时代的基石。 IBM中国研究院开发的超能云&#xff08;SuperVessel&#xff09;平台提供了给区块链爱好者、开发者的区块链开发测试环境。通…

DataGrid的几个小技巧

作者&#xff1a;网际浪子专栏&#xff08;曾用名littlehb&#xff09; http://blog.csdn.net/littlehb/方法一:使用模版列 我们在绑定好数据的DataGrid增加一个模版列,在列中放置一个按钮<asp:TemplateColumn><ItemTemplate><asp:Button id"btnDelete&q…

32岁程序员面试被拒:比又穷又忙更可怕的,是2020年你还不懂...

在大学阶段&#xff0c;大家都学过概率论、线性代数和微积分的课程&#xff0c;但是为什么在面对机器学习中的数学问题时&#xff0c;却有一种天书的既视感&#xff1f;第一&#xff0c;大学课程中的知识点并没有完全覆盖机器学习领域所需。 回想一下大学概率统计课程内容的内容…

DHCP tftp PXE实现Ghost网络克隆

概述&#xff1a;加ghost参数&#xff0c;实现客户端只需连接网线&#xff0c;在无光驱、软驱的情况下&#xff0c;且无需其他配置&#xff0c;完成网络克隆。所要用到的软件&#xff1a;1、ghost 8.2 企业版中的Ghost Boot Wizard&#xff08;其中的ghost.exe程序可以用ghost1…

PHP文件上传和文件操作案例

<?php /**文件配置变量$dirname是目录名称*/ session_start(); $dirname upload; $fileClass new fileClass($dirname); $fileClass -> fileCMM();/*文件重命名操作*/ $fileClass -> fileDelete();/*文件删除操作*/ ?> <!DOCTYPE html> <html> <…

将模式对话框的返回值回送(PostBack)到服务端

作者&#xff1a;张老三的专栏 http://blog.csdn.net/billy_zh/在一些应用中&#xff0c;希望将模式对话框的值回送(PostBack)到服务端&#xff0c;也就是执行一个服务端操作&#xff0c;从而刷新页面。举个例子&#xff1a;比方说新建一个订单&#xff0c;此时希望能在模式…

android zip解压缩(含有子目录)

为什么80%的码农都做不了架构师&#xff1f;>>> note: 如果目录中含有中文名称&#xff0c; 要用substr new String(substr.getBytes("8859_1"), "GB2312");这样的语句转换&#xff0c;否则为乱码 /*** 解压缩功能.* 将ZIP_FILENAME文件解压…

用Python实现抖音上的“人像动漫化”特效,原来这么简单

作者 | 黄伟呢来源 | 数据分析与统计学之美前几天&#xff0c;女友拉着我和她玩儿抖音&#xff0c;就是这个人像动漫化的操作&#xff0c;顿时觉得很好玩儿。我心想&#xff1a;Python 既然这么强大&#xff0c;是不是也可以使用 Python 程序来实现这样一个操作呢&#xff1f;哈…

easyui在IE中: SCRIPT1003: 缺少 ':'

转载于:https://www.cnblogs.com/FredLee/p/5894614.html

NumPy学的还不错?来试试这20题

来源 | 早起Python&#xff08;ID: zaoqi-python&#xff09;又到了NumPy进阶修炼专题。NumPy大家应该不陌生了&#xff0c;看了太多的原理讲解之后&#xff0c;用刷题来学习是最有效的方法&#xff0c;本文将带来20个NumPy经典问题&#xff0c;附赠20段实用代码&#xff0c;拿…

tomcat报 Context [] startup failed due toprevious errors

今天同事在实施项目中,遇到一个问题,就是项目布到tomcat服务器上了,却总是找不到访问路径.tomcat报 Context [] startup failed due toprevious errors 当时就觉的很郁闷,项目是本机测试完整打包的.按以前的项目实施也很少出现这样的问题.在网上搜索以下解决方案: ------解决方…

DataGrid中自带的分页功能的使用

作者&#xff1a;木子 http://blog.csdn.net/derny/1、把AllowPaging属性设置为true 并设置PageSize的大小 2、在html页面中设置datagrid的属性OnPageIndexChanged"DataGrid_Page" // //DataGrid_Page 为点击页数的时候激发的事件 3、在程序中加入代码如下&…

34补1-2_3 HA Cluster基础及heartbeat实现HA

HA Cluster基础及heartbeat实现HA配置环境node1&#xff1a;192.168.1.121CentOS6.7node2&#xff1a;192.168.1.122CentOS6.7node3&#xff1a;192.168.1.123CentOS6.7vip 192.168.1.80配置前准备# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 loc…

关于SSL配置的报告

作者&#xff1a;网际浪子专栏&#xff08;曾用名littlehb&#xff09; http://blog.csdn.net/littlehb/一&#xff0c;服务器上装有CA(Certificate Server) 1&#xff0c;服务器上安装CA Win2000中带有CA的安装程序。单击Start&#xff0c;Control Pannel Add/Remove Prog…

认知智能,AI的下一个十年 | AI Procon 2020

整理 | 屠敏 出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09; 60 年间的「三起两落」 &#xff0c;人工智能的沉浮变迁。 在 1956 年的夏天&#xff0c;人工智能在美国达特茅斯大学召开的学术会议之上蹒跚学步&#xff0c;吸引无数研究学者对其智能化的探索以及未来美…

STL 队列queue

q.front() 返回队头元素 q.push(a) 将元素a入队 q.pop() 队头元素出队 q.empty() 如果队列为空 返回ture 否则返回false转载于:https://www.cnblogs.com/xujian9502/archive/2012/02/19/2358055.html

OpenCV——使用ROI进行图像切割

ROI&#xff08;region of interest&#xff09;——感兴趣区域。 1.用途 这个区域是图像分析所关注的重点。圈定这个区域&#xff0c;以便进行进一步的处理。而且&#xff0c;使用ROI指定 想读入的目标&#xff0c;可以减少处理时间&#xff0c;增加精度&#xff0c;给图像处理…