2014-08-28 79 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);