Android 将图像视图转换到左上角

Android 将图像视图转换到左上角,android,animation,Android,Animation,我很难找到一个解决方案,从相对视图的中心转换ImageView,使该ImageView的左上角保持在布局的左上角。大多数选项将ImageView的中心作为参考和/或在所有屏幕尺寸上都不起作用 这里是迄今为止最好的选项,除了ImageView的中心保持在布局的左上角(0,0)之外: TranslateAnimation anim = new TranslateAnimation( TranslateAnimation.RELATIVE_TO_PARENT,0.0f, Transla

我很难找到一个解决方案,从相对视图的中心转换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);

您可以尝试使用
translateImation.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);
RelativeLayout RelativeLayout=(RelativeLayout)findViewById();
ImageView ImageView=(ImageView)findViewById();
int deltaX=(relativeLayout.getWidth()/2)-(imageView.getWidth()/2);
int deltaY=(relativeLayout.getHeight()/2)-(imageView.getHeight()/2);
TranslateAnimation anim=新的TranslateAnimation(
TranslateAnimation.ABSOLUTE,0.0f,
TranslateAnimation.ABSOLUTE,-deltaX,
TranslateAnimation.ABSOLUTE,0.0f,
TranslateImation.ABSOLUTE,-deltaY
);
anim.setFillAfter(真);
动画设定持续时间(1000);
图像。startAnimation(动画);