在新的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. Tcp 数据对象传输接口对象设计

    输入是一个对象inputObj,接口对象.Send(inputObj),对端接收之后解包成outputObj(与inputObj应相同),触发onPackageReceive事件 事件 public ...

  2. (转) Pointers

    原地址 http://www.cplusplus.com/doc/tutorial/pointers/ Pointers In earlier chapters, variables have bee ...

  3. bzoj 2049 Cave 洞穴勘测(LCT)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 动态树入门题,不需要维护任何信息. 我用的是splay,下标实现的lct. #in ...

  4. [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...

  5. 通过startup启动tomcat一闪而过的问题

    下载了免安装版的tomcat7,在通过startup.bat启动时,控制台一闪而过.记事本讲startup.bat打开,在最后一行加上pause,然后重新双击startup.bat,发现控制台打印的错 ...

  6. ZendFramework2 源码分析 index.php

    <?php /** * This makes our life easier when dealing with paths. Everything is relative * to the a ...

  7. C#代码实现,确保windows程序只有一个实例(instance)

    static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...

  8. google浏览器翻译失败解决方案

    用记事本打开 C:\Windows\System32\drivers\etc下hosts文件 在文件末尾加入如下两行 203.208.46.145 translate.google.com 203.2 ...

  9. Android jar包混淆

    具体可参考http://proguard.sourceforge.net/manual/examples.html#library 1.找到android的adt目录下的 D:\soft\adt-bu ...

  10. Android之自定义checkbox样式

    大部分情况下,我们在UI中并不采用Android自带的checkbox复选框样式,这时候就需要我们自定义自己的checkbox. 首先找两张checkbox背景图片,比如下图样子的: 然后在drawa ...