---
title: "Android实现抖动动画"
author: "scliu0718"
date: "2013-01-15T17:20:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2013-05-17T09:16:46Z"
id: 1870695
url: "https://scliu0718.iteye.com/blog/1870695"
---

使用动画让控件实现抖动，其实就是让横移（可心混合其它类型旋转等一起）的动画循环播放，使用Interpolator类来实现。

原理很简单，不多说了，代码如下：

Activity代码：

```
		this.btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 TextView yearAndMouth = (TextView) JGWorkLogDateAndOperaPanel.this.findViewById(R.id.year_mounth_text);
				 Animation cycleAnim = AnimationUtils.loadAnimation(context, R.anim.img_anim);
				 yearAndMouth.startAnimation(cycleAnim);
			}
		});
```

imag\_anim.xml

```
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="100"
        android:fromYDelta="0"
        android:toYDelta="500"
        android:fromXDelta="0"
        android:toXDelta="1"
        android:repeatCount="5"
        android:repeatMode="restart"
        android:interpolator="@anim/cylce" />
    <rotate 
        android:duration="300"
        android:pivotX="100%p"
        android:pivotY="100%p"
        android:fromDegrees="90"
        android:interpolator="@anim/cylce" />

</set>
```

cylce.xml

```
<?xml version="1.0" encoding="utf-8"?>
<!--
官方解析 动画循环加速器:
——AccelerateInterpolator：动画从开始到结束，变化率是一个加速的过程。
——DecelerateInterpolator：动画从开始到结束，变化率是一个减速的过程。
——CycleInterpolator：动画从开始到结束，变化率是循环给定次数的正弦曲线。
——AccelerateDecelerateInterpolator：动画从开始到结束，变化率是先加速后减速的过程。
——LinearInterpolator：动画从开始到结束，变化率是线性变化。

-->
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:cycles="5" />
```