2014-07-25 68 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); 

可能會有人得到更合適的解決方案,但我解決了我的問題做到這一點。

相關問題