android 中 Tween动画主要是对图片进行平移、旋转、缩放、渐变。下面通过自定义一个View来实现这些功能。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
@SuppressWarnings("deprecation")
public class AnimationView extends AbsoluteLayout{
private static final int VIEW_COUNTS = 8;
private static final long ANIM_DURATION = 1000;
private static final long DELAY_DURATION = 500;
private int childLength = 80;
private boolean isInitialized = false;
private boolean isRunning = false;
private Bitmap bm;
private Point[] mStartPoints = new Point[VIEW_COUNTS];
private Point mReachPoint = new Point();
private ImageView[] mAnimViews = new ImageView[VIEW_COUNTS];
private AnimationSet[] mAnimationArray = new AnimationSet[VIEW_COUNTS];
public AnimationView(Context context, AttributeSet attrs) {
super(context,attrs);
}
//开始动画
public void startAnim(int index) {
if (!isInitialized) {
initView();
}
isRunning = true;
if (index >=0 ) {
mAnimViews[index].startAnimation(mAnimationArray[index]);
} else {
for (int i = 0; i < VIEW_COUNTS; i++) {
mAnimViews[i].startAnimation(mAnimationArray[i]);
}
}
}
//结束动画
public void stopAnim() {
isRunning = false;
}
//初始化控件
private void initView() {
bm = getAnimBitmap();
loadAnimStartLocation();
loadAnimReachLocation();
for (int i = 0; i < VIEW_COUNTS; i++) {
mAnimViews[i] = createAnimView(i);
mAnimationArray[i] = createAnimation(i);
mAnimationArray[i]
.setAnimationListener(new AnimListener(i));
}
isInitialized = true;
}
private AnimationSet createAnimation(int i) {
AnimationSet anim = new AnimationSet(true);
Point mStart = mStartPoints[i];
//旋转动画
RotateAnimation ra = new RotateAnimation(0F, 719F,
Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF,
0.5F);
anim.setDuration(400);
//平移动画
TranslateAnimation ta = new TranslateAnimation(0, mReachPoint.x
- mStart.x-childLength, 0, mReachPoint.y - mStart.y-childLength);
ta.setDuration(400);
//放大动画
ScaleAnimation sa = new ScaleAnimation(0.5F, 2F, 0.5F, 2F);
sa.setDuration(400);
sa.setStartOffset(DELAY_DURATION * (i + 1)/2);
//设置透明度渐变动画
AlphaAnimation aa = new AlphaAnimation(1.0F, 0F);
aa.setDuration(400);
aa.setStartOffset(DELAY_DURATION * (i + 1)/2);
anim.addAnimation(ra);
anim.addAnimation(sa);
anim.addAnimation(ta);
anim.addAnimation(aa);
anim.setDuration(ANIM_DURATION * (i + 1));
anim.setFillAfter(true);
return anim;
}
//设置图片
private ImageView createAnimView(int index) {
ImageView anim = new ImageView(getContext());
anim.setImageBitmap(bm);
addView(anim, new LayoutParams(childLength, childLength, mStartPoints[index].x,
mStartPoints[index].y));
return anim;
}
//从资源中取图片
private Bitmap getAnimBitmap() {
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
return bm;
}
//长方形的4个顶点
private void loadAnimStartLocation() {
mStartPoints[0] = new Point(0, 0);
mStartPoints[1] = new Point(0,getHeight()/2);
mStartPoints[2] = new Point(0, getHeight());
mStartPoints[3] = new Point(getWidth()/2,getHeight());
mStartPoints[4] = new Point(getWidth(), getHeight());
mStartPoints[5] = new Point(getWidth(), getHeight()/2);
mStartPoints[6] = new Point(getWidth(), 0);
mStartPoints[7] = new Point(getWidth()/2, 0);
}
//中心点
private void loadAnimReachLocation() {
mReachPoint = new Point(getWidth()/2, getHeight()/2);
}
//当view的大小发生变化时触发
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
startAnim(-1);
}
//动画的运行的监听
class AnimListener implements AnimationListener {
private int index;
public AnimListener(int index) {
this.index = index;
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
postDelayed(new Runnable() {
@Override
public void run() {
if (isRunning) {
startAnim(index);
}
}
}, 200);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
}