按照惯例先放效果图:图中小球做抛物线运动

资源图片

1.首先布局文件activity_main.xml,布局很简单,就一个测试按钮

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10
11     <Button
12         android:id="@+id/btnClick"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:onClick="click()"
16         android:text="开始动画" />
17
18 </RelativeLayout>

2.然后是java代码MainActivity.java,这个是重点

  1 package com.example.curveanim;
  2
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.view.View;
  6 import android.view.View.OnClickListener;
  7 import android.view.ViewGroup;
  8 import android.view.animation.AccelerateInterpolator;
  9 import android.view.animation.Animation;
 10 import android.view.animation.Animation.AnimationListener;
 11 import android.view.animation.AnimationSet;
 12 import android.view.animation.LinearInterpolator;
 13 import android.view.animation.TranslateAnimation;
 14 import android.widget.ImageView;
 15 import android.widget.LinearLayout;
 16
 17 public class MainActivity extends Activity {
 18     private ViewGroup anim_mask_layout;// 动画层
 19
 20     @Override
 21     protected void onCreate(Bundle savedInstanceState) {
 22         super.onCreate(savedInstanceState);
 23         setContentView(R.layout.activity_main);
 24         findViewById(R.id.btnClick).setOnClickListener(new OnClickListener() {
 25
 26             @Override
 27             public void onClick(View v) {
 28                 click(v);
 29             }
 30         });
 31     }
 32
 33     private void click(View v) {
 34         int[] startLocation = new int[2];// 一个整型数组,用来存储按钮的在屏幕的X、Y坐标
 35         v.getLocationInWindow(startLocation);// 这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)
 36         ImageView ball = new ImageView(getApplicationContext());
 37         ball.setImageResource(R.drawable.sign);// 设置ball的图片
 38         setAnim(ball, startLocation);// 开始执行动画
 39     }
 40
 41     private void setAnim(final View v, int[] startLocation) {
 42         anim_mask_layout = null;
 43         anim_mask_layout = createAnimLayout();
 44         anim_mask_layout.addView(v);// 把动画小球添加到动画层
 45         final View view = addViewToAnimLayout(anim_mask_layout, v, startLocation);
 46
 47         // 计算位移
 48
 49         TranslateAnimation translateAnimationX = new TranslateAnimation(0, 400, 0, 0);
 50         translateAnimationX.setInterpolator(new LinearInterpolator());
 51         translateAnimationX.setRepeatCount(Animation.INFINITE);// 动画重复执行的次数
 52         translateAnimationX.setFillAfter(true);
 53
 54         TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, 400);
 55         translateAnimationY.setInterpolator(new AccelerateInterpolator());
 56         translateAnimationY.setRepeatCount(Animation.INFINITE);// 动画重复执行的次数
 57         translateAnimationX.setFillAfter(true);
 58
 59         AnimationSet set = new AnimationSet(false);
 60         set.setFillAfter(false);
 61         set.addAnimation(translateAnimationY);
 62         set.addAnimation(translateAnimationX);
 63         set.setDuration(2000);// 动画的执行时间
 64         view.startAnimation(set);
 65
 66         // 动画监听事件
 67         set.setAnimationListener(new AnimationListener() {
 68             // 动画的开始
 69             @Override
 70             public void onAnimationStart(Animation animation) {
 71                 v.setVisibility(View.VISIBLE);
 72             }
 73
 74             @Override
 75             public void onAnimationRepeat(Animation animation) {
 76             }
 77
 78             // 动画的结束
 79             @Override
 80             public void onAnimationEnd(Animation animation) {
 81                 v.setVisibility(View.GONE);
 82             }
 83         });
 84
 85     }
 86
 87     /**
 88      * @Description: 创建动画层
 89      * @param
 90      * @return void
 91      * @throws
 92      */
 93     private ViewGroup createAnimLayout() {
 94         ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
 95         LinearLayout animLayout = new LinearLayout(this);
 96         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PA            RENT);
 97         animLayout.setLayoutParams(lp);
 98         animLayout.setId(Integer.MAX_VALUE);
 99         animLayout.setBackgroundResource(android.R.color.transparent);
100         rootView.addView(animLayout);
101         return animLayout;
102     }
103
104     private View addViewToAnimLayout(final ViewGroup parent, final View view, int[] location) {
105         int x = location[0];
106         int y = location[1];
107         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CON            TENT);
108         lp.leftMargin = x;
109         lp.topMargin = y;
110         view.setLayoutParams(lp);
111         return view;
112     }
113 }

云盘分享下载链接:http://yunpan.cn/cFzVRfCpLwHz8  访问密码 e37f

转载于:https://www.cnblogs.com/_ymw/p/4922921.html

Android 曲线动画animation,类似加入购物车动画相关推荐

  1. 精灵动画Animation对话框组成Idle动画的各精灵

    精灵动画Animation对话框组成Idle动画的各精灵 场景中已经添加了精灵,现在是时候让让它动起来了.读者也许已经从精灵图集中,各精灵的命名中看出来了,这个精灵一共有两种动画状态:Idle(空闲) ...

  2. html加入购物车的动画,vue实现加入购物车动画

    预备基础 整体思路 定义商品列表,设置点击的元素(点击后触发加入购物车的函数addToShopCart)注:这里要把$event传递过去,方便得到target 定义一个component用来放购物车和 ...

  3. 小程序动画Animation,高度增加动画形式,图标旋转动画形式

    <view class="serach-title serach-list"  animation="{{animatheightadd}}" > ...

  4. Unity3d动画脚本 Animation Scripting(深入了解游戏引擎中的动画处理原理--旧的动画系统)

    (来自:http://blog.sina.com.cn/s/blog_409cc4b00100qmgz.html) 也许这一篇文章的内容有点枯燥,但我要说的是如果你想深入的了解游戏引擎是如何处理动画片 ...

  5. 微信小程序开发之——动画-Animation(3)

    一 概述 wx.createAnimation创建Animation动画实例 wx.createAnimation时,参数的常用属性 动画常见动作 动画执行完成之后导出动画队列(export) 二 w ...

  6. 04_动画 animation

    过渡 vs 动画 过渡:用于两个状态变化过程 动画:实现多个状态间的变化过程,动画过程可控 动画 动画的本质:快速切换大量图片时在人脑中行成的具有连续性的画面 构成动画的最小单元:帧或动画帧 实现步骤 ...

  7. Android 高级UI解密 (四) :花式玩转贝塞尔曲线(波浪、轨迹变换动画)

    讲解此UI系列必然少不了一个奇妙数学曲线-–贝塞尔曲线,它目前运用于App的范围是在太广了,最初的QQ气泡拖拽,到个人界面的波浪效果.Loading波浪效果,甚至于轨迹变化的动画都可以依赖贝塞尔曲线完 ...

  8. 贝塞尔曲线之购物车动画效果

    Question 贝塞尔曲线是什么? 贝塞尔曲线可以做什么? 怎么做? What is it ? 贝塞尔曲线在百度定义是贝塞尔曲线(Bézier curve),又称 贝兹 曲线或贝济埃曲线,是应用于二 ...

  9. Android 实现仿京东购物车动画

    为了给商品加入购物车添加点酷炫动画,最近参考了京东,淘宝等常用的电商App的购物车添加动画,根据产品的业务需求,使用补间动画结合插值器实现: 效果图: 关键代码(修改控件id,可直接使用): priv ...

最新文章

  1. flutter 实现不可滚动的ListView构建器
  2. boost::type_erasure::binding_of相关的测试程序
  3. VTK:Medical之GenerateModelsFromLabels
  4. java的比较运算符是_Java 基础(比较运算符,逻辑运算符,三元运算符)
  5. 《RabbitMQ 实战指南》第四章 RabbitMQ进阶(上)
  6. Leetcode每日一题:108.convert-sorted-array-to-binary-search-tree(有序数组转成BST)
  7. 12.15 Ext JS 选人组件
  8. c#读取ini配置文件、将配置数据保存至ini文件
  9. lamda表达式对list各种处理
  10. python--反射机制
  11. android上的sip软电话
  12. 秦九韶算法(java实现)
  13. python pandas 增加一列_Python Pandas 向DataFrame中添加一行/一列
  14. 1132. Cut Integer (20)
  15. Go channel 通道
  16. 手机扫描电脑百度网盘二维码,二维码无法刷新的解决办法
  17. 倪光南——世人笑我太疯癫,我笑他人看不穿
  18. nbiot和2g_NBIoT网络覆盖性能评价与优化
  19. 连锁店管理系统如何助力零售业
  20. Games202作业1 Unity(更新完毕)

热门文章

  1. suse11sp4配置vnc显示gnome
  2. 使用WinPcap和libpcap类库读写pcap文件(002)PCAP文件格式
  3. 传图识字有次数限制吗_5岁娃识字3000?别羡慕!过早逼娃认字,后果很严重
  4. 区块链热度背后的资本市场
  5. 1069 The Black Hole of Numbers
  6. 无线传感器网络WSN技术、协议、距离汇总
  7. 参加软件测试培训前景怎么样
  8. leetcode--删除排序链表中的重复元素--python
  9. Spring Boot 和 testNG 和 eclipse背景色
  10. 联通和阿里云合作 建互联网云化卡号管理系统