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

Android 曲线动画animation,类似加入购物车动画

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

资源图片

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

相关文章:

小程序音频播放报10001 解决方案 errCode:10001, errMsg:errCode:602,err:error,not found param

音频播放有两种方式&#xff1a; 第一种&#xff1a; innerAudioContext.src audioSrc;innerAudioContext.play(); 第二种&#xff1a; innerAudioContext.autoplay true;innerAudioContext.src audioSrc; 之前使用第一种&#xff0c;华为手机不能正常播放&#xff0c;…

在线学位课程_您在四年制计算机科学学位课程中学到的知识

在线学位课程by Colin Smith通过科林史密斯 您在四年制计算机科学学位课程中学到的知识 (What you learn in a 4 year Computer Science degree) I recently wrote an article on whether you need a computer science degree to get a job in tech. I thought that going ove…

Swift学习笔记-协议(Protocols)

1.0 翻译&#xff1a;geek5nan 校对&#xff1a;dabing1022 2.0 翻译&#xff1a;futantan 校对&#xff1a;小铁匠Linus 定稿&#xff1a;shanksyang 本页包含内容&#xff1a; 协议的语法&#xff08;Protocol Syntax&#xff09;对属性的规定&#xff08;Property Requireme…

JavaScript简单重写构造器的原型

1 //简单重写原型对象&#xff1a;2 3 //一个构造函数Person4 function Person(){5 6 }7 //重写Person的原型8 //把Person的原型赋值给一个新的对象 是我们重写的过程9 Person.prototype{ 10 // 对于构造器 如果我们不给他写&#xff0c;则构造器就是Object的构造器了 …

node.js cannot find module

找不到模块的解决方案 &#xff1a; 把node_module整个文件夹删掉,然后npm clean cache,看下package.json里有没有express的依赖项,有的话直接npm install,没有的话 npm install express --save。 有人说 npm clean cache 命令变成了 npm cache clean&#xff0c;可以都试一下

我在React Native中构建时获得的经验教训

by Amanda Bullington通过阿曼达布林顿(Amanda Bullington) 我在React Native中构建时获得的经验教训 (Lessons I learned while building in React Native) When I received an offer for a software engineering role to build an app in React Native, I wasn’t sure what…

【Ant Design Pro 五】箱套路由在菜单栏显示返回上一页

效果图&#xff1a; 场景&#xff1a;从菜单栏进入子页面&#xff0c;但是子页面默认不在路由显示&#xff0c;完成操作后需要返回上级页面。所以要在菜单栏中加返回的功能。 实现代码&#xff1a; import React from react; import { Button, Card, Icon } from antd; impor…

[002] The Perks of Being a Wallflower - 读后记

The Perks of Being a Wallflower 今天(2015年10月30日 18:26:17)读完"The Perks of Being a Wallflower". 本书290页,我是在小米pad上完成阅读的,epub格式,花费四天时间,每天至少5小时. 生词很多,就不一一列出了. 使用透析法并不强求完全的正确理解原文.强调完整的阅…

ios集成firebase_如何将Firebase与您的应用程序集成

ios集成firebaseYou’ve probably heard about Firebase, but may not know much about how it works and how it fits in with your application. Well, you’ve come to the right place. We’ll go over what Firebase is and how to integrate it with your Android projec…

PLSQL创建Oracle定时任务

转自&#xff1a;http://www.cnblogs.com/yx007/p/6519544.html#_label0转载于:https://www.cnblogs.com/good-tomorrow/p/7443817.html

判断h5是不是在小程序中

执行代码&#xff1a; if (ua.indexOf(MicroMessenger) -1) {//说明不在微信中// 走不在小程序的逻辑 } else {wx.miniProgram.getEnv(function(res) {if (res.miniprogram) {// 走在小程序的逻辑} else {// 走不在小程序的逻辑}}) }

CSS3关于过渡效果的问题

首先trasition:transform只是单单表示后面只要有含有的tranform的所有属性可以参与动画&#xff0c;而trasition:all表示后面所有动画属性都可以参动画&#xff0c;当父容器有relative时&#xff0c;子容器有absolute&#xff0c;子容器会跟着父容器相对定位。当你想要然后一个…

在线学位课程_如何选择计算机科学学位课程

在线学位课程by Colin Smith通过科林史密斯 如何选择计算机科学学位课程 (How to choose a Computer Science degree program) I remember combing through the countless computer science programs online and feeling a bit lost on what I should be looking for. I ended…

Dubbo 入门实例 本地伪集群测试Demo

1. 概述 Dubbo是一个分布式服务框架&#xff0c;致力于提供高性能和透明化的RPC远程服务调用方案&#xff0c;以及SOA服务治理方案 Dubbo是阿里巴巴SOA服务化治理方案的核心框架&#xff0c;每天为2,000个服务提供3,000,000,000次访问量支持&#xff0c;并被广泛应用于阿里巴…

Vue源码终笔-VNode更新与diff算法初探

写完这个就差不多了&#xff0c;准备干新项目了。 确实挺不擅长写东西&#xff0c;感觉都是罗列代码写点注释的感觉&#xff0c;这篇就简单阐述一下数据变动时DOM是如何更新的&#xff0c;主要讲解下其中的diff算法。 先来个正常的html模板&#xff1a; <body><div id…

JS获取当月每天的日期,JS获取本周每天的日期

获取当前月每天的日期&#xff0c;获取当前周每天的日期实现代码&#xff1a; 调用代码&#xff1a; console.log(-----------------, getNowM(), getWeekDay()) 结果&#xff1a;我今天是2020-2-28日 封装方法&#xff1a; function getDay(num, str) {var today new Dat…

@Scheduled注解的scheduler属性什么作用

注解是 Spring Framework 提供的一种机制,用于定义计划任务,即周期性执行的任务。 注解可以应用于方法上,以指示 Spring 容器在特定的时间间隔或按照某种调度规则来调用该方法。 属性是 注解的一个可选属性,它的作用是允许开发者指定一个自定义的 对象来控制任务的调度方式。默认情况下, 注解使用 Spring 内部的 来执行任务,但如果需要更高级的定制化需求,可以通过 属性指定一个自定义的 实现。自定义调度器:共享调度器资源:高级调度需求:假设你想使用 作为调度器,并且希望所有带有

自行车车把会吧车刮坏吗_花10分钟即可开始使用车把

自行车车把会吧车刮坏吗by Wing Puah永帕(Wing Puah) 花10分钟即可开始使用车把 (Take 10 minutes to get started with Handlebars) Nowadays front-end development is no longer about building static HTML markup and compiling SASS files. The rise of Single Page App…

每天一个linux命令(33):df 命令

linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况。可以利用该命令来获取硬盘被占用了多少空间&#xff0c;目前还剩下多少空间等信息。 1&#xff0e;命令格式&#xff1a; df [选项] [文件] 2&#xff0e;命令功能&#xff1a; 显示指定磁盘文件的可用…

一:HDFS 用户指导

1.hdfs的牛逼特性 Hadoop, including HDFS, is well suited for distributed storage and distributed processing using commodity hardware. It is fault tolerant, scalable, and extremely simple to expand. MapReduce, well known for its simplicity and applicability …

uni-app 封装企业微信config

第一步&#xff0c;在项目根目录加一个html文件&#xff0c; index.html 代码如下&#xff1a; <!DOCTYPE html> <html lang"zh-CN"><head><meta charset"utf-8"><meta http-equiv"X-UA-Compatible" content"I…

sqoop架构_SQOOP架构的深入介绍

sqoop架构by Jayvardhan Reddy通过杰伊瓦尔丹雷迪(Jayvardhan Reddy) SQOOP架构的深入介绍 (An in-depth introduction to SQOOP architecture) Apache Sqoop is a data ingestion tool designed for efficiently transferring bulk data between Apache Hadoop and structure…

JS实现录音,播放完整代码带示例图

效果图&#xff1a; 实现代码&#xff1a; <!DOCTYPE html> <html><head><script src"recorder.js" type"text/javascript" charset"utf-8"></script><meta name"viewport" content"widthdevi…

r.json()

requests模块中&#xff0c;r.json()为Requests中内置的JSON解码器 其中只有response返回为json格式时&#xff0c;用r.json()打印出响应的内容&#xff0c; 如果response返回不为json格式&#xff0c;使用r.json()会报错 报错内容&#xff1a;ValueError: Expecting property …

冒泡排序语法树

转载于:https://www.cnblogs.com/alfredzhu/p/4939268.html

valve 的设计_向Valve Portal开发人员学习游戏设计原则

valve 的设计In this talk, Valve programers who created the game Portal discuss problems they faced in development and how they solved them. Leaning about how they solved Portal problems can give you insight into how to design better games.在本次演讲中&…

Android之控件使用

Android系统为我们提供了大量的控件&#xff0c;例如&#xff1a;开关控件、单选按钮、多选按钮、单选菜单等等&#xff0c;那么这些控件如何使用呢&#xff1f;本篇我将带领大家一道学习一下如何使用这些控件。所谓无图无真相&#xff0c;先让大家看一下效果图&#xff1a; 下…

《对软件工程课程的期望》

要学习到的能力的预期&#xff1a;要学会个人&#xff0c;结对&#xff0c;团队的代码编辑流程&#xff0c;学会和别人进行交流。 对项目课程的期望&#xff1a;希望不是枯燥的代码详解。 对项目的愿景规划&#xff1a;希望团队里的每个人都能学到有用的知识。转载于:https://w…

HTML发送语音,上传音频PHP接收

实现需求&#xff1a;网页录制音频上传给后端接收&#xff0c;接收后PHP把文件的名字存到数据库的表里面&#xff0c;这里我的后端用的是PHP&#xff0c;并且把代码贴出来了。 前端实现代码&#xff1a; <!DOCTYPE HTML> <html><head><meta http-equiv&q…

html:漂亮的原生表格_HTML表格:关于它们的所有知识

html:漂亮的原生表格by Alexander Gilmanov亚历山大吉尔马诺夫(Alexander Gilmanov) HTML表格&#xff1a;关于它们的所有知识 (HTML Tables: All there is to know about them) Judging by the fact that we created wpDataTables, it’s no secret that we like tables. So …