Android开发 TranslateAnimation和ScaleAnimation动画叠加偏移量的计算

目标:在使用TranslateAnimation移动的同时,用ScaleAnimation放大图片,在屏幕居中显示. 看TranslateAnimation的构造方法: TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 四个参数分别是,动画开始时x座标偏移量,结束时x座标偏移量,开始时y座标偏移量,结束时y座标偏移量,单位像素,有一点搞不懂的是,为什么是float,像素还有半个? 缩放动画 ScaleAnimation(float fromX, float toX, float fromY, float toY) fromX 宽度起始缩放比例,1为原始大小 toX 宽度最终缩放比例 fromY 高度起始缩放比例,1为原始大小 toY 高度最终缩放比例 在TranslateAnimation里,偏移量还要考虑到scale的影响 下面是主要代码:

//计算activity的size,不包括actionbar
int activityWidth=findViewById(android.R.id.content).getWidth();
int activityHeight=findViewById(android.R.id.content).getHeight();
//获取图片的真实大小
int[] size=ImageUtils.getBitmapSize(this, R.drawable.wallpaper);
int bmpWidth=size[0];
int bmpHeight=size[1];
//计算目标显示尺寸
float displayWidth=0;
float displayHeight=0;
// 如果图片尺寸比activity的尺寸小,直接原大小显示
if (activityWidth > bmpWidth && activityHeight > bmpHeight) {
    displayWidth=bmpWidth;
    displayHeight=bmpHeight;
}else{
    //缩放
    // 计算宽高比
    float target_ratio = (float) activityWidth / activityHeight;
    float real_ratio = bmpWidth / bmpHeight;
    if (real_ratio > target_ratio) {
        // 如果width太大,height太小,以activityWidth为基准
        displayWidth=activityWidth;
        displayHeight=((float)activityWidth/bmpWidth)*bmpHeight;
    } else {
        displayHeight=activityHeight;
        displayWidth=((float)activityHeight/bmpHeight)*bmpWidth;
    }
}
//缩放比例,等比例放大,toY=toX
float toX=displayWidth/imageView.getWidth();

AnimationSet animationSet = new AnimationSet(true);
animationSet.setDuration(500);
animationSet.setInterpolator(new AccelerateInterpolator());

//计算左上角偏移量,有scale需要乘的1/toX
float toXDelta=((activityWidth-displayWidth)/2.0f-imageView.getX())*(1/toX);
float toYDelta=((activityHeight-displayHeight)/2.0f-imageView.getY())*(1/toX);
//移动
TranslateAnimation translateAnimation=new TranslateAnimation(0, toXDelta, 0, toYDelta);
animationSet.addAnimation(translateAnimation);

ScaleAnimation scaleAnimation=new ScaleAnimation(1.0f, toX, 1.0f, toX); 
animationSet.addAnimation(scaleAnimation);
animationSet.setFillAfter(true);
imageView.startAnimation(animationSet);