Android Animation学习 实现 IOS 滤镜退出动画
IOS的用户体验做的很好,其中一点很重要的地方就是动画效果。
最近在学习Android的Animation,简单实现了一个IOS相机滤镜退出的动画:
布局文件:activity_animation_demo.xml 布局未考虑各个分辨率,只是为了实现动画逻辑,(代码测试是在720P分辨率的手机上)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.andanimationdemo.AnimationDemoActivity" > <FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="25px"
android:layout_marginRight="25px"
>
<LinearLayout
android:id="@+id/rootLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/firstLinearLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/Button_1"
android:layout_width="200px"
android:layout_height="200px"
android:background="@android:color/holo_blue_bright"/>
<Button
android:id="@+id/Button_2"
android:layout_width="200px"
android:layout_height="200px"
android:layout_marginLeft="35px"
android:background="@android:color/holo_green_light"/>
<Button
android:layout_marginLeft="35px"
android:id="@+id/Button_3"
android:layout_width="200px"
android:layout_height="200px"
android:background="@android:color/black"/>
</LinearLayout> <LinearLayout
android:id="@+id/SencondLinearLayout"
android:layout_marginTop="35px"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/Button_4"
android:layout_width="200px"
android:layout_height="200px"
android:background="@android:color/holo_orange_dark"/>
<Button
android:id="@+id/Button_5"
android:layout_width="200px"
android:layout_height="200px"
android:layout_marginLeft="35px"
android:background="@android:color/holo_red_light"/>
<Button
android:layout_marginLeft="35px"
android:id="@+id/Button_6"
android:layout_width="200px"
android:layout_height="200px"
android:background="@android:color/darker_gray"/>
</LinearLayout> <LinearLayout
android:id="@+id/ThirdLinearLayout"
android:layout_marginTop="35px"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/Button_7"
android:layout_width="200px"
android:layout_height="200px"
android:background="@android:color/holo_green_light"/>
<Button
android:id="@+id/Button_8"
android:layout_width="200px"
android:layout_height="200px"
android:layout_marginLeft="35px"
android:background="@android:color/holo_orange_light"/>
<Button
android:layout_marginLeft="35px"
android:id="@+id/Button_9"
android:layout_width="200px"
android:layout_height="200px"
android:background="@android:color/holo_blue_light"/>
</LinearLayout>
</LinearLayout>
</FrameLayout> <Button
android:id="@+id/Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="40dp"
android:layout_marginBottom="40dp"
android:text="Reset"></Button> </RelativeLayout>
AnimationDemoActivity.java
package com.example.andanimationdemo; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.LinearLayout; public class AnimationDemoActivity extends Activity implements OnClickListener{ private static final String TAG = "AnimationDemo";
LinearLayout rootLinearLayout;
Button resetButton;
int mCurrentClickButtonId = -1; int[] ButtonID = new int[] {
R.id.Button_1,R.id.Button_2,R.id.Button_3,
R.id.Button_4,R.id.Button_5,R.id.Button_6,
R.id.Button_7,R.id.Button_8,R.id.Button_9
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_demo);
rootLinearLayout = (LinearLayout) findViewById(R.id.rootLinearLayout);
resetButton = (Button) findViewById(R.id.Reset);
setButtonListener();
} private void setButtonListener() {
for (int i = 0; i < ButtonID.length; i++) {
rootLinearLayout.findViewById(ButtonID[i]).setOnClickListener(this);
}
resetButton.setOnClickListener(this);
} /**
* 点击某个按钮后,开始放大动画
* 这里提供的是一个思路,并不局限于当前布局,放大倍数,通过哪个点放大,都可以计算出来。
*/
boolean onAnimationRunning = false; public void onAnimationButtonClick() {
Log.d(TAG, "onAnimationButtonClick");
int[] position = new int[2];
int[] ButtonPosition = new int[2];
Button AnimaitonBtton = (Button) rootLinearLayout.findViewById(ButtonID[mCurrentClickButtonId - 1]);
rootLinearLayout.getLocationInWindow(position);
AnimaitonBtton.getLocationInWindow(ButtonPosition);
int rootWidth = rootLinearLayout.getWidth();
int rootHeight = rootLinearLayout.getHeight();
int ButtonWidth = AnimaitonBtton.getWidth();
int ButtonHeight = AnimaitonBtton.getHeight(); /**
* 计算 scale factor
*/
float widthRate = (float)rootWidth/ButtonWidth;
float heightRate = (float)rootHeight/ButtonHeight; /**
* 计算放大的中心点
*/
float PointA = (ButtonPosition[0] - position[0]) * widthRate / (widthRate - 1);
float PointB = (ButtonPosition[1] - position[1]) * heightRate / (heightRate - 1); onAnimationRunning = true;
ScaleAnimation mScaleAnimation = new ScaleAnimation(1.0f, widthRate, 1.0f, heightRate,PointA,PointB);
mScaleAnimation.setDuration(2000);
mScaleAnimation.setFillAfter(true);
mScaleAnimation.setAnimationListener(new AnimationListener() { @Override
public void onAnimationStart(Animation animation) {
} @Override
public void onAnimationRepeat(Animation animation) {
} @Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG,"onAnimationEnd");
onAnimationRunning = false;
for (int i = 0; i< ButtonID.length; i++) {
rootLinearLayout.findViewById(ButtonID[i]).setEnabled(false);
}
}
});
rootLinearLayout.startAnimation(mScaleAnimation); } @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d(TAG, "onClick :" + v.getId());
if (onAnimationRunning) {
Log.d(TAG, "onAnimationRunning = true");
return;
} switch (v.getId()) {
case R.id.Button_1:
mCurrentClickButtonId = 1;
onAnimationButtonClick();
break;
case R.id.Button_2:
mCurrentClickButtonId = 2;
onAnimationButtonClick();
break;
case R.id.Button_3:
mCurrentClickButtonId = 3;
onAnimationButtonClick();
break;
case R.id.Button_4:
mCurrentClickButtonId = 4;
onAnimationButtonClick();
break;
case R.id.Button_5:
mCurrentClickButtonId = 5;
onAnimationButtonClick();
break;
case R.id.Button_6:
mCurrentClickButtonId = 6;
onAnimationButtonClick();
break;
case R.id.Button_7:
mCurrentClickButtonId = 7;
onAnimationButtonClick();
break;
case R.id.Button_8:
mCurrentClickButtonId = 8;
onAnimationButtonClick();
break;
case R.id.Button_9:
mCurrentClickButtonId = 9;
onAnimationButtonClick();
break;
case R.id.Reset:
mCurrentClickButtonId = -1;
rootLinearLayout.clearAnimation();
rootLinearLayout.postInvalidate();
for (int i = 0; i< ButtonID.length; i++) {
rootLinearLayout.findViewById(ButtonID[i]).setEnabled(true);
}
break;
default:
break;
}
}
}
点击某个Button后,可以通过它所在的位置坐标,以及父布局所在的位置坐标,计算出通过哪个点放大。
实现的效果如下图:

如有什么不对的地方,还望大神指正。
Android Animation学习 实现 IOS 滤镜退出动画的更多相关文章
- Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition
Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition Property animation系统还提供了对ViewGroup中的View改变 ...
- Android Animation学习(四) ApiDemos解析:多属性动画
Android Animation学习(四) ApiDemos解析:多属性动画 如果想同时改变多个属性,根据前面所学的,比较显而易见的一种思路是构造多个对象Animator , ( Animator可 ...
- Android Animation学习(三) ApiDemos解析:XML动画文件的使用
Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...
- Android Animation学习(六) View Animation介绍
Android Animation学习(六) View Animation介绍 View Animation View animation系统可以用来执行View上的Tween animation和F ...
- Android Animation学习(二) ApiDemos解析:基本Animators使用
Android Animation学习(二) ApiDemos解析:基本Animatiors使用 Animator类提供了创建动画的基本结构,但是一般使用的是它的子类: ValueAnimator.O ...
- Android Animation学习(一) Property Animation原理介绍和API简介
Android Animation学习(一) Property Animation介绍 Android Animation Android framework提供了两种动画系统: property a ...
- Android Animation学习(二) ApiDemos解析:基本Animatiors使用
Animator类提供了创建动画的基本结构,但是一般使用的是它的子类: ValueAnimator.ObjectAnimator.AnimatorSet ApiDemos中Animation部分是单独 ...
- Android Animation学习笔记
原文地址: http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html 关于动画的实现,Android提供了Animation,在And ...
- Android animation学习笔记之view/drawable animation
前一章中总结了android animation中property animation的知识和用法,这一章总结View animation和Drawable animation的有关知识: View ...
随机推荐
- SAAS相关技术要点
这篇文章本来是我们开发组内部用的一个小文档.因为我们公司以前没有做SAAS的经验,就成立了一个小组做一做这方面的技术前探,我是成员之一.这篇文档想从宏观的层面把开发一个SAAS应用所要用到的技术点稍微 ...
- mysql左联右联内联
在MySQL中由于性能的关系,常常要将子查询(Sub-Queries)用连接(join)来却而代之,能够更好地使用表中索引提高查询效率. 下面介绍各种join的使用,先上图: 我们MySQL常用的为左 ...
- JS仿淘宝详情页菜单条智能定位效果
类似于淘宝详情页菜单条智能定位 对于每个人来说并不陌生!如下截图所示:红色框的那部分! 基本原理: 是用JS侦听滚动事件,当页面的滚动距离(页面滚动的高度)大于或者等于 "对象"( ...
- tomcat 系统架构与设计模式 第一部分 系统架构工作原理 转
Tomcat 系统架构与设计模式,第 1 部分: 工作原理 许 令波, Java 开发工程师, 淘宝网 许令波,现就职于淘宝网,是一名 Java 开发工程师.对大型互联网架构设计颇感兴趣,并对一些开源 ...
- dump iot表
SQL> create user scan identified by scan default tablespace users; User created. SQL> grant db ...
- MySQL的Grant命令
来源:http://yingxiong.javaeye.com/blog/451208 本文实例,运行于 MySQL 5.0 及以上版本. MySQL 赋予用户权限命令的简单格式可概括为: gra ...
- 大型系统OA--需求
1.面向普通用户 普通用户一般都是公司的小职员,上OA都干什么呢?当然是获取信息咯.还有最好很多事儿,不需要跟公司的其他部门的人儿,打交道,直接在网上解决就好了. 收发邮件: 查看自己发动的流程,也就 ...
- 培训机构出来的iOS学员怎么了?
事件回放 前几天在 iOS 开发群里看到有人贴了一个 v2ex 上的帖子(地址:https://www.v2ex.com/t/244437 ) ,大概说收到了 1000 多份某培训机构出来的学员简历. ...
- HDOJ 2018 母牛的故事
Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? Input 输入数据由多个测 ...
- Service的两种启动方法
刚才看到一个ppt,介绍service的两种启动方法以及两者之间的区别. startService 和 bindService startService被形容为我行我素,而bindService被形容 ...