以编程方式将坐标设置为imageview android的中心

时间:2014-07-25 11:42:05

标签: android-layout layoutparams

我有坐标以编程方式对齐这些图像视图。现在图像正在对齐,但图像从这个cooridinate开始,或者我应该说坐标成为我的imageview的左上角,我想让它成为我的imageview的中心。如何制作imageview的坐标中心?如果有人知道答案,请帮助我。现在我正在这样做:

ImageView iv = new ImageView(this);

float x_coordinate = 256;
float y_coordinate = 350;
    iv.setX(x_coordinate);
    iv.setY(y_coordinate);

    iv.setImageResource(R.drawable.myimage);
    iv.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    mylayout.addView(iv);

1 个答案:

答案 0 :(得分:1)

我没有找到简单的一行解决方案,但我解决了我的问题,执行以下程序。这里是我的模型类,其中getX_Cord()和getY_Cord()返回图像的x和y坐标,并且你必须将图像保存在你想要在屏幕上设置的大小的可绘制文件夹中,这是R.drawable.placeImage。

// display parameters
Point size = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getSize(size);
width = size.x;
height = size.y;

final Place place = roomPlace.get(i);

Drawable d = getResources().getDrawable(R.drawable.placeImage);
int y = d.getIntrinsicHeight() / 2;
int x = d.getIntrinsicWidth() / 2;

placeImage.setX((place.getX_Cord() * width) - x);
placeImage.setY((place.getY_Cord() * height) - y);

可能有人得到更合适的解决方案,但我解决了我的问题。