2014-08-28 73 views
1

我在查找解决方案时发现问题,该解决方案从RelativeLayout的中心转换ImageView,方式为该ImageView的左上角保留在布局的左上角。大多数选项都会将ImageView的中心作为参考,并且/或在所有屏幕尺寸上都不能很好地工作。将ImageView翻译为左上角

这里是最好的选择,到目前为止,除了事实的ImageView的中心停留在布局的左上角(0,0):

TranslateAnimation anim = new TranslateAnimation(
    TranslateAnimation.RELATIVE_TO_PARENT,0.0f, 
    TranslateAnimation.RELATIVE_TO_PARENT,-0.5f, 
    TranslateAnimation.RELATIVE_TO_PARENT,0.0f, 
    TranslateAnimation.RELATIVE_TO_PARENT,-0.5f 
); 
anim.setFillAfter(true); 
anim.setDuration(1000); 

image.startAnimation(anim); 

回答

2

您可以用TranslateAnimation.ABSOLUTE尝试。有了这个,您可以更精确地计算增量值。

RelativeLayout relativeLayout = (RelativeLayout) findViewById(<R.id of your relative layout id>); 
ImageView imageView = (ImageView) findViewById(<R.id of your image view id>); 

int deltaX = (relativeLayout.getWidth()/2) - (imageView.getWidth()/2); 
int deltaY = (relativeLayout.getHeight()/2) - (imageView.getHeight()/2); 

TranslateAnimation anim = new TranslateAnimation(
    TranslateAnimation.ABSOLUTE, 0.0f, 
    TranslateAnimation.ABSOLUTE, -deltaX, 
    TranslateAnimation.ABSOLUTE, 0.0f, 
    TranslateAnimation.ABSOLUTE, -deltaY 
); 
anim.setFillAfter(true); 
anim.setDuration(1000); 

image.startAnimation(anim);