在新的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. HTML 表格入门

    每个表格都是由 table 标签开始. 每个表格行由 tr 标签开始. 每个表格数据由 td 标签开始. 这样是一行三列: <table border="1"> < ...

  2. 微软TTS,Neospeech TTS 简单使用

    今天搞了下微软的TTS,逛了好多网页.博客,拼拼凑凑搞了点东西吧. 首先添加类库调用,系统自带的system.speech using System.Speech.Synthesis; 然后就能调用方 ...

  3. Adnroid Studio使用技巧

    官方第一条提示:所有的使用技巧都可以通过Help→Tips of the Day查看. 下面摘抄一些比较有用的技巧: 1.Esc把活动窗口从工具窗口指向编辑窗口.F12把编辑窗口指向上一次活动的工具窗 ...

  4. SQL Server -查看数据库扩展属性

    1.fn_listextendedproperty 函数可以基于对象类型显示单个数据库对象或数据库中所有对象的扩展属性.例如,可以返回表或表中所有列的扩展属性. A.下面的示例显示了数据库本身设置的所 ...

  5. windows2008无线网卡和.net3.5安装

    今天在联想T420S笔记本上安装windows2008标准版,安装完成后部分驱动软件不能安装,要求.net framework3.5,下载.net3.5安装时提示应该用角色管理器安装. 根据提示打开服 ...

  6. C语言实现界面(不通过MFC\避免遗忘)

    感觉MFC不属于程序员细究的东西,今实现基本界面避免日后遗忘. 源代码: #include<windows.h>#include<stdio.h>char str[] = {' ...

  7. 简单的php数据库操作类代码(增,删,改,查)

    这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...

  8. JavaScript函数学习要点总结(一)

    1.函数的最大作用是提供代码复用: 2.JavaScript函数是弱类型语言,定义函数时既不要声明函数的返回值,也不要声明函数的参数列表类型: 语法如下: function functionName( ...

  9. 关于tuple的只读特性

    a = (1,3,[5,4,1]) a[2][1] = 2 print(a) 结果是:(1,3,[5,2,1]) 可以看到,在这里tuple的内容被修改了. 原因就是tuple的“只读”属性是指tup ...

  10. vmware9安装centos和Mac经验总结

    其实以前安装过,但是这次安装发现还是很陌生 主要是因为以下几点vm11下载的最新版本结果不支持用vm-Mac激活包一直激活不成功,所以只好用vm9来进行,中间的细节操作就是要停止的那几项VMware服 ...