2014-07-25 65 views
0

我有坐标来对齐这些图像,编程方式。现在图像正在对齐,但图像从这个cooridinate开始,或者我应该说,坐标变成了它的左上角,我想让它成为imageview的中心。如何使imageview的坐标中心?帮助我,如果有人知道answer.Right现在我这样做:设置坐标为中心的ImageView android programamtically

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); 
+0

我不是说你问这些选项中的任何一个。我想做一个坐标,我的imageview的中心。现在通过上面的书面代码,坐标是我的imageview的左上角。 –

+0

你知道如何做到这一点?我有同样的问题。 – sboehnke

回答

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); 

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

相关问题