---
title: "Tween 补间动画（软编码方式）"
author: "AndLi"
date: "2011-10-10T07:49:08Z"
tags: []
categories: ["移动开发"]
slug: "tween"
draft: false
lastmod: "2011-10-08T19:18:45Z"
id: 1187131
url: "https://andli.iteye.com/blog/1187131"
---

```
/**
 * Tween 动画
 * 
 * @author lilin
 * @date 2011-9-5 下午04:03:40
 * @ClassName: Main
 * @Description: 通过XML布局的方式
 */
public class MainActivity extends Activity implements OnClickListener {
	private Button b1, b2, b3, b4;
	private ImageView imageView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imageView = (ImageView) findViewById(R.id.ImageView01);
		b1 = (Button) findViewById(R.id.Button01);
		b2 = (Button) findViewById(R.id.Button02);
		b3 = (Button) findViewById(R.id.Button03);
		b4 = (Button) findViewById(R.id.Button04);
		b1.setOnClickListener(this);
		b2.setOnClickListener(this);
		b3.setOnClickListener(this);
		b4.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Button01:// 创建Sacle(尺寸)变化动画
			Animation scaleAnimation = AnimationUtils.loadAnimation(
					MainActivity.this, R.anim.my_scale);
			imageView.startAnimation(scaleAnimation);// 开始动画
			break;
		case R.id.Button02:// 创建Alpha(渐变)动画
			Animation alphaAnimation = AnimationUtils.loadAnimation(
					MainActivity.this, R.anim.my_alpha);
			imageView.startAnimation(alphaAnimation);
			break;
		case R.id.Button03:// 创建translate(位置变化)动画
			Animation translateAnimation = AnimationUtils.loadAnimation(
					MainActivity.this, R.anim.my_translate);
			imageView.startAnimation(translateAnimation);
			break;
		case R.id.Button04:// 创建rotate(旋转)动画
			Animation rotateAnimation = AnimationUtils.loadAnimation(
					MainActivity.this, R.anim.my_rotate);
			rotateAnimation.setDuration(3000);
			imageView.startAnimation(rotateAnimation);
			break;
		default:
			break;
		}

	}
}
```

 my\_alpha.xml

```
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<alpha 
	android:fromAlpha="0.1" 
	android:toAlpha="1.0"
	android:duration="5000" />
</set>
```

 

my\_rotate.xml

```
<set xmlns:android="http://schemas.android.com/apk/res/android">  
   <rotate 
   		android:fromDegrees="0"
   		android:toDegrees="-180"
   		android:pivotX="50%"
   		android:pivotY="50%"
   		android:duration="5000"
   />
</set>
```

 

my\_scale.xml

```
<set xmlns:android="http://schemas.android.com/apk/res/android">  
   <scale 
   		android:fromXScale="0.0"
   		android:toXScale="1.0"
   		android:fromYScale="0.0"
   		android:toYScale="1.0"
   		android:pivotX="50%"
   		android:pivotY="50%"
   		android:duration="5000"
   />
</set>
```

 

my\_translate.xml

```
<set xmlns:android="http://schemas.android.com/apk/res/android">  
   <translate
   		android:fromXDelta="10"
   		android:toXDelta="100"
   		android:fromYDelta="10"
   		android:toYDelta="100"
   />
</set>
```