在新的android sdk中谷歌为我们提供了新的动画实现方式。化繁为简。将以前的animation动画进一步封装,使用起来更加方便。

先来看XML文件:

<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" > <LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="40dp" > <ImageView
android:id="@+id/rect"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rect" />
<ImageView
android:id="@+id/star"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@drawable/star"
/>
</LinearLayout> <ImageView
android:id="@+id/b"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:background="@drawable/b" /> <ImageView
android:id="@+id/c"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:background="@drawable/c" /> <ImageView
android:id="@+id/d"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:background="@drawable/d" /> <ImageView
android:id="@+id/e"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:background="@drawable/e" /> <ImageView
android:id="@+id/a"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:background="@drawable/a" /> </RelativeLayout>

然后看看activity文件

package com.example.testanimator;

import java.util.ArrayList;
import java.util.List; import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.widget.ImageView;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener {
private ImageView a, b, c, d, e;
private int res[] = { R.id.a, R.id.b, R.id.c, R.id.d, R.id.e };
private List<ImageView> listImg = new ArrayList<ImageView>();
private boolean isStart = false;
private float y;
private float x; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
for (int i = 0; i < res.length; i++) {
ImageView img = (ImageView) findViewById(res[i]);
listImg.add(img);
img.setOnClickListener(this);
}
x = listImg.get(0).getPivotX();
y = listImg.get(0).getPivotY();
} private void startAnimator() { isStart = true;
for (int i = 0; i < listImg.size(); i++) {
ObjectAnimator.ofFloat(listImg.get(i), "translationY", y, y - 400 + i * 100).setDuration(1000).start();
ObjectAnimator.ofFloat(listImg.get(i), "translationX", x, x + i * 100).setDuration(1000).start();
}
} private void closeAnimator() {
isStart = false;
for (int i = 0; i < listImg.size(); i++) {
ObjectAnimator animator = ObjectAnimator.ofFloat(listImg.get(i), "translationY", y - 400 + i * 100, y); animator.setDuration(1000);
animator.setStartDelay(1000);
animator.setInterpolator(new BounceInterpolator()); //设置插值器
animator.start();
} } @Override
public boolean onTouchEvent(MotionEvent event) {
float startx ,starty;
float endx ,endy ; switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: break;
case MotionEvent.ACTION_MOVE: break;
default:
break;
}
return super.onTouchEvent(event);
} @Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.a:
if (isStart) {
closeAnimator(); //还原位置
} else {
startAnimator(); //开始动画移动位置和焦点
}
break; default:
break;
} } }

android——ObjectAnimator动画的更多相关文章

  1. 【Android】Android ObjectAnimator动画初识、模仿

    ObjectAnimator: ObjectAnimator的概念这里就不解释了,直接从代码中说明,以下是模仿Persicope的加载动画,简单的几行代码即可实现,当然我也是模仿的,更好的实现思路还请 ...

  2. android——ObjectAnimator动画(一)

    直接贴上集中用法 package com.example.test; import com.example.test.views.CircleView; import android.animatio ...

  3. 【转】android 属性动画之 ObjectAnimator

    原文网址:http://blog.csdn.net/feiduclear_up/article/details/39255083 前面一篇博客讲解了 android 简单动画之 animtion,这里 ...

  4. Android属性动画之ObjectAnimator控制

    Android为我们提供了大量的动画效果,如何通过这些动画来达到我们需要的效果呢?今天就为大家总结一下ObjectAnimator动画控制事件. 该项目的的布局文件只有两个控件:ImageView和B ...

  5. Android属性动画之ObjectAnimator

    相信对于Android初学者,对于Android中的动画效果一定很感兴趣,今天为大家总结一下刚刚学到的属性动画案例. 首先和一般的Android应用一样,我们先建一个工程,为了方便,我们的布局文件中就 ...

  6. Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法 ...

  7. Android -- Fragment动画异常Unknown animation name: objectAnimator

    异常 Caused by: java.lang.RuntimeException: Unknown animation name: objectAnimator 异常代码 FragmentTransa ...

  8. Android属性动画ObjectAnimator的使用1

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/106 属性动画ObjectAnimator的使用 属性动画 ...

  9. Android属性动画

    这几天看郭神的博客 Android属性动画完全解析(上),初识属性动画的基本用法之后,我自己突然想实现一种动画功能,就是我们在携程网.阿里旅行等等手机APP端买火车票的时候,看到有选择城市,那么就有出 ...

随机推荐

  1. Android应用切换皮肤功能实现

    原文地址:http://www.eoeandroid.com/thread-318159-1-1.html 现在大多数android应用都支持切换皮肤的功能.比如千千静听,墨迹天气等等.本文介绍两种切 ...

  2. 无法登陆mysql服务器

    解决 .#2002 无法登录 MySQL 服务器 将config.sample.inc.php复制成config.inc.php 出现这个错误,表示没有连接到数据库.修改config.inc.php文 ...

  3. Android学习笔记--广播(Broadcast)

    1.Android广播分类 android的广播类型分为两类:标准广播和有序广播. 标准广播:异步广播,广播发出后,所有注册了的广播接收器都会同时接收到该广播.打个比方:做地铁过程中的语音播报,当列车 ...

  4. Socket编程(摘抄)

    http://www.blogjava.net 例子代码就在我的博客中,包括六个UDP和TCP发送接受的cpp文件,一个基于MFC的局域网聊天小工具工程,和此小工具的所有运行时库.资源和执行程序.代码 ...

  5. eclipse中配置免安装tomcat7

    参看如下链接:http://hi.baidu.com/gpy11/item/744c13e14614c9b52e140b25

  6. 写jQuery插件时,一种更好的合并参数的方法

    看到很多人写jQuery插件时居然这样合并参数: this.defaults = { 'color': 'red', 'fontSize': '12px', 'textDecoration':'non ...

  7. 关于安卓的log学习

    什么时候会产生log文件? 1. 程序异常退出 Uncaused Exception. 2. 程序强制关闭 Force Closed(FC). 3. 程序无响应 Application No Resp ...

  8. 【3】python核心编程 第五章-数字

    1.用大写字母 “L”表示长整数 尽管 Python 也支持用小写字母 L 标记的长整型,但是我们郑重推荐您仅使用大写的 “L”, 这样能有效避免数字1 和小写L 的混淆.Python 在显示长整数类 ...

  9. #1042 - Can't get hostname for your address

    my.ini 或 my.cnf 末尾添加 skip-name-resolve 并重启MySQL服务器 ok!

  10. WIN下和LINUX动态库的区别

    **************************************************************************************************** ...