代码摘自:Android 3.0 Animations Beginner's Guide
Tween用于两个状态之间的就动画切换,独立于View。可以创建Tween,然后将其添加到View的元素。
Tween包含<rotate>,<translate>,<alpha>,<scale>和<set>。
<translate>:view元素从一个位置移动到另一个位置
<rotate> :view元素以一个轴旋转
<scale> :view元素变大或者变小
<alpha> :修改view元素的alpha值,使其出现淡入淡出效果
<set> :集合,包含以上各中tween方式,并且可以嵌套使用set
block_drop.xml代码
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/bounce_interpolator" > <translate android:duration="3000" android:fromYDelta="-100%p"------------相对于parent的位置 android:toYDelta="0" /> </set>
TextView block_1 = (TextView) findViewById(R.id.block_1); Animation drop = AnimationUtils.loadAnimation(this, R.anim.block_drop); block_1.startAnimation(drop);
layout_tower.xml
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/block_drop" android:animationOrder="reverse" android:delay="20%" />
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" android:shareInterpolator="false" > <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:duration="1500" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:startOffset="1500" android:toDegrees="360" /> 顺时针旋转 <translate android:duration="3000" android:fromYDelta="0" android:toYDelta="-100%p" /> <translate android:duration="1500" android:startOffset="1500" android:toXDelta="100%" /> </set>
block_move_left.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" > <rotate android:duration="1500" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:startOffset="1500" android:toDegrees="-360" /> 逆时针旋转 <translate android:duration="3000" android:fromYDelta="0" android:toYDelta="-100%p" /> <translate android:duration="1500" android:startOffset="1500" android:toXDelta="-100%" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="1000" android:fromAlpha="1" android:repeatCount="infinite" android:repeatMode="reverse" android:toAlpha="0.7" /> </set>
package com.tween.hanoi;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
public class HanoiActivity extends Activity {
private static final int UNDECIDED = -1;
private int fromTower = UNDECIDED;
private static int[] towers = { R.id.tower_1, R.id.tower_2, R.id.tower_3 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// TextView block_1 = (TextView) findViewById(R.id.block_1);
// Animation drop = AnimationUtils.loadAnimation(this,
// R.anim.block_drop);
// block_1.startAnimation(drop);
// 如下代码表示使用函数加载动画到ViewGroup中
// AnimationSet set = new AnimationSet(true);
// TranslateAnimation translate = new TranslateAnimation(
// Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
// Animation.RELATIVE_TO_PARENT, -1.0f,
// Animation.RELATIVE_TO_PARENT, 0);
// set.setDuration(3000);
// set.setStartOffset(1500);
// set.addAnimation(translate);
// set.setInterpolator(new BounceInterpolator());
// LayoutAnimationController layoutanim = new LayoutAnimationController(set);
// layoutanim.setOrder(LayoutAnimationController.ORDER_REVERSE);
// layoutanim.setDelay(0.2f);
// ViewGroup tower_1 = (ViewGroup) findViewById(R.id.tower_1);
// tower_1.setLayoutAnimation(layoutanim);
for (int i = 0; i < towers.length; ++i) {
ViewGroup tower = (ViewGroup) findViewById(towers[i]);
tower.setOnClickListener(new TowerPicker(i));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class BlockMover implements Animation.AnimationListener {
private int to, from;
View block;
protected BlockMover(View block, int from, int to) {
this.block = block;
this.to = to;
this.from = from;
}
public void move() {
// Animation removeAnimation = AnimationUtils.loadAnimation(
// HanoiActivity.this, R.anim.block_move_right);
int block_anim_id;
if (to < from) {
block_anim_id = R.anim.block_move_left;
} else {
block_anim_id = R.anim.block_move_right;
}
Animation removeAnimation = AnimationUtils.loadAnimation(
HanoiActivity.this, block_anim_id);
block.startAnimation(removeAnimation);
removeAnimation.setAnimationListener(this);
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
block.post(new Runnable() {
public void run() {
ViewGroup toTower = (ViewGroup) findViewById(towers[to]);
ViewGroup fromTower = (ViewGroup) block.getParent();
fromTower.removeView(block);
fromTower.clearDisappearingChildren();
toTower.addView(block, 0);
Animation addAnimation = AnimationUtils.loadAnimation(
HanoiActivity.this, R.anim.block_drop);
block.setAnimation(addAnimation);
}
});
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
}
private class TowerPicker implements View.OnClickListener {
private int towerIndex;
public TowerPicker(int towerIndex) {
this.towerIndex = towerIndex;
}
public void onClick(View v) {
if (fromTower == UNDECIDED) {
ViewGroup tower = (ViewGroup) findViewById(towers[towerIndex]);
if (tower.getChildCount() > 0) {
fromTower = towerIndex;
Animation glowAnimation = AnimationUtils.loadAnimation(
HanoiActivity.this, R.anim.tower_glow);
// tower.getChildAt(0).startAnimation(glowAnimation);
tower.startAnimation(glowAnimation);
}
} else {
ViewGroup fromTowerView = (ViewGroup) findViewById(towers[fromTower]);
if (fromTower != towerIndex) {
ViewGroup toTowerView = (ViewGroup) findViewById(towers[towerIndex]);
View block = fromTowerView.getChildAt(0);
View supportingBlock = toTowerView.getChildAt(0);
if (supportingBlock == null
|| supportingBlock.getWidth() > block.getWidth()) {
(new BlockMover(block, fromTower, towerIndex)).move();
} else if (supportingBlock.getWidth() < block.getWidth()) {
block.clearAnimation();
}
}
fromTowerView.clearAnimation();
fromTower = UNDECIDED;
}
}
}
}