在新的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内存泄漏监测(MAT)及解决办法

    http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html 这篇文章是2010年1月份写的,其中有些已经 ...

  2. stm32之通用定时器TIM

    STM32系列的CPU,有多达8个定时器: 1.其中TMI1和TIM8是能够产生三对PWM互补输出的高级定时器,常用于三相电机的驱动:它们的时钟有APB2的输出产生: 2.其它6个为普通定时器,时钟由 ...

  3. My way to Python - Day03

    列表和字典的赋值 dict1 = {} dict1['k1'] = 'v1' list1 = [] list1.append('v1') 集合系列 1,计数器 Python 2.7.6 (defaul ...

  4. parseInt引发的血案

    今天做了个专题活动,页面头上有个倒计时 专题做完后上线了,没发现有什么问题,结果,运营MM突然和我说:技术哥哥出问题了,360浏览器在秒数从10到09的时候直接变成 00 了! 一看我去真的,该死的3 ...

  5. 一个项目涉及到的50个Sql语句(整理版)

    /* 标题:一个项目涉及到的50个Sql语句(整理版) 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句. */ --1.学生表Student(S,Sname,Sage,Sse ...

  6. flex 调用WebService2(基于.net)

    flex 访问WebService的方法有很多种,使用FLEX4中的"数据/服务"功能可以自动生成访问WebService的代理类,这样可以避免把所有的数据访问都写到MXML页面上 ...

  7. 设置改变oracle字符集

      修改过密码之后就能以dba的身份进行修改了,不是dba的话在执行修改命令的时候会提示你权限不足. 开始-->运行-->cmd,之后输入:"sqlplus sys/oracle ...

  8. iOS-设计模式之Block

    Block是代码块, Block定义 返回值 (^ 块名)(参数1,参数2…); 在定义Block的时候可以使用typedef 重命名一下. typedef void(^blockName)(NSSt ...

  9. JS正则表达式---分组

    JS正则表达式---分组 之前写了一篇关于正则新手入门的文章,本以为对正则表达式相对比较了解 但是今天我又遇到了一个坑,可能是自己不够细心的原因吧,今天就着重和大家分享一下javascript正则表达 ...

  10. C语言获取键盘按键

    在写控制台游戏的时候,发现不管用cin,scanf还是getchar,都不能实时的输入按键,必须要按回车才能读进去,而按回车的话会导致输入异常,所以要使用获取键盘按键的函数. 加入头文件#includ ...