我在查找从RelativeLayout中心转换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);
答案 0 :(得分: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);