【问题标题】:set coordinates as center of imageview Android programmatically以编程方式将坐标设置为imageview Android的中心
【发布时间】:2019-11-24 11:10:14
【问题描述】:

我有坐标以编程方式对齐这些图像视图。现在图像正在对齐,但图像从这个坐标开始,或者我应该说坐标成为我的图像视图的左上角,我想让它成为我的图像视图的中心。如何制作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);

【问题讨论】:

  • 我不是说你问的任何这些选项。我想制作一个坐标,我的图像视图的中心。现在通过上面编写的代码,坐标位于我的图像视图的左上角。
  • 你知道怎么做吗?我有同样的问题。

标签: android-layout layoutparams


【解决方案1】:

我没有找到简单的单行解决方案,但我通过以下过程解决了我的问题。这里 Place 是我的模型类,其中 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);

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

【讨论】:

    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 2020-10-06
    • 2012-05-05
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多