在新的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. ASP.NET网站与ASP.NET应用程序的区别

    我们使用VS做ASP.NET的时候,可以选择新建ASP.NET应用程序,同时也可以新建ASP.NET网站,两者有什么具体区别呢?今天真是很幸运,比别人多上了老师一节课,讲的是这两者之间的一些区别.我学 ...

  2. ios10 适配问题总结

    看各个大神整理而成 1.检查版本问题 不可以像下面这样用 #define isiOS10 ([[[[UIDevice currentDevice] systemVersion] substringTo ...

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

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

  4. CDZSC_2015寒假新人(2)——数学 G

    G - G Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  5. python调webservice和COM接口

    调webservice # -*- coding: cp936 -*- from suds.client import Client url = 'http://192.168.50.165/port ...

  6. Sublime text3使用技巧及快捷键

    一.快速查找文件Crtl+P(Goto->Goto Anyghing) 在打开的搜索框中输入文件名按Enter键即可. 提示:1.支持文件夹+文件名的搜索,比如 "js/main.js ...

  7. Spring HibernateTemplate的使用

    Spring HibernateTemplate的使用 2008-03-25 11:38 2020人阅读 评论(0) 收藏 举报 springbeanhibernatesessiondaoclass ...

  8. [Head First Python]5. 推导数据:处理数据

    读取4个文件内容,格式化数据,升序,显示每个文件前3个数据 julie.txt 2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21 james.txt 2-34, ...

  9. Hdu1089

    #include <stdio.h> int main() { int a,b; while(scanf("%d %d",&a,&b)!=EOF){ p ...

  10. 常见的SQL字符串函数

    1.LEN:计算字符串的长度(字符的个数) select len('哈哈hello') 返回长度为7 2.datalength();计算字符串所占用的字节数,不属于字符串函数 select DATAL ...