您的位置:首页 > 其它

自定义Animation中如何让组件移动至屏幕右下角

2012-04-22 09:59 519 查看
package com.demo;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;

public class MyAnimation extends Animation {

private int parentWidth;
private int parentHeight;
private float objectWidth;
private float objectHeight;
private Camera camera;

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
Matrix matrix=t.getMatrix();
camera.save();
camera.getMatrix(matrix);  //必须写在preTranslate postTranslate方法之前否则达不到想要的效果

matrix.preTranslate(0,0); //动画起始点,默认左上角
matrix.postTranslate((parentWidth-objectWidth)*interpolatedTime, (parentHeight-objectHeight)*interpolatedTime);
/*
matrix.preTranslate(parentWidth/2,parentHeight/2);
matrix.postTranslate((parentWidth/2-objectWidth)*interpolatedTime, (parentHeight/2-objectHeight)*interpolatedTime);
*/

camera.restore();

}

@Override
public void initialize(int width, int height, int parentWidth, //width,height表目标组件的尺寸,
int parentHeight) {                                    //parentWidth,parentHeight表目的组件的父组件的尺寸
// TODO Auto-generated method stub
super.initialize(width, height, parentWidth, parentHeight);
setDuration(5000);
setFillAfter(false); //设置是否保存动画后的位置,false表示动画后恢复到初始位置
objectHeight=height;
objectWidth=width;
this.parentWidth=parentWidth;
this.parentHeight=parentHeight;
camera=new Camera();
setInterpolator(new LinearInterpolator()); //设置动画播放速率
}

}
本文出自 “Android开发旅程” 博客,请务必保留此出处http://randino.blog.51cto.com/1972239/841044
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: